This question already has an answer here:

Git clone will behave copying remote current working branch into local.

Is there any way to clone a specific branch by myself without switching branches on remote repository?

marked as duplicate by Jaydles Mar 4 '14 at 22:28

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 10
    @domokun Jorge added his answer a year after Michael. So it frequently occurs on SO that the best answer is added after another answer has already been accepted. (IOW, always at least scan the top 3-4 answers on any question.) – Ryan Ballantyne Dec 6 '13 at 23:57
  • 5
    @scud: if you're still on SO, do you want to fix the accepted answer so others can see it more easily? – mikemaccana Jun 15 '16 at 16:59
  • 2
    why does SO require @Scud to edit the answer? After 5000 votes, the general community should have a say in what is marked as the correct answer. – gdbj May 21 '17 at 22:12
  • Change the accepted answer please SO moderators. – user1271772 Sep 17 '17 at 21:15
  • 1
    Vote down accepted answer. Maybe SO will finally change it if it goes negative. – user1271772 Sep 17 '17 at 21:16
up vote 274 down vote accepted

Please see also the other answer which many people prefer.

You may also want to make sure you understand the difference, unlike the majority of angry commenters and downvoters. And the difference is: by invoking git clone -b branch url you're fetching all the branches and checking out one. That may, for instance, mean that your repository has a 5kB documentation or wiki branch and 5GB data branch. And whenever you want to edit your frontpage, you may end up cloning 5GB of data.

Again, that is not to say git clone -b is not the way to accomplish that, it's just that it's not always what you want to accomplish, when you're asking about cloning a specific branch.

The answer I would write today, with today's git in mind would be

git clone --single-branch -b branch host:/dir.git

at the time of writing the original answer below, git had no --single-branch option, but let's preserve it for full satisfaction of angry bees.

The answer so badly disliked by copypasters was this:

git init
git remote add -t refspec remotename host:/dir.git
git fetch
  • 58
    Thanks. I figured out using below method. git clone <remote_repo> git checkout -b <wanted_branch> origin/<wanted_branch> git branch -D master – Scud Dec 16 '09 at 0:05
  • 2
    I think so, and you will get [remote "origin"] fetch = +refs/heads/(refspec):refs/remotes/origin/(refspec) instead of fetch = +refs/heads/*:refs/remotes/origin/* – Scud Dec 16 '09 at 1:54
  • 17
    this one is easier and more appropriate stackoverflow.com/a/4568323/636762 – Syed Rakib Al Hasan Dec 15 '12 at 23:32
  • 316
    Guys, make sure to see the real answer below. – BrainSlugs83 Jun 27 '15 at 20:34
  • 14
    @Scud, can you please accept the other answer, I'm somewhat tired of brilliant minds attracted, each trying to contribute their unique opinion not unlike millions before them. – Michael Krelin - hacker Nov 18 '15 at 11:04
git clone -b <branch> <remote_repo>

Example:

git clone -b my-branch git@github.com:user/myproject.git

With Git 1.7.10 and later, add --single-branch to prevent fetching of all branches. Example, with OpenCV 2.4 branch:

git clone -b opencv-2.4 --single-branch https://github.com/Itseez/opencv.git
  • 473
    pierr: I'm not sure if this answers the description of the problem given above, but it does answer the actual question - how to clone a specific branch of a repository. I voted this up because it's the answer I was googling for when I came to this page. – Jaime Bellmyer May 15 '11 at 3:35
  • 23
    This works. It points the new HEAD at the specified branch rather than at the HEAD-branch in myproject. However, it still fetches all branches. See @edmar-miyake's answer. – cdunn2001 Mar 17 '12 at 20:35
  • 6
    See this answer for news: stackoverflow.com/a/14930421/755257 – cbeleites Feb 22 '13 at 20:15
  • 3
    It answers the description of the problem if you add a --depth X to the command. If you do so, it will clone only the specified branch and its last content. – ramsvidor Sep 12 '13 at 23:32
  • 7
    thx for --single-branch; git 2.5 is out at time of writing this. Don't care for older versions. – Bernhard Döbler Aug 20 '15 at 10:46

To clone a branch without fetching other branches:

mkdir $BRANCH
cd $BRANCH
git init
git remote add -t $BRANCH -f origin $REMOTE_REPO
git checkout $BRANCH
  • Good solution. On older git (I have 1.5.5.6), a git branch --track $BRANCH origin/$BRANCH may be needed before the checkout. – Johannes Thoma Aug 13 '13 at 13:19
  • 2
    Works, and also fetches just those tags present on the branch, which is what I wanted. (I actually wanted to fetch multiple branches, but only selected ones; for that, it sufficed to repeatedly remote add and checkout as here, then git remote rm origin to clean up.) – Jesse Glick Sep 3 '14 at 1:45
  • 4
    Using the Jorge Eduardo Cardona's answer gives me huge repositories, using this method gives me very clean repositories. Thanks. – joshcomley Jan 4 '15 at 15:17
  • 1
    Perfect solution for shallowly incorporating a specific tag of a git repo in another project. Recommend omitting -f from the git remote command, then using git fetch --depth=1 $BRANCH $TAG, then git checkout FETCH_HEAD. The init is innocuous, and changing tags will automatically update the checked out code. – taranaki May 12 '17 at 20:41

Here is a really simple way to do it :)

Clone the repository

git clone <repository_url>

List all branches

git branch -a 

Checkout the branch that you want

git checkout <name_of_branch>
  • 1
    This switched the working directory to the correct branch, but I'm not able to push any changes I make, because I'm not "currently on a branch". – Seppo Enarvi Jan 21 '15 at 9:46
  • 1
    This was the solution for me, since I had already cloned 'master'. I didn't know I could simply 'checkout' a remote branch. – yazzer Jul 30 '15 at 21:34
  • 1
    This is probably the correct way to do it; best-practices-wise – aaiezza Jul 25 '16 at 21:01
  • git checkout mybranchname then git fetch done – Shohanur Rahaman Mar 31 '17 at 16:54
  • This way doesn't clone only the choosen branch. This answer seems better: stackoverflow.com/a/7349740/3075243. For example if a repo has many branches that are big enough that we don't wanna clone each one. – Proustibat Jan 31 at 23:04
git checkout -b <branch-name> <origin/branch_name>

for example in my case:

 git branch -a
* master
  origin/HEAD
  origin/enum-account-number
  origin/master
  origin/rel_table_play
  origin/sugarfield_customer_number_show_c

So to create a new branch based on my enum-account-number branch I do:

git checkout -b enum-account-number origin/enum-account-number

After you hit return the following happens:

Branch enum-account-number set up to track remote branch refs/remotes/origin/enum-account-number.
Switched to a new branch "enum-account-number

"

  • 4
    Note that it may be useful to git pull origin first so that git branch -a can list all new (current) remote branches. – André Caron Oct 12 '12 at 4:13
  • Good point. Probably git fetch is better so that the auto merge doesn't happen, though. – dkinzer Oct 12 '12 at 14:09

Create a branch on the local system with that name. e.g. say you want to get the branch named "branch-05142011"

git branch branch-05142011 origin/branch-05142011

It'll give you a message like - "Branch branch-05142011 set up to track remote branch branch-05142011 from origin."

Now just checkout the branch like below and you have the code -
git checkout branch-05142011

  • 3
    This will do too : git fetch origin [remote-branch]:[new-local-branch] – PlanetUnknown May 15 '11 at 21:09
  • has it right. Miyake (below) shows how to do it when the remote is added. – cdunn2001 Feb 23 '12 at 21:37
  • That should say, "PlanetUnknown has it right." – cdunn2001 Feb 26 '12 at 21:38
  • 2
    @PlanetUnknown Thanks for git fetch origin [remote-branch]:[new-local-branch], I love that! – Jeaf Gilbert Mar 9 '12 at 5:49
git --branch <branchname> <url>

But bash completion don't get this key: --branch

Enjoy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.