GitPython如何克隆所有分支?

3

我找到了一个使用Bash脚本完成相同任务的Bash脚本。

使用GitPython创建存储库的完整备份,无法弄清楚如何在不必手动检出它们的情况下显示所有分支。

Repo.clone_from(repo, folder)

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

@AlexeyShrub 最好使用kwargs:mirror=True(请参见我的更新答案) - dlazesz
所有分支都可以使用“Repo.remotes.origin.refs”列出。 - Ravi Bandoju
2个回答

4

我最近偶然发现了这个帖子,但没有找到任何解决方案,所以我自己想出了一个。它不是最优的,也不太美观,但至少它能用:

# 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. 检出分支并根据需要创建它们

附注:我认为Gitpython没有内置方法可以使用clone_from完成此操作。

更新:

no_single_branch=True选项在GitPyhton中等同于git CLI中的--no-single-branch(其他“无值”参数也可以使用True值进行提供)。

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

-1
for b in Repo.clone_from(repo, folder).remote[0].fetch():
   print(b.name)

类型错误:'instancemethod'对象没有属性'getitem'当我使用这段代码时,目前出现了这个错误。 在Repo.clone_from(repo, folder).remote[0].fetch()中,remote[0]返回的是一个实例方法,而不是一个可索引的对象。因此,尝试使用[]访问它会导致TypeError。 - ghh1415
对于以 repo 为地址,folder 为存储目录的仓库,通过 remotes.origin.fetch() 方法克隆下来的所有分支名称都会打印出来。但将它们复制到文件夹中仍然存在问题。 - ghh1415

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接