NetAuto Academy

Git / NetDevOps

GitPython: Driving Git from Python

Advanced12 min read

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
🎓 Teach This Lesson

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.

real, verified install
pip install GitPython
# Successfully installed GitPython-3.1.60 gitdb-4.0.12 smmap-5.0.3

Verified 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.

Quick checkConcept check
What is GitPython's actual relationship to the git binary already verified in Lesson 1?

The object model: real, verified

real signatures, installed GitPython 3.1.60
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.

Quick checkIdentify the correct code
Which correctly represents GitPython's real object relationships?

The same real workflow, now explicitly through this object model

real, executed -- identical mechanics to Lesson 2, read through GitPython's 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 -> Actor

No new mechanics here -- Lesson 2's real workflow, made explicit: which GitPython class each call belongs to.

output — real, verified
commit1: 4581349b 'Initial SW1 config'
commit1 author: Network Automation Curriculum
commit1 committed_date: 2026-08-27 10:36:51+05:30

Real, 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.

Quick checkPredict output
Based on the real output above, what type is commit.committed_datetime?

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.

Quick checkScenario
Why does this lesson deliberately not introduce any new git capability beyond what Lesson 2 already covered?

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.