[git] How to download a branch with git?

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?

This question is related to git branch

The answer is


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.


git checkout -b branch/name

git pull origin branch/name


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

git clone -b < branch name > <remote_repo url> 

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.

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.


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

Create a new directory, and do a clone instead.

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


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]