fetch in git doesn't get all branches

Viewed 217264

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

11 Answers

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 git@github.com:web3coach/the-blockchain-bar-newsletter-edition.git

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

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

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>

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
Related