Remote branch is not showing up in "git branch -r"

I have been pushing to a remote Bitbucket repository and recently a colleague has pushed a new branch he created to the same repository.

I'm trying to fetch the changes he uploaded.

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

$ git branch -r origin/master

In the web UI for Bitbucket I can see the branch he has made. How can I do this?

Next try:

$ git fetch bitbucket
Password for 'https://[email protected]':
From https://bitbucket.org/user/repo
 * branch            HEAD       -> FETCH_HEAD

If the branch he created is called new_branch_b should I be expecting to see the following?

$ git branch -r
origin/master
origin/new_branch_b

Third try:

$ git remote update
Fetching bitbucket
Password for 'https://[email protected]':
From https://bitbucket.org/user/repo
 * branch            HEAD       -> FETCH_HEAD

$ git branch -r
  origin/master

Fourth try:

[remote "bitbucket"]
url = https://[email protected]/user/repo.git

I called the remote bitbucket rather than origin (at least that's what I recall; I set it up a while ago)

Fifth try:

I updated the Bitbucket remote configuration as per kan's answer:

$ git config -e

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

For most people it will be called origin:

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

Afterwards,

$ git remote update

Fetching bitbucket
Password for 'https://[email protected]':
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

.... and so on.

I think git fetch origin would also work for git remote update.


Solution 1:

Update your remote if you still haven't done so:

$ git remote update
$ git branch -r

Solution 2:

The remote section also specifies fetch rules. You could add something like this into it to fetch all branches from the remote:

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

(Or replace origin with bitbucket.)

Please read about it here: 10.5 Git Internals - The Refspec

Solution 3:

If you clone with the --depth parameter, it sets .git/config not to fetch all branches, but only master.

You can simply omit the parameter or update the configuration file from

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

to

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

Solution 4:

I had the same issue. It seems the easiest solution is to just remove the remote, readd it, and fetch.