如何“撤销”一个 --single-branch 克隆?

166

我使用以下命令克隆了一个代码库:

git clone -b <分支名称> --single-branch <github网址> <目标目录>

这样只克隆了一个分支,但是现在我想要切换到主分支和其他分支。除了清空并重新开始克隆整个代码库之外,还有没有其他方法可以取消--single-branch选项呢?

7个回答

240

您可以这样告诉Git拉取所有分支:

git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
git fetch origin

如果你查看 .git/config 文件,它会像这样:

[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
    ignorecase = true
    precomposeunicode = false
[remote "origin"]
    url = https://github.com/owner/repo.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
    remote = origin
    merge = refs/heads/master
    rebase = true

我将其与完全克隆进行比较,发现唯一的区别是在[remote "origin"]下面的“fetch”。

注意:我正在运行Git版本1.8.2。如果您正在运行旧版本的Git,则可能已更改配置选项。如果我的命令无效,则建议您查看.git/config,以查看是否有类似的内容。


3
感谢你,因为我读了很多文章并进行了大量的搜索,但都找不到像这样的信息。 - danieltalsky
2
很高兴能帮忙。git命令行工具非常强大(事实上,大多数命令都是基于其他命令实现的),因此一旦您了解存储库的布局(基本上是.git文件夹的工作原理),就可以使用它来完成许多任务。 - sarahhodne
这对我没有用 - 运行这些命令后,git show-ref tags 仍然失败。 - felixfbecker
1
请注意,此解决方案也适用于使用以下语法进行浅克隆的分支,该分支经历了 OP 描述的相同缺点:git clone --depth=1 --branch branchname git@github.com:foobar/repo.git destinationpath - danemacmillan
2
哇,这远非直观!Git 没有一个 readily 的命令来完成这个任务,这很奇怪。是的,我搞定了...现在我知道 .git 目录下的 config 文件了! - kashiraja
我能够在不手动更改配置的情况下完成这个操作:git remote set-branches --add origin '*' 然后 git fetch - Vladimir Still

95

如果你想添加一个单独的分支,可以按照以下步骤进行:

git remote set-branches --add origin [remote-branch]
git fetch origin [remote-branch]:[local-branch]

支持 git 版本 1.9.1


8
这将在 .git/config 文件中添加多个 fetch = 行,在您想要在远程两个分支之间切换但不想获取一大堆其他分支时非常方便。谢谢! - Terry Brown
这并没有回答问题。 - StudioLE

25

如果我使用了--single-branch来克隆本地仓库,那么要将另一个远程分支添加到我的本地仓库中,我可以使用以下命令:

git remote set-branches --add origin [remote-branch]
git fetch
git checkout [remote-branch]

您也可以为[remote-branch]使用通配符,例如:

git remote set-branches --add origin release-1.*
git fetch
git checkout release-1.5

这个方法适用于 git 版本 2.21.1。其他建议使用 git fetch origin [remote-branch]:[local-branch] 的答案并不起作用,因为它会在未跟踪状态下创建本地分支。运行 git pull 命令时会再次尝试将远程分支的所有提交合并到我的本地分支。


3
第二次回到这个答案,非常有用。谢谢! - godbout
2
最有用的答案 - Denis Itskovich
1
我已经来过这里好几次了 xd - Nestor
1
非常感谢! - Luka

7

对于我而言,以下方法可行:

git remote remove origin
git remote add origin https://*<yourPath>*.git
git fetch

不需要修改 git config 的最简单解决方案。 - Prasanna Venkatesh
这也应该是一个被接受的答案。完全没问题。 - undefined

1

只需将原始存储库添加为新的远程存储库,并从那里工作即可?

git remote add path/to/myrepo myNewOrigin
git fetch myNewOrigin

你甚至可以删除当前的“origin”远程并将“myNewOrigin”重命名为“origin”,如果你想这样做。
从那里可以进行拉取/合并/变基。

0

我最初尝试了Dominik Pawlak的答案,并且它有效。但在我将更多代码提交到新分支后,我无法再拉取任何进一步的更改。

还有另一种完全重置单分支的方法,在这里没有提到:

git remote remove origin
git remote add origin git@gitlab.com:{yourProject}/{yourRepo}.git
git branch --set-upstream-to=origin/{yourBranch}  {yourBranch}
git pull

这将所有内容重置为原始状态。


0

只需更改本地存储库的.git/config文件,即[remote origin] sectionfetch行。

在此之前,类似于这样的内容

[remote "origin"]
    url = https://github.com/owner/repo.git
    fetch = +refs/heads/master:refs/remotes/origin/master

之后就会这样

[remote "origin"]
    url = https://github.com/owner/repo.git
    fetch = +refs/heads/*:refs/remotes/origin/*

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