Search code examples
gitpython

GitPython get list of commits from a repo-branch


I have a Git repository URL and a branch name.

Using GitPython how do I get all the commits from the branch?


Solution

  • From https://gitpython.readthedocs.io/en/stable/tutorial.html

    Meet the Repo type

    The first step is to create a git.Repo object to represent your repository.

    from git import Repo
    # rorepo is a Repo instance pointing to the git-python repository.
    
    # For all you know, the first argument to Repo is a path to the repository
    
    # you want to work with
    
    repo = Repo(self.rorepo.working_tree_dir)
    
    assert not repo.bare
    

    In the above example, the directory self.rorepo.working_tree_dir equals /Users/mtrier/Development/git-python and is my working repository which contains the .git directory. You can also initialize GitPython with a bare repository.

    ...

    ...

    The Commit object

    Commit objects contain information about a specific commit. Obtain commits using references as done in Examining References or as follows.

    Obtain commits at the specified revision

    repo.commit('master')
    
    repo.commit('v0.8.1')
    
    repo.commit('HEAD~10')
    

    ...

    I would suggest reading the tutorial I quoted and at least the entire The Commit Object section of it. (https://gitpython.readthedocs.io/en/stable/tutorial.html#the-commit-object)