[git] How do I clone a single branch in Git?

I have a local Git repository called 'skeleton' that I use for storing project skeletons. It has a few branches, for different kinds of projects:

casey@agave [~/Projects/skeleton] git branch
* master
  rails
  c
  c++

If I want to check out the master branch for a new project, I can do

casey@agave [~/Projects] git clone skeleton new
Initialized empty Git repository in /Users/casey/Projects/new/.git/

and everything is how I want it. Specifically, the new master branch points to the skeleton master branch, and I can push and pull to move around changes to the basic project setup.

What doesn't work, however, is if I want to clone another branch. I can't get it so that I only pull the branch I want, for instance the rails branch, and then the new repository has a master branch that pushes to and pulls from the skeleton repository's rails branch, by default.

Is there a good way to go about doing this? Or, maybe this isn't the way that Git wants me to structure things, and I'm certainly open to that. Perhaps I should have multiple repositories, with the Ruby on Rails skeleton repository tracking the master skeleton repository? And any individual project cloning the Ruby on Rails skeleton repository.

This question is related to git branch git-clone

The answer is


For cloning a branch of Git you don't have the public key to, use this:

git clone -b <branch> <Git repository URL or clone URL you get from Git repository>

Let us take the example of flask repo. It has 3 branches in addition to master. Let us checkout the 1.1.x remote branch

clone the git repo

git clone https://github.com/pallets/flask

cd into the repo.

cd flask

fetch remote branch 1.1.x

git fetch origin 1.1.x

checkout the branch

git checkout 1.1.x

You will switch to the branch 1.1.x and it will track the remote 1.1.x branch.


git clone <url> --branch <branch> --single-branch

Just put in URL and branch name.


you can use this command to git single branch and rename the folder if you want to keep branched stand alone

git clone -b [branch-name] --single-branch [url]  [folder_name]

example

git clone -b mybranch --single-branch git://github/repository.git  project_mybranch

For the newbies like me, just run the code below

git clone https://gitlab.com/repo/repo.git --branch <name of branch> --single-branch

Open the cmd.

cd folder_name  # enter the path where to clone the branch

Just one command:

git clone url_of_projecturltoclone -b branch_name

This should work

git clone --single-branch <branchname> <remote-repo-url>
git clone --branch <branchname> <remote-repo-url>

Can be done in 2 steps

  1. Clone the repository

    git clone <http url>
    
  2. Checkout the branch you want

    git checkout $BranchName
    

There are primarily 2 solutions for this:

  1. You need to specify the branch name with -b command switch. Here is the syntax of the command to clone the specific git branch.

    git clone -b <BRANCH_NAME> <GIT_REMOTE_URL>
    

    Example:

    git clone -b tahir https://github.com/Repository/Project.git
    

    The following command will clone the branch tahir from the git repository.The above command clones only the specific branch but fetches the details of other branches. You can view all branches details with command.

    git branch -a
    
  2. You can use --single-branch flag to prevent fetching details of other branches like below:

    git clone -b <BRANCH_NAME> --single-branch <GIT_REMOTE_URL>
    

    Example:

    git clone -b tahir --single-branch \ 
    https://github.com/Repository/Project.git
    

    Now if you do a git branch -a, it will only show your current single branch that you have cloned and not all the branches. So it depends on you how you want it.


For cloning a specific branch you can do :

git clone --branch yourBranchName [email protected]

git clone --branch {branch-name} {repo-URI}

Example:

git clone --branch dev https://github.com/ann/cleaningmachine.git

Using Git version 1.7.3.1 (on Windows), here's what I do ($BRANCH is the name of the branch I want to checkout and $REMOTE_REPO is the URL of the remote repository I want to clone from):

mkdir $BRANCH
cd $BRANCH
git init
git remote add -t $BRANCH -f origin $REMOTE_REPO
git checkout $BRANCH

The advantage of this approach is that subsequent git pull (or git fetch) calls will also just download the requested branch.


Clone only one branch. This is the easiest way:

git clone -b BRANCH_NAME --single-branch [email protected]:___/PROJECTNAME.git

You can try the long-winded way:

mkdir newrepo.git
cd newrepo.git
git init
git remote add origin file:///path/to/original
git fetch origin branchiwant:refs/remotes/origin/branchiwant
git checkout -b branchiwant --track origin/branchiwant

What this does is:

  • Create and init an empty Git repository.
  • Adds the original repository as a remote called origin.
  • Fetches only the branch you require from the remote called origin.
  • Creates and checks out a new branch that is set up to track the source branch you just cloned.

Hopefully that will be something like what you are after.


If you want a shallow clone, you can do this with:

git clone -b mybranch --depth=1 https://example.com/myproject.git localname

--depth=1 implies --single-branch.


You can do it by using the below command:

git clone -b branch_name --single-branch project_url local_folder_to_clone_in

A little late but I wanted to add the solution I used to solve this problem. I found the solution here.

Anyway, the question seems to be asking 'how to start a new project from a branch of another repo?'

To this, the solution I used would be to first create a new repo in github or where ever. This will serve as the repo to your new project.

On your local machine, navigate to the project that has the branch you want to use as the template for your new project.

Run the command:

git push https://github.com/accountname/new-repo.git +old_branch:master

What this will do is push the old_branch to new-repo and make it the master branch of the new repo.

You then just have to clone the new repo down to your new project's local directory and you have a new project started at the old branch.


From git-clone man page:

--single-branch is your friend during clone remember to use with --branch <branch name> or only remote primary HEAD will be cloned (master by default)

Always remember to do Ctrl + F5 to read fresh source, not the one from cache :-) (I didn't so didn't know about this option for long time.)


One way is to execute the following.

git clone user@git-server:project_name.git -b branch_name /your/folder

Where branch_name is the branch of your choice and "/your/folder" is the destination folder for that branch. It's true that this will bring other branches giving you the opportunity to merge back and forth.

Update

Now, starting with Git 1.7.10, you can now do this

git clone user@git-server:project_name.git -b branch_name --single-branch /your/folder

Similar to what @nosaiba-darwish said here: here

This is what we usually do in our company:

git clone -b <name_of_branch> --single-branch <git_url> folder_to_clone_locally

There are ample answers here which mention:

# (We'll refer to this as "the 1st command" below.)
git clone -b branch_name --single-branch \
https://github.com/some_project/some_project.git

...or some version of that, and a few which mention just:

# (We'll refer to this as "the 2nd command" below.)
git clone -b branch_name https://github.com/some_project/some_project.git

...withOUT the --single-branch part.

But, I'd like to expound upon these two things a bit and show a more familiar set of equivalent commands so we can see what is happening with each under-the-hood.

Let's assume that you have a remote repo on GitHub at https://github.com/micronucleus/micronucleus.git, with remote branches master and version_2.5 (this is a real example you can actually run right now).

Breakdown of the 2nd command from above:

The 2nd command (git clone -b version_2.5 https://github.com/micronucleus/micronucleus.git) clones ALL REMOTE BRANCHES to your local PC, but then checks out the version_2.5 branch instead of the master branch. That one command is the equivalent of doing this:

git clone https://github.com/micronucleus/micronucleus.git
cd micronucleus  # cd into the repo you just cloned
git checkout version_2.5
# To be pedantic, also delete the local `master` branch since
# technically it won't exist yet since you haven't yet checked
# it out with `git checkout master`, which would create it from
# your locally-stored remote-tracking branch, `origin/master`
git branch -d master

The -b version_2.5 part automatically checked out the version_2.5 branch for us instead of master.

git branch -a shows us that ALL branches, however, were cloned to our local PC. Here you can see our local branch version_2.5, which we are on, plus the locally-stored remote-tracking branches origin/HEAD (which points to origin/master), plus origin/master, and origin/version_2.5:

$ git branch -a
* version_2.5
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
  remotes/origin/version_2.5

We can also look at what our fetch references are. You can either open up the .git/config file to see them directly, or just run git config remote.origin.fetch:

$ git config remote.origin.fetch
+refs/heads/*:refs/remotes/origin/*

You can see above that our git fetch command (which is also triggered by git pull since that is equivalent to git fetch && git merge) is configured to fetch ALL heads for ALL branches in the origin remote. I'm not an expert on this part, but I believe that's what +refs/heads/*:refs/remotes/origin/* means.

Breakdown of the 1st command from above:

The 1st command (git clone -b version_2.5 --single-branch https://github.com/micronucleus/micronucleus.git) clones ONLY the version_2.5 branch to your local PC, and it also checks it out. That one command is the equivalent of doing this (in the end result at least, except that it also downloads much less data in the beginning since it only clones ONE branch NOT all of them):

git clone https://github.com/micronucleus/micronucleus.git
cd micronucleus  # cd into the repo you just cloned
git checkout version_2.5

# Delete ALL other branches, including remote-tracking ones, which are not the 
# `version_2.5` branch:
# One local branch
git branch -d master
# ALL other locally-stored remote-tracking branches
git branch -dr origin/HEAD 
git branch -dr origin/master

# Fix your `.git/config` file so that `git fetch` does the right thing, fetching
# ONLY the `version_2.5` branch head from the `origin/version_2.5` remote branch:
git config remote.origin.fetch \
"+refs/heads/version_2.5:refs/remotes/origin/version_2.5"

The -b version_2.5 part caused the version_2.5 branch to be checked out instead of the master branch by default (as previously explained above), and the --single-branch part caused:

  1. NONE of the other branches to be cloned to our PC, and
  2. git fetch to be configured such that NONE of the other branches will ever be fetched when we call git fetch or git pull!

This command truly cloned and will fetch only the one branch we wanted, and that's it!

git branch -a shows us that ONLY the version_2.5 branch was cloned and checked out. Here we see by the * which branch is checked-out, and we see also that we have a locally-stored remote-tracking branch for origin/version_2.5:

$ git branch -a
* version_2.5
  remotes/origin/version_2.5

We can also look at what our fetch references are. You can either open up the .git/config file to see them directly, or just run git config remote.origin.fetch:

$ git config remote.origin.fetch
+refs/heads/version_2.5:refs/remotes/origin/version_2.5

You can see above that our git fetch command will only fetch the version_2.5 branch head from the origin/version_2.5 remote branch. That's it! Beware that no other remote branches will ever be fetched.

Summary:

So, now you see that using -b branch_name basically just ensures the branch_name branch is checked-out after the clone, but still clones ALL remote branches, whereas adding also --single-branch ensures that ONLY branch_name is cloned, fetched, pulled, and tracked. No other remote branches will be cloned to your PC whatsoever.

Personally, I prefer the -b branch_name option alone, because I want all branches cloned to my local PC. The one exception might be on a huge, shared mono-repo which has dozens, or even hundreds or thousands of remote branches. In that case, just use -b branch_name --single-branch to clone just the one main branch you care about and be done. Better to download 50 GiB of data for the master branch in a huge mono-repo, for instance, than to download 200 GiB of data so you can have 2000 of your peers' branches they are working on too!

References:

  1. Clone only one branch
  2. How do you stop tracking a remote branch in Git?

Examples related to git

Does the target directory for a git clone have to match the repo name? Git fatal: protocol 'https' is not supported Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) git clone: Authentication failed for <URL> destination path already exists and is not an empty directory SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443 GitLab remote: HTTP Basic: Access denied and fatal Authentication How can I switch to another branch in git? VS 2017 Git Local Commit DB.lock error on every commit How to remove an unpushed outgoing commit in Visual Studio?

Examples related to branch

Get git branch name in Jenkins Pipeline/Jenkinsfile Why do I have to "git push --set-upstream origin <branch>"? Your configuration specifies to merge with the <branch name> from the remote, but no such ref was fetched.? When does Git refresh the list of remote branches? Fix GitLab error: "you are not allowed to push code to protected branches on this project"? Git push: "fatal 'origin' does not appear to be a git repository - fatal Could not read from remote repository." Git: Merge a Remote branch locally git pull from master into the development branch Depend on a branch or tag using a git URL in a package.json? How can I copy the content of a branch to a new local branch?

Examples related to git-clone

Git: Permission denied (publickey) fatal - Could not read from remote repository. while cloning Git repository git clone from another directory How to git clone a specific tag fatal: could not create work tree dir 'kivy' Unable to Connect to GitHub.com For Cloning Is it possible to find out the users who have checked out my project on GitHub? BitBucket - download source as ZIP "fatal: Not a git repository (or any of the parent directories)" from git status Git clone without .git directory How to get Git to clone into current directory