git fetch origin doesn't fetch all branches
Solution 1
You have all 7 branches, but git branch
only shows local branches. Even though you now have the branch data locally on your system, they are still considered "remote branches". You can see them with git branch -a
. They'll be called something like remotes/origin/branchname
. You can check them out by specifying this full name: git checkout remotes/origin/branchname
.
Also, you already got all of these branches when you cloned the repository. Running git fetch origin
will simply update your repository with anything new that happened on origin since you last fetched (or cloned).
You can read more about remote branches in the git documentation: Git Branching - Remote Branches
Solution 2
Although this question was answered by the question asker, here is the real deal: the reason you are getting confused is because git fetch
will show you the gory details of what it does on a new remote, but it will not show you anything (git clone) when cloning the repo at first. So cloning only creates the local master branch. Because it checks it out, by default. Checking it out creates it locally.
So basically what you need to do is what you indicated in the beginning: check out all branches individually. So basically what you get is this:
git branch -a | grep origin | sed "s@.*origin/@@" | while read f; do
git checkout $f
done
There are probably more low-level "plumbing" commands that can do it, but basically this is it from a user perspective. Actually, the answer seems to be that you can create local branches directly.
git branch -a | grep origin | sed "s@.*origin/@@" | while read f; do
git branch $f origin/$f
done
But this won't set up tracking branches, probably. Answer from How to clone all remote branches in Git?
Cerran
Updated on July 11, 2022Comments
-
Cerran almost 2 years
I read in the answers to this question that
git fetch origin
should fetch all branches of origin. In my case, it doesn't seem to give me any branches, though. Here's what I did:Initially, a remote called origin had 7 branches. I cloned it.
git branch
then returned onlymaster
. I didgit fetch origin
, andgit branch
still only showsmaster
. How can I get the other 6 branches without fetching them individually? -
fIwJlxSzApHEZIl about 6 yearsI was able to check out the branch but the code was not the most up-to-date. Using any number of pull or fetch commands was not updating it either. I am trying to update a PR I started on another computer so I checked out the branch
git checkout origin/finalize
but it says I'm in adetached HEAD
state and the code that I did check out is not up to date. I know the code is not up to date because it doesn't reflect the changes I made in the PR. I'm essentially trying to pick up work I did at home when I got back into work. Same repo, same user, just different clones of the same repo -
handles almost 6 yearsThe thing I don't understand is why during a clone, git doesn't just clone all the .git/ref/heads too. I.e. all the .git/object files are already there. In fact I tried just copying the .git/refs/heads files into a new cloned repo and git would list the branches with just git branch (no -a).