How do I clone a specific Git branch? [duplicate]

Asked 2023-09-20 20:13:50 View 970,540

Git clone will clone remote branch into local.

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

Answers

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

Answered   2023-09-20 20:13:50

  • 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. - anyone
  • 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. - anyone
  • 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. - anyone
  • thx for --single-branch; git 2.5 is out at time of writing this. Don't care for older versions. - anyone
  • @jorge Why -b option requires a separate --single-branch flag? Does -b alone clones all branches? - anyone
git clone --single-branch --branch <branchname> <remote-repo>

The --single-branch option is valid from version 1.7.10 and later.

Please see also the other answer which many people prefer.

You may also want to make sure you understand the difference. And the difference is: by invoking git clone --branch <branchname> 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 --branch 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.

Answered   2023-09-20 20:13:50

  • Comments are not for extended discussion; this conversation has been moved to chat. - anyone
  • Pardon me, are you sure about the part you said "you're fetching all..."? I read somewhere that git fetch doesn't actually "copy" any files, it just fetches metadata and information about the changes. So it should be relatively light weight... Maybe you've used the word "fetch" literally and not from the git vocabulary? - anyone
  • @aderchox, no it will actually fetch all the content. It is pretty smart about things it transfers when you update, but when you clone a big repository it actually pulls the history, unless you explicitly tell it not to. But it will still fetch the tip of the branch. What git fetch does not — it does not check out files, but that's not about the transfer. - anyone
  • Often you will also want --depth 1 so that you only get the latest. This can save a lot of downloading time. - anyone
  • git clone -b branch_name --single-branch 'repo_url' - anyone

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>

Answered   2023-09-20 20:13:50

  • 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". - anyone
  • This was the solution for me, since I had already cloned 'master'. I didn't know I could simply 'checkout' a remote branch. - anyone
  • This is probably the correct way to do it; best-practices-wise - anyone
  • 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. - anyone
  • Very crisp answer. One additional things you have to do is: After this step: "git checkout <name_of_branch>" Do this: git branch --set-upstream-to=origin/<branch> <local_branch> Thanks. - anyone

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

Answered   2023-09-20 20:13:50

  • Good solution. On older git (I have 1.5.5.6), a git branch --track $BRANCH origin/$BRANCH may be needed before the checkout. - anyone
  • 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.) - anyone
  • 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. - anyone
  • Unlike Michael Krelin's (3-step) answer, this one actually worked for me (git 1.7.9.5) - anyone
  • after alot of fail, this code is works to me.. git version 2.9.2 - anyone

Use:

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"

Answered   2023-09-20 20:13:50

  • Note that it may be useful to git pull origin first so that git branch -a can list all new (current) remote branches. - anyone
  • Good point. Probably git fetch is better so that the auto merge doesn't happen, though. - anyone
  • this solution is helpful for creating a new local branch, switching to it and setting up to track the corresponding remote branch - anyone

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:

$ git checkout --track origin/branch-05142011
Branch branch-05142011 set up to track remote branch refs/remotes/origin/branch-05142011.
Switched to a new branch "branch-05142011"

Now just checkout the branch like below and you have the code

git checkout branch-05142011

Answered   2023-09-20 20:13:50

  • This will do too : git fetch origin [remote-branch]:[new-local-branch] - anyone
  • has it right. Miyake (below) shows how to do it when the remote is added. - anyone
  • That should say, "PlanetUnknown has it right." - anyone
  • @PlanetUnknown Thanks for git fetch origin [remote-branch]:[new-local-branch], I love that! - anyone
  • I tried git branch ue5-early-access origin/ue5-early-access and it errors: "fatal: Not a valid object name: 'origin/ue5-early-access'.", any tip? - anyone
git --branch <branchname> <url>

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

Answered   2023-09-20 20:13:50