使用PyGit2仅克隆主分支 [英] Cloning only the main branch using PyGit2

查看:121
本文介绍了使用PyGit2仅克隆主分支的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想克隆一些远程存储库,但只检索主分支.

I want to clone some remote repositories, but only retrieving the main branch.

我的代码当前获得了所有分支.

My code currently gets all of the branches.

def init_remote(repo, name, url):
    # Create the remote with a mirroring url
    remote = repo.remotes.create(name, url, "+refs/*:refs/*")
    # And set the configuration option to true for the push command
    mirror_var = "remote.{}.mirror".format(name)
    repo.config[mirror_var] = True
    # Return the remote, which pygit2 will use to perform the clone
    return remote

pygit2.clone_repository(url, "../../clones/"+location, remote=init_remote)

推荐答案

您的代码不仅获得了所有分支,而且还镜像了远程服务器,并获得了远程跟踪分支,这会导致一些令人困惑的布局.

Your code doesn't just get all the branches, it mirrors the remote, getting its remote-tracking branches as well, which can lead to some confusing layout.

您已经设置了自己的refspec,因此需要做的是将refspec设置为下载默认分支.如果知道,您可以更改代码以仅获取一个分支

You're already setting your own refspec, so what you need to do is set the refspec to download the default branch. If you know it you can change the code to get just the one branch

remote = repo.remotes.create(name, url, "+refs/heads/master:refs/heads/master")

这篇关于使用PyGit2仅克隆主分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆