Question

How do I fetch back all the branches for my repository on Git? I tried the following:

git remote add origin git@github.com:anchetaWern/Zenoir-Online-Classroom.git
git pull git@github.com:anchetaWern/Zenoir-Online-Classroom.git

I have 3 branches in that repository but now I only have the master branch. How do I pull back the other 2?

Was it helpful?

Solution

After you do the 'git remote add origin the-repo' just perform a 'git fetch -a origin' at which point all the branches are there and ready to be checked out. Here is a typical workflow:

$ git init
Initialized empty Git repository in /Users/ebg/test/dev5/.git/
$ git remote add origin  /Users/ebg/test/dev1
$ git fetch -a origin
remote: Counting objects: 41, done.
remote: Compressing objects: 100% (30/30), done.
remote: Total 41 (delta 15), reused 0 (delta 0)
Unpacking objects: 100% (41/41), done.
From /Users/ebg/test/dev1
 * [new branch]      add-on     -> origin/add-on
 * [new branch]      master     -> origin/master
$ git checkout add-on
Branch add-on set up to track remote branch add-on from origin.
Switched to a new branch 'add-on'

So, for this example, branch 'add-on' is now in the working directory and 'git checkout master' will get files from the remote 'origin' if needed.

OTHER TIPS

After adding the remote origin, you don't have to refer to the url again. That is the point of adding the remote.

You could have done git pull origin

Now, if you do git branch -a you will see the remote branches ( the 2 that are "missing"). By default only the master is checked out as a local branch.

Just do git checkout branch_name to check them out and setup a local branch ( once each)

When I work with branches, this is my typical workflow:

  1. push branch to github

    git push origin newbranch
    
  2. pull branches on github to another computer

    git fetch origin
    
  3. work on branch pulled from github

    git checkout -b newbranch origin/newbranch
    

To checkout myBranch that exists remotely and not a locally - This worked for me:

git fetch --all
git checkout <BranchName>

Another solution:

I have used fetch followed by checkout ...

git fetch <remote> <rbranch>:<lbranch> 
git checkout <lbranch>

... where is the remote branch or source ref and is the as yet non-existent local branch or destination ref you want to track and which you probably want to name the same as the remote branch or source ref.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top