212

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.

4
  • 1
    Cool, but maybe it had more sense to use refs/remotes/bitbucket/* instead of refs/remotes/origin/*. Commented Sep 7, 2012 at 15:35
  • Thanks, duly noted about the naming consistency. It probably make more sense to change bitbucket to origin though! Convention and all that :) Commented Sep 7, 2012 at 18:36
  • 3
    git fetch origin does the job Commented Aug 28, 2017 at 11:29
  • Maybe try git remote set-branches origin '*' or git remote set-branches origin --add new_branch_b. Commented Jan 16 at 7:56

7 Answers 7

320

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

$ git remote update $ git branch -r 
Sign up to request clarification or add additional context in comments.

5 Comments

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
It didn't help.
I think I had aleady successfully fetched the branch but just needed to see it listed with git branch -r. On 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.
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
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.
168

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

9 Comments

This fixed my ongoing branching issue in Git that I had for weeks. Suddenly all the git remote update stuff actually started working. Thanks!
For some reason, it looked like: fetch = +refs/heads/master:refs/remotes/origin/master for me. Replacing master with * fixed my problem.
The file you need to edit is .git/config
This is very usefull if you have converted a shallow clone to an unshallow.
@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.
|
123

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/* 

3 Comments

you can edit the configuration with git config -e
This is critical if you have cloned with --depth= specified, thanks
This happened to me after cloning with --depth=1, thank you!
38

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

5 Comments

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 <url> will re-add it.
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.
worked for me, but now I get a warning warning: ignoring broken ref refs/remotes/origin/HEAD
It's frustrating how often with git config issues the best solution is to delete something and start over.
I also needed this command: git branch --set-upstream-to=origin/main main. Otherwise I was getting "There is no tracking information for the current branch. Please specify which branch you want to merge with."
19

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.

Comments

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

Comments

0

If you are using a git repo that was checked-out using Bamboo, the URL that Bamboo uses is not the same as the original repo. After doing every single suggestion in this question, I did git config -e and found the URL for [remote "origin"] and fixed the url setting so that it matched the URL I have been using in my personal workspace.

Before:

url = file:///home/agent/bamboo-agent-home/xml-data/build-dir/_git-repositories-cache/71efa04b5e7c457ab86f97a26aa286d9258556fb 

After:

url = http://bitbucket.mycompany.com:7990/scm/seq/myrepo.git 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.