Python GitPython

From wikinotes

I'm only taking a fleeting glance at this right now - it seems primarily aimed at people that want to read/automate the git database to do crazy things.

Documentation

official docs https://gitpython.readthedocs.io/en/stable/


Basics

Repo

from git import Repo


repo = Repo(
    '/home/will/progs/python/path/to/file.py', 
    search_parent_directories=True
)
assert not repo.bare  # assert this is a git-project, not git-repository

repo.is_dirty(untracked_files=True)

repo.remote()                              ## get tracked remote (push/pull...)
repo.remotes

repo.active_branch                         ## get current branch 
repo.branches

Remotes

remote = repo.remote()                    ## current remote 
remote.url                                ## remote's url
remote.exists()
remote.pull()
remote.push()

Branches

branch = repo.active_branch
branch.checkout()
branch.log()                      # list all commits in log

repo.create_head('branch')        # create new branch

Index (stage)

repo.untracked_files                      ## list of untracked files
repo.index.diff()                         ## list of changed tracked files
repo.index.add( <list> )                  ## add list of files to the index(stage)


Common Tasks

git clone

repo   = git.Repo.init( '/home/will/dev/testrepo' )
origin = repo.create_remote( 'origin', url=gitsource )
assert   origin.exists()
origin.pull('master')