Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 604
Thom Ives Avatar asked Oct 06 '17 03:10

Thom Ives


2 Answers

git branch, without any parameters, only shows your local branches. When you fetch, information about your remote branches is updated, but it will only be shown when you use git branch -r (only remote branches) or, as @SajibKhan suggested, git branch -a (all remote and local branches).

So yes, this is intended behaviour. You can check them out since git recognizes the remote branch and sets up a new local branch that automatically is set up to track the remote branch.

like image 29
kowsky Avatar answered Sep 27 '22 16:09

kowsky


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

like image 177
Sajib Khan Avatar answered Sep 27 '22 17:09

Sajib Khan