DEV Community

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

Posted on • Originally published at waylonwalker.com

8 3

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

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (2)

Collapse
 
ben profile image
Ben Halpern

Nice tip!

Collapse
 
waylonwalker profile image
Waylon Walker

Thanks Ben!

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay