I am using the GitPython library and was wondering how to get all commits on a branch in range of two commit sha-1's. I have the start one and end one. Is there any way to get list of them?
I have instantiated the repo object and was wondering if there was a way to query it and obtain a list of commits in the range of two shas?
Would be looking to do something similar to this command but return them as a list:
git log e0d8a4c3fec7ef2c352342c2ffada21fa07c1dc..63af686e626e0a5cbb0508367983765154e188ce --pretty=format:%h,%an,%s > commits.csv
Seems like there is Repo.iter_commits() method but can't see how to specify a range.
repo.iter_commits("revA..revB")
@rtn's answer has the solution, but it's a little hidden. Here's a minimal example for ease of understanding.
from git import Repo
repo = Repo("/path/to/your/repo")
start_rev, end_rev = "HEAD~2", "HEAD" # Or any other valid ref
commits = repo.iter_commits(f"{start_rev}..{end_rev}")
for commit in commits:
# Do your thing...
start_rev
and end_rev
can be a git commit hash, branch name, or tag name -- i.e. anything supported by git log A..B
From the docs for Repo.iter_commits
:
to receive only commits between two named revisions, use the “revA…revB” revision specifier