git 如何“撤消” --single-branch 克隆?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17714159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 08:47:39  来源:igfitidea点击:

How do I "undo" a --single-branch clone?

gitgithub

提问by danieltalsky

I cloned a repo using the

我克隆了一个回购使用

git clone -b <branch name> --single-branch <github url> <target directory>

git clone -b <branch name> --single-branch <github url> <target directory>

This cloned ONLY this branch, but now I want to switch to the master and other branches. Is there any way besides clearing it out and starting over to clone the rest of the repo that I can undo the --single-branch preference?

这仅克隆了这个分支,但现在我想切换到 master 和其他分支。除了清除它并重新开始克隆我可以撤消 --single-branch 偏好的其余 repo 之外,还有什么方法吗?

回答by sarahhodne

You can tell Git to pull all branches like this:

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

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

If you look in .git/config, it'll look something like this:

如果你看一下.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/master:refs/remotes/origin/master
[branch "master"]
    remote = origin
    merge = refs/heads/master
    rebase = true

I compared this to a full clone, and saw that the only difference was the "fetch" under [remote "origin"].

我将其与完整克隆进行了比较,发现唯一的区别是[remote "origin"].

Note: I'm running Git version 1.8.2. The config options may have changed if you're running an older version of Git. If my commands don't work, then I'd recommend looking through .git/configto see if you can see something similar.

注意:我正在运行 Git 版本 1.8.2。如果您运行的是旧版本的 Git,配置选项可能已更改。如果我的命令不起作用,那么我建议您仔细.git/config查看是否可以看到类似的内容。

回答by Dominik Pawlak

If you want to add a single branch, you can do the following:

如果要添加单个分支,可以执行以下操作:

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

Works with git version 1.9.1

适用于 git 版本 1.9.1

回答by sjakubowski

Just add the original repo as a new remote, and work off of there?

只需将原始存储库添加为新的遥控器,然后在那里工作?

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

You can even delete your current 'origin' remote and rename 'myNewOrigin' to 'origin' if you would want to.

如果您愿意,您甚至可以删除当前的“origin”遥控器并将“myNewOrigin”重命名为“origin”。

From there you can pull/merge/rebase.

从那里你可以拉/合并/变基。

回答by Arno Fiva

To add another remote branchto my local repository that was cloned using --single-branch, the following works for me:

要将另一个远程分支添加到我使用 克隆的本地存储库,--single-branch以下对我有用:

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

You can also use wildcards for [remote-branch], e.g.

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

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

This works using git version 2.21.1. Other answers suggesting to do git fetch origin [remote-branch]:[local-branch]did not work as it creates the local branch in an untracked state. When running git pullit first tried to merge all the commits of the remote branch to my local one once again.

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

回答by trueboroda

Just change .git/configfile of your local repo, line fetchof [remote origin] section.

只需更改.git/config本地存储库的文件fetch[remote origin] section.

Before something like this

在这样的事情之前

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

After it will be so

以后会这样

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

回答by Houman

I initially applied Dominik Pawlak's answer and it worked. But I wasn't able to pull any further changes, after I committed more code into my new branch.

我最初应用了 Dominik Pawlak 的答案并且它奏效了。但是在我将更多代码提交到我的新分支之后,我无法进行任何进一步的更改。

There is another way of resetting single-branchentirely, which hasn't been mentioned here:

还有另一种single-branch完全重置的方法,这里没有提到:

git remote remove origin
git remote add origin [email protected]:{yourProject}/{yourRepo}.git
git branch --set-upstream-to=origin/{yourBranch}  {yourBranch}
git pull

This resets everything to the original.

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