DEV Community

Cover image for List all git commits with GitPython
Waylon Walker
Waylon Walker

Posted on • Originally published at waylonwalker.com

List all git commits with GitPython

I am getting ready to do some timeseries analysis on a git repo with python, my first step is to figure out a way to list all of the git commits so that I can analyze each one however I want. The GitPython library made this almost trivial once I realized how.

from git import Repo

repo = Repo('.') commits = repo.iter_commits()
Enter fullscreen mode Exit fullscreen mode

This returns a generator, if you are iterating over them this is likely what you want.

commits
# <generator object Commit._iter_from_process_or_stream at 0x7f3307584510>
Enter fullscreen mode Exit fullscreen mode

The generator will return git.Commit objects with lots of information about each commit such as hexsha, author, commited_datetime, gpgsig, and
message.

next(commits)
# <git.Commit "d125317892d0fab10a36638a2d23356ba25c5621">
Enter fullscreen mode Exit fullscreen mode

Discussion (2)

Collapse
ben profile image
Ben Halpern

Nice tip!

Collapse
waylonwalker profile image
Waylon Walker Author

Thanks Ben!