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 ")