一、参考资料
二、常用指令
安装git模块
pip install gitpython
初始化
from git import Repo
# 创建一个git文件夹
Repo.init('/data/test2')
添加与提交
#将文件提交到缓存区
repo.index.add(['a.txt'])
# 将缓存区文件提交到版本库
repo.inex.commit('update new')
回滚
# 回滚缓存区文件
repo.index.checkout(['a.txt'])
#回滚版本库文件
repo.index.reset(commit='486a9565e07ad291756159dd015eab6acda47e25',head=True)
创建分支
# 创建分支
repo.create_head('debug')
创建tag标签
# 创建tag
repo.create_tag('v1.0')
拉取远程仓库
clone_repo = Repo.clone_from(url='root@101.133.170.175:/root/YOYOFile/Git_Server/TCP_Hardware_Server.git',to_path='./Hardware_TCP_Server/')
# 拉取远程代码
clone_repo=git.Repo.clone_from('https://github.com/wangfeng7399/syncmysql.git','/data/test3')
remote_101 = clone_repo.remote()
从远程版本库拉取分支
#后面跟需要拉取的分支名称
remote_101.pull('master')
推送本地分支到远程版本库
#后面跟需要提交的分支名称
remote_101.push('master')
使用原生命令
# 所有git支持的命令这里都支持
repo=git.Git('/data/test4')
repo.checkout('debug')
print(repo.status())