如何检出远程分支

how to checkout a remote branch

我可以在我的本地存储库中看到两个远程分支:

$ git branch -a
  remote/origin/master
  remote/origin/feature1

我的远程存储库中实际上有四个分支。如何将第 3 或第 4 家分行结帐到我的本地?

我通过搜索互联网尝试了很多命令,none 其中对我有用。 不过,从远程到本地获取 master 和 feature1 分支的更新没有问题。

您的本地存储库可能已过时。追赶,运行

git fetch

现在您应该看到其他分支。如果其中一个分支名为 feature2,则 运行ning

git checkout -b feature2 origin/feature2

或者,最近 git,只需

git checkout feature2

将检查分支并设置跟踪以防您与其他开发人员协作。

好的, 我在本地 git 存储库

config 文件中有以下内容
[remote "origin"]
    url = <url>
    fetch = +refs/heads/feature1:refs/remotes/origin/feature1
    fetch = +refs/heads/master:refs/remotes/origin/master
[branch "master"]
    remote = origin
    merge = refs/heads/master
[branch "feature1"]
    remote = origin
    merge = refs/heads/feature1
    rebase = false

将远程分支 feature2 添加到 [remote "origin"] 部分后,

[remote "origin"]
    url = <url>
    fetch = +refs/heads/feature1:refs/remotes/origin/feature1
    fetch = +refs/heads/master:refs/remotes/origin/master
    fetch = +refs/heads/feature2:refs/remotes/origin/feature2

然后运行

git fetch --all
git branch -a

我在输出中看到远程 feature2 分支。 我现在可以结账 feature2

还有一个问题,我应该如何将本地 git 仓库的分支信息与远程仓库同步,​​而无需手动修改 config 文件?

这个:

[remote "origin"]
    url = <url>
    fetch = +refs/heads/feature1:refs/remotes/origin/feature1
    fetch = +refs/heads/master:refs/remotes/origin/master

是问题的根源。在这里,您已指示 您的 Git 无论 origin 上的 Git 中存在什么分支,您都希望 您的 Git 取并记住 只有 feature1master,您将调用 origin/feature1origin/master.

origin 的默认标准 fetch 设置,如果您不更改它,则为:

fetch = +refs/heads/*:refs/remotes/origin/*

如果你有这个设置,一切都会自动运行。

(问题就变成了:当您似乎想要 行为 提供的 行为 时,为什么您停止使用标准的默认设置标准的默认设置?Edit: 显然答案是:你没有,Eclipse / EGit 做到了。)