python - GitPython 如何克隆所有分支?

标签 python git gitpython

我找到了这个 Bash脚本,它完成相同的任务,但在 Bash 中。

使用 GitPython 创建存储库的完整备份,似乎无法弄清楚如何在无需手动检查的情况下显示所有分支。

Repo.clone_from(repo, folder)

我在循环中使用这一行来复制存储库中的所有文件。

最佳答案

我最近偶然发现了这个线程,但没有找到任何解决方案,所以我自己酿造了。它总比没有好,既不是最优的也不是漂亮的,但至少它有效:

# Clone master
repo = Repo.clone_from('https://github.com/user/stuff.git', 'work_dir', branch='master')

# Clone the other branches as needed and setup them tracking the remote
for b in repo.remote().fetch():
    repo.git.checkout('-B', b.name.split('/')[1], b.name)

解释:

  1. 克隆主分支
  2. 获取远程分支名称
  3. 检查分支并根据需要创建它们

PS:我认为 Gitpython 没有内置的方法来使用 clone_from 执行此操作。

更新:

GitPyhton 中的

no_single_branch=True 选项等于 git CLI 中的 --no-single-branch (其他“无值”参数也可以与 True 一起提供 值)

repo = Repo.clone_from('https://github.com/user/stuff.git', 'work_dir', branch='master', no_single_branch=True)

关于python - GitPython 如何克隆所有分支?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42132676/

相关文章:

python - 我可以在Python中使用什么代码将40,000行纬度和经度数字(以弧度为单位)转换为笛卡尔坐标?

java - 将本地工作区 checkin 新的 git repo

linux - 如何将 git 存储库的更改存储在另一个服务器端存储库中?

python - GitPython 列出特定文件的所有提交

用于 git clone 的 Python 脚本,无需在提示符下输入密码

python - 是否可以从迭代器获取下一项的索引?

Python麻省理工开放课件股票市场模拟不完整?

python - 使用 sklearn 使用 k 折叠来预测测试数据的类别

git - 需要从 bitbucket 中删除特定提交

python - gitpython:git commit 的命令语法