The Simplest Way To Use Git In Python Gitpyton Git Class

Pavol Kutaj
Apr 16, 2021

usecase

The aim of this how-to-guide🏁 is to make git working in Python scripts fast for simple tasks.

1. steps

  1. install gitPython
  2. import the module with import git
  3. use the Git class and pass commands with git.Git().<gitCommand>(<param1>, <param2>, ...)
  4. to get output in CLI, embed into a print statement

2. examples

  • this is for git pull orogin master
import git
print(' UPDATES '.center(80, '~'))
print(git.Git().pull('origin', 'master'))
  • output
~~~~~~~~~~~~~~~~~~ UPDATES ~~~~~~~~~~~~~~~~~~~~~~~
Already up to date.

3. sources

--

--

Pavol Kutaj

Today I Learnt | Infrastructure Support Engineer at snowplow.io with a passion for cloud infrastructure/terraform/python/docs. More at https://pavol.kutaj.com