pip install GitPython import git★The import name isgit(packageGitPython). It shells out to the installedgitexecutable.repo = git.Repo(".") # open existing repo = git.Repo.clone_from(url, "dest/") # clone repo = git.Repo.init("newdir") # create★Three ways to get aRepo— the entry point for everything.repo.is_dirty(untracked_files=True) · repo.untracked_files★Working-tree status. Note:is_dirty()ignores untracked files unless you passuntracked_files=True.repo.git.status() · repo.git.checkout("main") # raw git passthroughrepo.git.<cmd>(...)runs any git command — the escape hatch when the object API doesn't cover something.
repo.index.add(["a.py", "b.py"]) commit = repo.index.commit("Add files")★Stage then commit via the index.commit.hexshais the new SHA.for c in repo.iter_commits("main", max_count=10): print(c.hexsha[:7], c.summary, c.author.name)★Walk history. Commit objects expose.message/.summary,.author,.committed_datetime,.parents.repo.active_branch.name · repo.heads feature = repo.create_head("feature"); feature.checkout()★Branches areHeadobjects inrepo.heads. Create, thencheckout(). Tags:repo.create_tag("v1.0").
origin = repo.remote("origin") origin.fetch() · origin.pull() · origin.push()★Remote sync.push()/pull()return info objects you can inspect for flags/errors. Add one withrepo.create_remote(name, url).repo.index.diff(None) # unstaged changes repo.index.diff("HEAD") # staged changesStructured diffs between the index and the working tree / a commit.print(repo.git.diff("HEAD~1", "HEAD")) # textual diffFor a plain unified diff, drop to the git passthrough.