-3

I have a Git repository URL and a branch name.

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

torek
  • 448,244
  • 59
  • 642
  • 775
intrigued_66
  • 16,082
  • 51
  • 118
  • 189

1 Answers1

1

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)

(Sorry for bad formatting, for some reason stackoverflow didn't let me make it a quote block)

  • How do you do this without the repo existing on your local disk? I don't want to clone the repo, just read the branch commits from the Github repository. – intrigued_66 Jan 07 '22 at 18:52
  • @mezamorphic I suppose you could first clone the repo, which would probably be easier but you could also continue using just GitPython, for doing that I would suggest checking out what they mention about remote in the same tutorial in the `Advanced Repo Usage` section of the `Meet The Repo` section and you should also check out this relevant documentation: https://gitpython.readthedocs.io/en/stable/reference.html#module-git.refs.remote – LucaRicardo Jan 07 '22 at 23:21