安装
pip3 install gitpython
基本使用
# 从远处仓库下载代码到本地
import os
from git.repo import Repo
# 创建本地存储地址
download_path = os.path.join('jason','NB')
# 从远程仓库下载代码
Repo.clone_from('https://github.com/DominicJi/TeachTest.git',to_path=download_path,branch='master')
常用方法大全
# ############## 2. pull最新版代码 ##############
import os
from git.repo import Repo
local_path = os.path.join('jason', 'NB')
repo = Repo(local_path)
repo.git.pull()
# ############## 3. 获取所有分支 ##############
import os
from git.repo import Repo
local_path = os.path.join('jason', 'NB')
repo = Repo(local_path)
branches = repo.remote().refs
for item in branches:
print(item.remote_head)
# ############## 4. 获取所有版本 ##############
import os
from git.repo import Repo
local_path = os.path.join('jason', 'NB')
repo = Repo(local_path)
for tag in repo.tags:
print(tag.name)
# ############## 5. 获取所有commit ##############
import os
from git.repo import Repo
local_path = os.path.join('jason', 'NB')
repo = Repo(local_path)
# 将所有提交记录结果格式成json格式字符