Git is not showing all branches on local

xan picture xan · Mar 30, 2013 · Viewed 37.4k times · Source

I forked a repo from Github. On doing git remote -v it displays:

origin  https://github.com/myusername/moodle.git (fetch)
origin  https://github.com/myusername/moodle.git (push)
upstream    https://github.com/moodle/moodle.git (fetch)
upstream    https://github.com/moodle/moodle.git (push)

The moodle.git has about 10 branches, but the repo shows only 2 of them. On doing git branch -a (show all branches) I get:

  MOODLE_24_STABLE// just these two on local..how?
* master//
  origin/MOODLE_13_STABLE
  origin/MOODLE_14_STABLE
  origin/MOODLE_15_STABLE
  origin/MOODLE_16_STABLE
  origin/MOODLE_17_STABLE
  origin/MOODLE_18_STABLE
  origin/MOODLE_19_STABLE
  origin/MOODLE_20_STABLE
  origin/MOODLE_21_STABLE
  origin/MOODLE_22_STABLE
  origin/MOODLE_23_STABLE
  origin/MOODLE_24_STABLE
  origin/master
  upstream/MOODLE_13_STABLE
  upstream/MOODLE_14_STABLE
  upstream/MOODLE_15_STABLE
  upstream/MOODLE_16_STABLE
  upstream/MOODLE_17_STABLE
  upstream/MOODLE_18_STABLE
  upstream/MOODLE_19_STABLE
  upstream/MOODLE_20_STABLE
  upstream/MOODLE_21_STABLE
  upstream/MOODLE_22_STABLE
  upstream/MOODLE_23_STABLE
  upstream/MOODLE_24_STABLE
  upstream/master

How do I resolve my problem without any loss of data or any irregularities?

Answer

VonC picture VonC · Mar 30, 2013

Cloning a repo won't duplicate all the remote branches on the local repo: for a large remote repo with a lot of branches, that would pollute your local namespace with tons of branches.

I have a one-liner command in order to create local branches tracking all the remote branches of a remote repo, but this is usually not needed.
You only create a local branch tracking a remote one when needed.

git checkout -b aBranch --track origin/aBranch

# or, shorter:
$ git checkout --track origin/aBranch 
Branch aBranch set up to track remote branch refs/remotes/origin/aBranch.
Switched to a new branch "aBranch"  

# even shorter at the end of this answer.

Adding a --track allows for setting up the configuration to mark the start-point branch as "upstream" from the new branch.
This configuration will tell git to show the relationship between the two branches in git status and git branch -v.
Furthermore, it directs git pull without arguments to pull from the upstream when the new branch is checked out.


kostix mentions that --track is implied when forking a branch off a remote branch (unless branch.autosetupmerge is set to false)

This could be enough

git checkout aBranch

The exact explanation from git checkout man page is:

If <branch> is not found but there does exist a tracking branch in exactly one remote (call it <remote>) with a matching name, treat as equivalent to:

$ git checkout -b <branch> --track <remote>/<branch