202

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://xxxxx@bitbucket.org':
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://xxxxx@bitbucket.org':
From https://bitbucket.org/user/repo
 * branch            HEAD       -> FETCH_HEAD

$ git branch -r
  origin/master

Fourth try:

[remote "bitbucket"]
url = https://user@bitbucket.org/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://user@bitbucket.org/user/repo.git
    fetch = +refs/heads/*:refs/remotes/bitbucket/*

For most people it will be called origin:

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

Afterwards,

$ 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

.... and so on.

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
feargal
  • 2,655
  • 2
  • 22
  • 27

6 Answers6

305

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

$ git remote update
$ git branch -r
B B
  • 3,922
  • 2
  • 18
  • 13
  • 2
    I'm using the GitHub client on Win and sometimes it doesn't update the remote branches. The first line "git remote update" works like a charm. Easy & clean – Stefano Buora Aug 09 '16 at 13:42
  • 8
    It didn't help. – dKab Mar 20 '17 at 15:31
  • I think I had aleady successfully fetched the branch but just needed to see it listed with `git branch -r`. On https://git-scm.com/docs/git-branch it says "Option -r causes the remote-tracking branches to be listed, and option -a shows both local and remote branches". `git remote update` fetches all branches of all remotes listed in `git remote -v`. – Rock Lee Jun 04 '18 at 22:21
  • After running this I was able to run ```git checkout origin/master``` and then branched off of that detached head to my master branch ```git branch master; git checkout master``` – Dfranc3373 Jun 20 '18 at 17:40
  • 1
    Works for me. Sure seems like Visual Studio should have this as a right click option on the remotes/origin folder in the Branches UI. – Kirk Kuykendall Apr 15 '19 at 20:15
160

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
kan
  • 28,279
  • 7
  • 71
  • 101
  • 3
    This fixed my ongoing branching issue in Git that I had for weeks. Suddenly all the git remote update stuff actually started working. Thanks! – PålOliver Jul 05 '13 at 09:28
  • 5
    For some reason, it looked like: `fetch = +refs/heads/master:refs/remotes/origin/master` for me. Replacing `master` with `*` fixed my problem. – Sebastian Blask Oct 07 '15 at 10:07
  • 6
    The file you need to edit is `.git/config` – Mateus Gondim Sep 16 '16 at 12:38
  • 2
    This is very usefull if you have converted a shallow clone to an unshallow. – Warpzit Oct 03 '16 at 13:29
  • @kan Do you know why this sometimes happens? It just happen to me when `git clone` a project. I don't recall having done anything special with my local git. – dotnetCarpenter Feb 22 '17 at 22:51
  • @dotnetCarpenter Not sure... never happened to me unexpectedly. Is it possible that `clone` was interrupted and then resumed? Or something else has accesed repo concurrently? – kan Feb 23 '17 at 18:34
  • @kan it's possible that the connection was interrupted. – dotnetCarpenter Feb 24 '17 at 14:52
  • @dotnetCarpenter in my case, I think it was due to cloning with --single-branch maybe? Still odd that fetch mechanics were modified... that's never happened when I use --single-branch clones in the past. – CCJ Sep 18 '19 at 19:59
  • @CCJ I always use git from the command line and I have never used `--single-branch`. – dotnetCarpenter Sep 20 '19 at 13:58
96

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/*
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
DonPaulie
  • 2,004
  • 17
  • 26
35

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
jessicah
  • 945
  • 10
  • 15
  • 8
    ```git remote -v``` will show you your remotes so you can get hold of the url, ```git remote rm origin``` will remove it, ```git remote add origin ``` will re-add it. – Siddhartha Jan 09 '18 at 22:57
  • This worked for me. I had a git submodule which somehow, did not fetch any remote branch than master. Removing and adding it again solved it. – Zamrony P. Juhara Aug 13 '19 at 21:33
  • worked for me, but now I get a warning `warning: ignoring broken ref refs/remotes/origin/HEAD` – dafnahaktana Jul 12 '20 at 07:24
  • It's frustrating how often with git config issues the best solution is to delete something and start over. – Jasha Oct 21 '21 at 04:39
17

Unfortunately, git branch -a and git branch -r do not show you all remote branches, if you haven't executed a "git fetch".

git remote show origin works consistently all the time. Also git show-ref shows all references in the Git repository. However, it works just like the git branch command.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Thushan
  • 1,220
  • 14
  • 14
0

you could also just write git checkout BRANCH_NAME and it will create one locally and link it to the one on the origin

you should get this message prompted: Branch 'BRANCH_NAME' set up to track remote branch 'BRANCH_NAME' from 'origin'.

anthony422
  • 52
  • 4