
GitPython 是一个用于操作 Git 版本库的 python 包,它提供了一系列的对象模型(库 - Repo
、树 - Tree
、提交 - Commit
等),用于操作版本库中的相应对象。
模块安装
pip install gitpython
初始化
from git import Repo
repo =Repo("D:\s17\c.py") #git文件的路径
操作
from git import Repo
repo =Repo("F:\git") #git文件的路径 #获取当前所在的分支 git branch
print(repo.active_branch) #添加到缓存区 git add命令
repo.index.add(["test.txt"]) #提交到版本库 git commit -m
repo.index.commit("创建test文件") #添加版本号 git tag -a
repo.create_tag("v0.1") #创建分支 git branch dev
repo.create_head("dev") #回退到某个版本 git reset --hard hash
repo.index.reset(commit="哈希地址",head=True) #获取所有分支
print([str(b) for b in repo.branches]) #查看标签
print(repo.tags)
import git
#git clone
clone = git.Repo.clone_from("url","to_path") #git pull
clone.remote().pull() #git push
clone.remote().push()
gitpython还可以直接操作git命令
g = git.Git("PATH")
g.add()
g.commit("-m ")