远程分支在“git branch -r”中未显示。

203

我一直在将代码推送到远程的Bitbucket仓库,最近一位同事也向这个仓库推送了一个新分支。

我正在尝试获取他上传的更改。

 $ git branch -a
 * master
 localbranch1
 localbranch2
 remotes/origin/master

$ git branch -r origin/master

在 Bitbucket 的 web UI 中我可以看到他创建的分支,我该如何做到这一点?

接下来尝试:

$ git fetch bitbucket
Password for 'https://xxxxx@bitbucket.org':
From https://bitbucket.org/user/repo
 * branch            HEAD       -> FETCH_HEAD

如果他创建的分支叫做new_branch_b,那么我应该期待看到以下内容吗?

$ git branch -r
origin/master
origin/new_branch_b

第三次尝试:

$ git remote update
Fetching bitbucket
Password for 'https://xxxxx@bitbucket.org':
From https://bitbucket.org/user/repo
 * branch            HEAD       -> FETCH_HEAD

$ git branch -r
  origin/master

第四次尝试:

[remote "bitbucket"]
url = https://user@bitbucket.org/user/repo.git

我调用了远程的bitbucket而不是origin(至少我记得是这样; 我设置它已经有一段时间了)

第五次尝试:

我按照kan的答案更新了Bitbucket远程配置:

$ git config -e

[remote "bitbucket"]
    url = https://user@bitbucket.org/user/repo.git
    fetch = +refs/heads/*:refs/remotes/bitbucket/*

对于大多数人来说,它将被称为"origin":

[remote "origin"]
    url = https://user@bitbucket.org/user/repo.git
    fetch = +refs/heads/*:refs/remotes/origin/*

之后,

$ git remote update

Fetching bitbucket
Password for 'https://user@bitbucket.org':
remote: Counting objects: 48, done.
remote: Compressing objects: 100% (32/32), done.
remote: Total 35 (delta 21), reused 0 (delta 0)
Unpacking objects: 100% (35/35), done.
From https://bitbucket.org/user/repo
 * [new branch]      branch_name1 -> origin/branch_name1
 * [new branch]      branch_name2    -> origin/branch_name2

我认为 git fetch origin 也可以用于 git remote update


1
很酷,但也许使用 refs/remotes/bitbucket/* 而不是 refs/remotes/origin/* 更有意义。 - kan
谢谢,已经注意到命名一致性的问题了。不过把Bitbucket改成origin可能更合理些!毕竟要遵循惯例 :) - feargal
2
git fetch origin 完成了这项工作。 - coretechie
7个回答

308

如果您仍未更新远程,请进行更新:

$ git remote update
$ git branch -r

2
我正在使用 Windows 上的 GitHub 客户端,有时它无法更新远程分支。第一行命令 "git remote update" 的效果非常好。简单又干净。 - Stefano Buora
9
没什么帮助。 - dKab
我认为我已经成功地获取了分支,只需要用git branch -r列出它。 在https://git-scm.com/docs/git-branch上说:“选项-r会导致列出远程跟踪分支,选项-a显示本地和远程分支”。 git remote update获取在git remote -v中列出的所有远程的所有分支。 - Rock Lee
运行后,我能够运行git checkout origin/master,然后从那个分离的头部分支到我的主分支git branch master; git checkout master - Dfranc3373
1
对我来说没问题。似乎Visual Studio应该在分支UI中的远程/本地文件夹上提供此右键单击选项。 - Kirk Kuykendall

161

remote 部分还指定了获取规则。您可以将以下内容添加到其中,以从远程获取所有分支:

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

(或将origin替换为bitbucket。)

请在此处阅读详情:10.5 Git 内部原理 - Remote 引用规范


3
这解决了我在 Git 中遇到的长达数周的分支问题。突然之间所有的 git remote update 都真正开始工作了。谢谢! - PålOliver
5
由于某些原因,对我而言它看起来像是:fetch = +refs/heads/master:refs/remotes/origin/master。将master替换为*解决了我的问题。 - Sebastian Blask
6
你需要编辑的文件是 .git/config - Mateus Gondim
2
如果您将一个浅克隆转换为非浅克隆,这将非常有用。 - Warpzit
@dotnetCarpenter 不确定...我从未遇到过这种意外情况。clone被中断然后恢复是可能的吗?或者其他东西同时访问了仓库? - kan
显示剩余4条评论

102
如果您使用--depth参数进行克隆,它会将.git/config设置为仅获取主分支,而不是所有分支。您可以简单地省略该参数或从配置文件中更新它。
fetch = +refs/heads/master:refs/remotes/origin/master

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

4
您可以使用 git config -e 命令编辑配置。 - David

35

我遇到了同样的问题。看起来最简单的解决方法就是删除远程仓库,然后重新添加并拉取。


8
git remote -v 命令可以展示你的远程仓库,以便获取 url。git remote rm origin 命令会移除远程仓库,git remote add origin <url> 命令则可以重新添加它。 - Siddhartha
这对我有用。我有一个git子模块,它不知何故没有拉取除主分支以外的任何远程分支。删除并重新添加解决了这个问题。 - Zamrony P. Juhara
之前代码对我来说正常运行,但现在出现警告 warning: ignoring broken ref refs/remotes/origin/HEAD - dafnahaktana
令人沮丧的是,在处理git配置问题时,最好的解决方案经常是删除某些内容并重新开始。 - Jasha

18

很遗憾,如果您没有执行“git fetch”,git branch -agit branch -r不会显示所有远程分支。

git remote show origin在任何时候都有效。另外git show-ref可以显示Git存储库中的所有引用。然而,它的工作方式与git branch命令类似。


0
如果您在更新服务器云计算平台或类似的事情上遇到困难,以下是解决方案:
修改git配置文件`/path/your_project/.git/config`,将以下内容: ``` [remote "origin"] url = https://github.com/company_name/your_project.git fetch = +refs/heads/*:refs/remotes/origin/* ``` 修改为: ``` [remote "origin"] url = git@github.com:company_name/your_project.git fetch = +refs/heads/*:refs/remotes/origin/* ```
保存更改。
然后运行`git remote update`。
检查远程分支`git branch -r`。
切换到分支`git checkout origin/branch-name`。

0

你也可以直接写 git checkout BRANCH_NAME,这会在本地创建一个分支并将其链接到远程分支。

你应该会看到以下提示信息: 分支 'BRANCH_NAME' 已设置为跟踪来自 'origin' 的远程分支 'BRANCH_NAME'。


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