问题
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)
回答1:
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.
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")
来源:https://stackoverflow.com/questions/34711540/cloning-only-the-main-branch-using-pygit2