20

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?

xan
  • 4,640
  • 13
  • 50
  • 83
  • 3
    Why would you expect more local branches? After cloning a repository, there's only one local master branch. Other branches are created on demand. – knittl Mar 30 '13 at 18:43
  • @knittl: Okay. I didn't know that. Would you please instruct me how to do it? I've created a branch say `git checkout -b STABLE_23_STABLE` but the files are not those of that branch. – xan Mar 30 '13 at 18:46
  • 1
    `git checkout -b MOODLE_23_STABLE` creates a new branch off of the current commit, so you will still see the same commits. Either `git checkout -b MOODLE_23_STABLE upstream/MOODLE_23_STABLE` or `git checkout MOODLE_23_STABLE` (shorthand/DWIM syntax) – knittl Mar 30 '13 at 18:48
  • Duplicate of [Git branch not showing](http://stackoverflow.com/questions/8889753/git-branch-not-showing) –  May 29 '14 at 20:35

3 Answers3

24

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
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • `--track` is implied when forking a branch off a remote branch (unless `branch.autosetupmerge` is not set to `false`). I would also advise @xan, to take their time and read some [introductory material on remote branches](http://git-scm.com/book/en/Git-Branching-Remote-Branches) to not get lost later. – kostix Mar 31 '13 at 14:47
  • @kostix good point. I have added that precision to the answer for more visibility. – VonC Mar 31 '13 at 15:54
  • 1
    `git checkout --track origin/aBranch ` this solved the issue for me, thanks – StealthTrails Oct 17 '16 at 18:12
6

Some times , if you havn't pulled the latest code, you wont be allowed to checkout the newly created branch.Because your changes are not in synch.

So first -pull the latest -checkout from newly created branch

Grace Aloysius
  • 131
  • 2
  • 2
0

if your starting to initialise a repository : 1 - git reset 2 - delete .git reposytory 3 - git init 4 - You must commit a first commit