GitPython 是一个用于操作 Git 版本库的 python 包,
它提供了一系列的对象模型(库 - Repo、树 - Tree、提交 - Commit等)
用于操作版本库中的相应对象。
版本库对象 - Repo
首先,使用包含 .git 文件夹的版本库路径创建 git.Repo 对象
from git import Repo
# 创建版本库对象
repo = git.Repo(r'E:\Notes')
然后便可以使用这个 Repo 对象对版本库进行操作,如:
# 版本库是否为空版本库
repo.bare
# 当前工作区是否干净
repo.is_dirty()
# 版本库中未跟踪的文件列表
repo.untracked_files
# 克隆版本库
repo.clone('clone_path')
# 压缩版本库到 tar 文件
with open('repo.tar', 'wb') as fp:
repo.archive(fp)
# 新建分支
repo.create_head('branchname')
# 查看当前分支
repo.active_branch
索引/暂存区对象 - Index
Git 术语中,index 表示暂存区,为下次将要提交到版本库里的文件,
GitPython 提供 Repo.Index 来操作暂存区,如添加、提交操作
index = repo.index
index.