我使用以下命令克隆了一个代码库:
git clone -b <分支名称> --single-branch <github网址> <目标目录>
这样只克隆了一个分支,但是现在我想要切换到主分支和其他分支。除了清空并重新开始克隆整个代码库之外,还有没有其他方法可以取消--single-branch选项呢?
您可以这样告诉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
,以查看是否有类似的内容。
如果你想添加一个单独的分支,可以按照以下步骤进行:
git remote set-branches --add origin [remote-branch]
git fetch origin [remote-branch]:[local-branch]
支持 git 版本 1.9.1
fetch =
行,在您想要在远程两个分支之间切换但不想获取一大堆其他分支时非常方便。谢谢! - Terry Brown如果我使用了--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
命令时会再次尝试将远程分支的所有提交合并到我的本地分支。
对于我而言,以下方法可行:
git remote remove origin
git remote add origin https://*<yourPath>*.git
git fetch
git config
的最简单解决方案。 - Prasanna Venkatesh只需将原始存储库添加为新的远程存储库,并从那里工作即可?
git remote add path/to/myrepo myNewOrigin
git fetch myNewOrigin
我最初尝试了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
这将所有内容重置为原始状态。
只需更改本地存储库的.git/config
文件,即[remote origin] section
的fetch
行。
在此之前,类似于这样的内容
[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/*
git
命令行工具非常强大(事实上,大多数命令都是基于其他命令实现的),因此一旦您了解存储库的布局(基本上是.git
文件夹的工作原理),就可以使用它来完成许多任务。 - sarahhodnegit show-ref tags
仍然失败。 - felixfbeckergit clone --depth=1 --branch branchname git@github.com:foobar/repo.git destinationpath
。 - danemacmillangit remote set-branches --add origin '*'
然后git fetch
。 - Vladimir Still