292

I have a project hosted on GitHub. I created a branch on one computer, then pushed my changes to GitHub with:

git push origin branch-name

Now I am on a different computer, and I want to download that branch. So I tried:

git pull origin branch-name

...but all this did was overwrite my master branch with the changes in my new branch.

What do I need to do to properly pull my remote branch, without overwriting existing branches?

Johwhite
  • 323
  • 4
  • 18
Andrew
  • 227,796
  • 193
  • 515
  • 708

11 Answers11

517

Thanks to a related question, I found out that I need to "checkout" the remote branch as a new local branch, and specify a new local branch name.

git checkout -b newlocalbranchname origin/branch-name

Or you can do:

git checkout -t origin/branch-name

The latter will create a branch that is also set to track the remote branch.


Update: It's been 5 years since I originally posted this question. I've learned a lot and git has improved since then. My usual workflow is a little different now.

If I want to fetch the remote branches, I simply run:

git pull

This will fetch all of the remote branches and merge the current branch. It will display an output that looks something like this:

From github.com:andrewhavens/example-project
   dbd07ad..4316d29  master     -> origin/master
 * [new branch]      production -> origin/production
 * [new branch]      my-bugfix-branch -> origin/my-bugfix-branch
First, rewinding head to replay your work on top of it...
Fast-forwarded master to 4316d296c55ac2e13992a22161fc327944bcf5b8.

Now git knows about my new my-bugfix-branch. To switch to this branch, I can simply run:

git checkout my-bugfix-branch

Normally, I would need to create the branch before I could check it out, but in newer versions of git, it's smart enough to know that you want to checkout a local copy of this remote branch.

jperezmartin
  • 407
  • 4
  • 19
Andrew
  • 227,796
  • 193
  • 515
  • 708
  • I am using GIT 1.7.2.5, and the command that worked for me was: ``git branch --track XX origin/XX``. Your command gives me an error. – dimitarvp May 14 '12 at 16:17
  • 10
    If you don't have all remote branches downloaded this may fail. Do "git remote update" to pull them down. – Ben Walding Jul 05 '12 at 22:31
  • 1
    Actually, it can be even shorter "git checkout branch-name". If branch-name is the same as a remote tracking branch (ie origin/branch-name), then its automatically a local tracking branch. – David Neiss Aug 29 '15 at 04:43
79

For any Git newbies like me, here are some steps you could follow to download a remote repository, and then switch to the branch that you want to view. They probably abuse Git in some way, but it did the job for me! :-)

Clone the repository you want to download the code for (in this example I've picked the LRResty project on Github):

$ git clone https://github.com/lukeredpath/LRResty.git
$ cd LRResty

Check what branch you are using at this point (it should be the master branch):

$ git branch    
* master

Check out the branch you want, in my case it is called 'arcified':

 $ git checkout -b arcified origin/arcified
 Branch arcified set up to track remote branch arcified from origin.
 Switched to a new branch 'arcified'

Confirm you are now using the branch you wanted:

$ git branch    
* arcified
  master

If you want to update the code again later, run git pull:

$ git pull
Already up-to-date.
Dan J
  • 25,433
  • 17
  • 100
  • 173
56

you can use :

git clone <url> --branch <branch>

to clone/download only the contents of the branch.

This was helpful to me especially, since the contents of my branch were entirely different from the master branch (though this is not usually the case). Hence, the suggestions listed by others above didn't help me and I would end up getting a copy of the master even after I checked out the branch and did a git pull.

This command would directly give you the contents of the branch. It worked for me.

Old Markus
  • 836
  • 7
  • 12
  • 3
    This does not appear to work for me. It says there's already a folder with that name (which, of course, there is) – Jonathan Tuzman Apr 04 '19 at 21:23
  • 1
    @Jonathan It wont append a new branch with your existing one. It can only be used to clone a fresh copy. I don't see why there should already be a folder with that name if you haven't cloned the repository already. Try cloning at a different location. – Old Markus Apr 09 '19 at 14:24
  • 1
    Does not update an existing project with new branch – Dazzle Oct 02 '19 at 15:19
37

You could use git remote like:

git fetch origin

and then setup a local branch to track the remote branch like below:

git branch --track [local-branch-name] origin/remote-branch-name

You would now have the contents of the remote github branch in local-branch-name.

You could switch to that local-branch-name and start work:

git checkout [local-branch-name]
MikeN
  • 887
  • 5
  • 18
ardsrk
  • 2,407
  • 2
  • 21
  • 33
18

Navigate to the folder on your new machine you want to download from git on git bash.

Use below command to download the code from any branch you like

git clone 'git ssh url' -b 'Branch Name'

It will download the respective branch code.

Amit Joshi
  • 15,448
  • 21
  • 77
  • 141
Srikanta Sahoo
  • 227
  • 2
  • 2
9

Git clone and cd in the repo name:

$ git clone https://github.com/PabloEzequiel/iOS-AppleWach.git
Cloning into 'iOS-AppleWach'...
$ cd iOS-AppleWach

Switch to the branch (a GitHub page) that I want:

$ git checkout -b gh-pages origin/gh-pages
Branch gh-pages set up to track remote branch gh-pages from origin.
Switched to a new branch 'gh-pages'

And pull the branch:

$ git pull
Already up-to-date.

ls:

$ ls
index.html      params.json     stylesheets
Pablo Ezequiel Inchausti
  • 16,623
  • 7
  • 28
  • 42
9

$ git fetch

$ git checkout <branch>

git fetch will fetch all the remote branches. You can verify the remote branches with git branch -r (or git branch -rv) If you don't have an existing branch with same name, you can just switch directly to the branch with git checkout <branch>

Aditya Jain
  • 153
  • 1
  • 9
3

Quick answer:

https://github.com/[username]/[respository]/archive/refs/heads/[branch].zip

You can use the link to download directly without using the complex cmd.

myworldbox
  • 361
  • 6
  • 11
1

git checkout -b branch/name

git pull origin branch/name

Dazzle
  • 2,880
  • 3
  • 25
  • 52
-3

We can download a specified branch by using following magical command:

git clone -b < branch name > <remote_repo url> 
craigcaulfield
  • 3,381
  • 10
  • 32
  • 40
-8

Create a new directory, and do a clone instead.

git clone (address of origin) (name of branch)

Sei Satzparad
  • 1,137
  • 1
  • 9
  • 12