Git: 如何切换到新的远程分支?

6
我正在尝试检出一个本地不存在的远程分支。
git checkout -b branch_name origin/branch_name

提供:

fatal: Cannot update paths and switch to branch 'branch_name' at the same time.
Did you intend to checkout 'origin/branch_name' which can not be resolved as commit?

git branch -a 命令没有显示我想要切换的分支。

我该如何在本地检出远程分支?


可能是检查远程Git分支的重复问题。 - nwinkler
我确实尝试了那个问题中的解决方案,但它没有起作用。 - MeesterPatat
在尝试检出分支之前,请确保先执行git fetch - nwinkler
@zoe 为什么我的主页突然变成了你编辑的问题? - hobbs
@hobbs正在清理[tag:checkout]标签。 - Zoe stands with Ukraine
2个回答

12

试一下

git remote update
git fetch
git checkout -b branch_name origin/branch_name
您本地的代码库不知道远程分支的存在。

我尝试了但没有成功。检出仍然会给出相同的错误信息(致命错误:无法同时更新路径并切换到分支“branch_name”)。 - MeesterPatat
前两个命令可以正常工作,但是在git 2.12上执行git checkout -b branch_name origin/branch_name会出现相同的错误提示:“fatal: Cannot update paths and switch to branch 'branch_name' at the same time.” - SteveExdia

2
如果 git branch -a 没有显示您想要的分支,那么它在远程上也不存在 - 'origin/branch_name' which can not be resolved 信息确认了这一点。
首先,运行 git fetch origin 来同步您本地的远程快照,并查看远程分支是否出现在 git branch -a 中。在这种情况下,您当前的命令应该可以使用,或者您可以在Checkout remote Git branch中找到其他版本。
如果远程分支没有出现,则需要使用以下命令创建它:
git checkout -b branch_name
git push -u origin branch_name

你可能还想检查一下git remote -v,确保你的远程存在并且叫做origin

远程分支确实存在。git branch -a 显示: develop
  • CurrentBranch remotes/origin/HEAD -> origin/develop remotes/origin/develop
我尝试了 git fetch origin,但没有任何变化。git remote -v 显示: origin https://github.com/reponame/reponame.git (fetch) origin https://github.com/reponame/reponame.git (push)
- MeesterPatat
看起来 origin 唯一知道的分支是 develop。你想要检出哪个分支,才会看到 origin 有它的信息? - Kristján
它确实存在于同一个用户/仓库中。我在笔记本电脑上尝试了git branch -a命令,develop2确实出现了。所以这只是在我的当前电脑上出现的问题。 - MeesterPatat
2
听起来你第一台电脑的副本可能出了点问题,这会很难进一步调试。我建议尝试使用 git fetch --all 命令,并比较你的机器之间的 .git/config 文件,然后考虑重新进行 git clone - Kristján
我进行了全新的Git克隆,现在可以进行检出。 - MeesterPatat
显示剩余2条评论

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