Git / NetDevOps
GitPython: Driving Git from Python
What you'll learn
- GitPython's real object model: Repo, IndexFile, Commit
- How GitPython relates to the real, local git binary underneath it
- Real, verified signatures for the operations Lesson 2 already used
- That GitPython is a driver, not a reimplementation of git
Listen to a guided explanation of this lesson.
Spoken narration isn't supported in this browser.
A real client library, not a reimplementation
The same role pynetbox played, one phase ago
pynetbox (Phase 3.7) is a real Python client wrapping raw requests calls to NetBox's REST API. GitPython plays the exact same role here: a real Python client wrapping the real, local git binary already verified in Lesson 1. It doesn't reimplement version control -- every operation ultimately shells out to the same git you'd run by hand.
pip install GitPython
# Successfully installed GitPython-3.1.60 gitdb-4.0.12 smmap-5.0.3Verified directly. A minimal footprint -- no server components, matching every dependency verified since Phase 3.0's own discipline of checking what actually gets pulled in.
The object model: real, verified
Repo.init(path=None, mkdir=True, ...) -> Repo
IndexFile.add(items, force=True, ...) -> List[BaseIndexEntry]
IndexFile.commit(message, parent_commits=None, ...) -> Commit
Repo.iter_commits(rev=None, paths="", ...) -> Iterator[Commit]Verified via inspect.signature. Repo is the repository itself; repo.index is an IndexFile (the staging area); commit() returns a real Commit object.
The same real workflow, now explicitly through this object model
repo = git.Repo.init("repo1") # Repo
repo.index.add(["sw1.cfg"]) # IndexFile.add()
commit = repo.index.commit("...") # IndexFile.commit() -> Commit
commit.hexsha # Commit.hexsha
commit.message # Commit.message
commit.author # Commit.author -> ActorNo new mechanics here -- Lesson 2's real workflow, made explicit: which GitPython class each call belongs to.
commit1: 4581349b 'Initial SW1 config'
commit1 author: Network Automation Curriculum
commit1 committed_date: 2026-08-27 10:36:51+05:30Real, verified output. author is a real Actor object (printed here as its name); committed_datetime is a real, timezone-aware Python datetime -- not a plain string.
Where this is headed
One capability at a time
This lesson covered exactly what Lesson 2 already did, through GitPython's object model. Lesson 4 adds real, new capability -- branches and diffing one branch against another, the actual review mechanism this module builds toward.
Key takeaways
- GitPython is a real client library over the real, local git binary -- the same role pynetbox played over requests (Phase 3.7), not a reimplementation.
- Repo, IndexFile, and Commit are real, verified classes -- repo.index is the staging area; commit() returns a real Commit object.
- Commit.author and Commit.committed_datetime are real, typed objects (Actor, timezone-aware datetime) -- not plain strings needing manual parsing.
- This lesson deliberately covers no new Git concept -- only Lesson 2's mechanics, read through GitPython's real object model.