Git Branches NOT Showing in "git branch" after Pull or Fetch but "git checkout" works ...?

Thom Ives picture Thom Ives · Oct 6, 2017 · Viewed 7.6k times · Source

After committing my latest work on a new branch branchname to my local git repo on machine A, I pushed that work to my gitlab remote repo with

$ git push origin branchname 

The master branch was already in sync with the remote repo. All branches showed up on the remote repo on my gitlab list.

I later went to machine B. I first did a

$ git pull origin master 

and master was updated, but my new branchname didn't show up when I typed git branch, nor did another branch that I new existed in the history tree. I went to this post and followed the directions from the first answer. Another 'git branch' still did not show my other branches. On a whim, I simply tried to do

$ git checkout branchname 

and it was there and checked out fine. A git branch command then showed master and branchname in my list of branches. I did the same thing with another branch, and it too then showed up in the branch list after a git branch command.

Is this normal git behavior for such operations? The main thing I am wondering is if you pull or fetch from a remote repo to update a local repo that had to know previous knowledge of branches on the remote, why don't they show up during a git branch command? And, why can I check them out when I couldn't see them after a git branch?

This saga is similar to THIS one, but my branches were actually there and just NOT showing following git branch commands until I checked them out.

Answer

Sajib Khan picture Sajib Khan · Oct 6, 2017
$ git branch     # only local branches
* master

In machine B, branchname does not exists as local branch before git checkout branchname command, so the list shows only master.

$ git fetch
$ git checkout branchname
$ git branch
  master
* branchname

See all remote and local branches.

$ git branch -a   # remote and local branches
$ git branch -r   # remote branches only

Note: here, git checkout branchname actually finds a local branch named branchname. If found, then just checkout to that branch, but if not found then it searches in remote branch lists (e.g. origin/branchname ). If found, then create a local branch branchname with the same history of origin/branchname.