pygit2库,可以快速没有安装git情况下可以进行克隆操作,并且支持大部分git克隆.
安装方法:
pip install pygit2
完整实例:
import os
import pygit2
# 仓库 URL
repo_url = 'https://gitee.com/sofu456/chat_robot.git'
# 获取仓库名称
repo_name = repo_url.rstrip('.git').split('/')[-1]
# 创建本地目录
local_path = os.path.join(os.getcwd(), repo_name)
os.makedirs(local_path, exist_ok=True)
# 克隆仓库
pygit2.clone_repository(repo_url, local_path)
pygit2其他功能:
e_repository(url, path),克隆远程仓库到本地指定路径。
打开仓库:pygit2.Repository(path),打开本地仓库。
创建仓库:pygit2.init_repository(path, bare=False),创建一个新的本地仓库。
检查分支:repo.listall_branches(),列出所有分支。
创建分支:repo.create_branch(name, commit, force=False),在当前仓库中创建一个新的分支。
切换分支:repo.checkout(branch_or_commit, force=False),切换到指定的分支或提交。
添加文件:repo.index.add(path),将文件添加到暂存区。
提交更改:repo.index.write_tree(),将暂存区的更改提交为新的树对象。
创建提交:repo.create_commit(refname, author, committer, message, tree, parents),创建一个新的提交。
推送到远程:repo.remotes['origin'].push(refspecs),将更改推送到远程仓库。
拉取远程更改:repo.remotes['origin'].fetch(),从远程仓库拉取最新更改。
合并分支:repo.merge(branch_or_commit),将指定分支或提交合并到当前分支。
查看提交历史:repo.walk(commit_id, pygit2.GIT_SORT_TIME),按时间顺序遍历提交历史。
来源—–python学霸