如何解决远程分支未显示在“git branch -r”中?

remote部分还指定了获取规则。您可以在其中添加类似的内容以从远程获取所有分支:

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

(或替换originbitbucket。)

请在此处阅读: 10.5 Git Internals - The Refspec

解决方法

我一直在推送到远程 Bitbucket 存储库,最近一位同事将他创建的新分支推送到了同一个存储库。

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

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

$ git 分支 -r 原点/主

在 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而不是起源(至少这是我记得的;我前一阵子设置的)

第五次尝试:

我根据kan 的回答更新了 Bitbucket 远程配置:

$ git配置-e

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

对于大多数人来说,它将被称为起源:

[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.