How do I add existing remote branch to my locally cloned repository?

11.1k views Asked by At

I would like to add this branch to my local clone: https://github.com/dmitriz/mithril.js/tree/rewrite

For some unclear reason, it did not get cloned.

So I ended up with local directory without that branch. Is there any easy way to clone this branch too?

3

There are 3 answers

8
Richard-Degenne On BEST ANSWER

All you have to do is checking out to this branch.

$ git checkout rewrite

Git will switch to a new branch, tracking the remote one. Here is the message you should get

Branch rewrite set up to track remote branch rewrite from origin.
Switched to a new branch 'rewrite'
1
lonelyelk On

If you clone a repository you already have all branches. All you need to do is to create a local branch from it since it is a distributed scm.

git checkout -b rewrite origin/rewrite

You can also have other name for your local branch or have several copies of it.

1
MrTux On

I assume that the repository you initially cloned is your origin.

So just do git fetch origin and it should pop up on git branch -a - after that you can easily check it out using git checkout -b rewrite origin/rewrite.

A possibe explanation is that this branch was simply not there when you initially cloned it and you never fetched all (new) branches.