How to 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

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

  • Is there a way to output the previous git branch name?
  • If a git pull request is still pending, how do I continue working across branches?
  • Git pull on non-working branch without switching
  • pull-only repo's 'git status' saying the branch is ahead of origin/master. Why?
  • In Git, how do you see and manage commits that aren't in a branch?
  • Tools for Maintaining Branches in SVN
  • 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 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 repo has a ‘master’ branch that pushes to and pulls from the skeleton repo’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 repos, with the rails skeleton repo tracking the master skeleton repo? And any individual project cloning the rails skeleton repo.

    Any thoughts and suggestions are appreciated!

  • How does the Bittorrent Sync secret work?
  • git - track or pull from multiple remotes
  • get the username of who triggered pipeline job ( manually or scm trigger)
  • Speeding up the initial git-svn fetch
  • Highlight in git log
  • Linking a single file from another git repository
  • 12 Solutions collect form web for “How to clone a single branch in git?”

    Note: the git1.7.10 (April 2012) actually allows you to clone only one branch:

    # clone only the remote primary HEAD (default: origin/master)
    git clone --single-branch
    as in:
    git clone <url> --branch <branch> --single-branch [<folder>]

    You can see it in

    test_expect_success 'single branch clone' '
      git clone --single-branch "file://$(pwd)/." singlebranch

    Tobu comments that:

    This is implicit when doing a shallow clone.
    This makes git clone --depth 1 the easiest way to save bandwidth.

    And since Git 1.9.0 (February 2014), shallow clones support data transfer (push/pull), so that option is even more useful now.
    See more at “Is git clone --depth 1 (shallow clone) more useful than it makes out?”.

    “Undoing” a shallow clone is detailed at “Convert shallow clone to full clone” (git 1.8.3+)

    # unshallow the current branch
    git fetch --unshallow
    # for getting back all the branches (see Peter Cordes' comment)
    git config remote.origin.fetch refs/heads/*:refs/remotes/origin/*
    git fetch --unshallow

    As Chris comments:

    the magic line for getting missing branches to reverse --single-branch is (git v2.1.4):

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

    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. 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

    Using git version (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.

    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 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 brach that is setup to track the source branch you just cloned.

    Hopefully that will be something like what you are after.

    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)

    You can do it by using the below command:

    git clone -b branch_name --single-branch project_url local_folder_to_clone_in
    git clone <url> --branch <branch> --single-branch

    Just put url and branch name

    Can be done in 2 steps

    1. Clone the repository

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

      • git checkout $BranchName

    After cloning was complete, I had to enter git submodule update --init --recursive to download all submodules

    open the cmd
    cd folder_name (Enter the path where to clone the branch)
    Just a command

    git clone url_of_projecturltoclone -b branch_name
    1. Open Git bash shell.
    2. Create an directory in your file system where you want to checkout.
      • $ mkdir Feature_develop_branch
    3. change directory to Feature_develop_branch folder.
      • $ cd Feature_develop_branch
    4. clone the repository using external clone url.
    5. After cloning, change the directory to created repositoryName.
      • $ cd /repositoryName
    6. Check out the branch.
      • $ git checkout

    for cloning branch of git you don’t have public key the use this

    git clone -b <branch> <git repo url or clone url you get from git repo>
    Git Baby is a git and github fan, let's start git clone.