Edward Newell
Edward Newell

Reputation: 18549

fetch in git doesn't get all branches

I have cloned a repository, after which somebody else has created a new branch, which I'd like to start working on. I read the manual, and it seems dead straight easy. Strangely it's not working, and all the posts I've found suggest I'm doing the right thing. So I'll subject myself to the lambasting, because there must be something obviously wrong with this:

The correct action seems to be

git fetch
git branch -a
* master
  remotes/origin/HEAD --> origin/master
  remotes/origin/master
git checkout -b dev-gml origin/dev-gml

At this point there is a problem, for some reason after git fetch I can't see the dev-gml remote branch. Why not? If I clone the repository freshly, it's there, so certainly the remote branch exists:

$ mkdir ../gitest
$ cd ../gitest
$ git clone https://github.com/example/proj.git
Cloning into proj...
remote: Counting objects: 1155, done.
remote: Compressing objects: 100% (383/383), done.
remote: Total 1155 (delta 741), reused 1155 (delta 741)
Receiving objects: 100% (1155/1155), 477.22 KiB | 877 KiB/s, done.
Resolving deltas: 100% (741/741), done.
$ cd projdir
$ git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/dev-gml
  remotes/origin/master

I've tried git update, git pull, git fetch --all, git pretty-please in all possible permutations...

Upvotes: 415

Views: 311956

Answers (14)

Samet ÖZTOPRAK
Samet ÖZTOPRAK

Reputation: 3374

write it from the terminal

git fetch --prune

it works fine.

Upvotes: 9

Malak
Malak

Reputation: 460

A possible solution:

git remote update --prune

It synchronizes your local repository with remotes. The prune option removes references to deleted remote branches.

Upvotes: 2

AndASM
AndASM

Reputation: 10428

The problem can be seen when checking the remote.origin.fetch setting
(The lines starting with $ are bash prompts with the commands I typed. The other lines are the resulting output)

$ git config --get remote.origin.fetch
+refs/heads/master:refs/remotes/origin/master

As you can see, in my case, the remote was set to fetch the master branch specifically and only. I fixed it as per below, including the second command to check the results.

$ git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
$ git config --get remote.origin.fetch
+refs/heads/*:refs/remotes/origin/*

The wildcard * of course means everything under that path.

Unfortunately I saw this comment after I had already dug through and found the answer by trial and error.

Edit: It seems this happens if you do a shallow clone - e.g. git clone --depth 1 https://github.com/SomeRepo would cause this problem.

Adding from the comments

Note that this can happen if you have cloned your repository with a single branch only, e.g. git clone --branch --single-branch [] – Narretz

A shallow clone with git clone --depth implies --single-branch, as noted in the man page git-clone(1), so we'd better do it with git clone --depth --no-single-branch . – whatacold

Upvotes: 785

weaming
weaming

Reputation: 6525

My issue: fetch and checkout tell me not found, re-clone works.

git config --get remote.origin.fetch output:

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

Seems no problem.

Because the commit I try to checkout is on a tag but not a branch, I try to git fetch --tags, then checkout the commit, it works finally!

See https://stackoverflow.com/a/14946840/5281824 to configure git to fetch all branches and tags.

Upvotes: 1

philipvr
philipvr

Reputation: 6388

Remote update

You need to run

git remote update

or

git remote update <remote> 

Then you can run git branch -r to list the remote branches.

Checkout a new branch

To track a (new) remote branch as a local branch:

git checkout -b <local branch> <remote>/<remote branch>

or (sometimes it doesn't work without the extra remotes/):

git checkout -b <local branch> remotes/<remote>/<remote branch>

Helpful git cheatsheets

Upvotes: 123

ninpnin
ninpnin

Reputation: 329

I had cloned the repo with --depth 1, so these answers weren't working. What did work for me was

git fetch origin BRANCHNAME:BRANCHNAME

It succesfully created a local branch with the same name.

Upvotes: 10

Deb
Deb

Reputation: 663

All you need to do is, apply the following 2 commands:

git fetch --all

And once you see the branch (which was not visible before e.g. osc_at_works), select that and checkout as below:

git checkout origin/team/Enterprise/osc_at_works

Upvotes: -2

Craig  Hicks
Craig Hicks

Reputation: 2558

git checkout --track origin/formats seemed to do the trick:

% git branch      ### show local branches
* main

% git branch - a  ### show local and remote branches
* main
  remotes/origin/HEAD -> origin/main
  remote/origin/formats
  remote/origin/main

% git checkout --track origin/formats
Switched to a new branch 'formats'
Branch 'formats' set up to track remote branch 'formats' from 'origin'

% git branch
* formats
  main

The following should do the same but with different local branch name:

git checkout -b my-formats origin/formats 

A new syntax git switch is available in git c2.23

git switch -c <branch> --track <remote>/<branch>

Upvotes: 5

Lukas Lukac
Lukas Lukac

Reputation: 8376

Had the same problem today setting up my repo from scratch. I tried everything, nothing worked except removing the origin and re-adding it back again.

git remote rm origin
git remote add origin [email protected]:web3coach/the-blockchain-bar-newsletter-edition.git

git fetch --all
// Ta daaa all branches fetched

Upvotes: 38

Juh_
Juh_

Reputation: 15569

I had a similar problem, however in my case I could pull/push to the remote branch but git status didn't show the local branch state w.r.t the remote ones.

Also, in my case git config --get remote.origin.fetch didn't return anything

The problem is that there was a typo in the .git/config file in the fetch line of the respective remote block. Probably something I added by mistake previously (sometimes I directly look at this file, or even edit it)

So, check if your remote entry in the .git/config file is correct, e.g.:

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

Upvotes: 6

stux
stux

Reputation: 3016

I had this issue today on a repo.

It wasn't the +refs/heads/*:refs/remotes/origin/* issue as per top solution.

Symptom was simply that git fetch origin or git fetch just didn't appear to do anything, although there were remote branches to fetch.

After trying lots of things, I removed the origin remote, and recreated it. That seems to have fixed it. Don't know why.

remove with: git remote rm origin

and recreate with: git remote add origin <git uri>

Upvotes: 280

Swapna
Swapna

Reputation: 401

To make it more specific Create a tracking branch, which means you are now tracking a remote branch.

git branch --track branch remote-branch
git branch --track exp remotes/origin/experimental

After which you can

git branch   # to see the remote tracking branch "exp" created .

Then to work on that branch do

git checkout branchname
git checkout exp

After you have made changes to the branch. You can git fetch and git merge with your remote tracking branch to merge your changes and push to the remote branch as below.

git fetch origin
git merge origin/experimental  
git push origin/experimental

Hope it helps and gives you an idea, how this works.

Upvotes: 3

Maslow
Maslow

Reputation: 18746

I had to go into my GitExtensions Remote Repositories as nothing here seemed to be working. There I saw that 2 branches had no remote repository configured. after adjusting it looks as followsenter image description here

Notice branch noExternal3 still shows as not having a remote repository. Not sure what combo of bash commands would have found or adjusted that.

Upvotes: 0

jerseyboy
jerseyboy

Reputation: 1326

This could be due to a face palm moment: if you switch between several clones it is easy to find yourself in the wrong source tree trying to pull a non-existent branch. It is easier when the clones have similar names, or the repos are distinct clones for the same project from each of multiple contributors. A new git clone would obviously seem to solve that "problem" when the real problem is losing focus or working context or both.

Upvotes: 1

Related Questions