How to shallow clone a single branch in git?

I have a git repo I’d like to do a shallow copy on, and only pull a single branch.

This SO question says that git clone by default pulls all remote branches. I’d like to do a shallow copy of only a single branch.

  • Import history from renamed files and folders to version control
  • using git-flow with multiple git repositories per project/app?
  • Importing with EGit clones repository but doesn't let me import project
  • Git remote tracking branch diverged from local branch with identical commits with different SHAs
  • Workflow for Cypress, Pharo and Amber
  • Problems using git merge
  • I’m doing this to deploy into production. A full checkout is over 400MB, but a git archive of head is only 16MB. It appears that clone’s behavior of pulling down all the branches causes my download to be much larger than necessary.

  • How to update posh-git
  • How to rebase off master with github client?
  • Using eclipse push to branch in Remote
  • What are some more forceful ways than a .gitignore to keep (force) files out of a repo?
  • I got an error while using “heroku open” command
  • git: exchange the order of two commits before (including) common ancestor of multiple branches
  • 3 Solutions collect form web for “How to shallow clone a single branch in git?”

    Jakub already mentioned a shallow clone of selected branches is possible, but quite complex to do.
    And he added:

    Note however that because branches usually share most of their history, the gain from cloning only a subset of branches might be smaller than you think.

    I would add that you shouldn’t have any VCS tool in a production plateform (you only install/monitor what is necessary for the production to run).
    So git archive remains the best way to extract just what you need, as an archive (zip or tar, format that you can then uses without Git, once transferred on the production side)


    Update March 2012:

    the upcoming git1.7.10 (April 2012) will actually allow you to clone only one branch:

    git clone --single-branch
    

    You can see it in t5500-fetch-pack.sh:

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

    That feature was then fixes with:

    • commit 31b808a03290a4f87c571cc1c61f26d8a03c7025 (Ralf Thielow, Git 1.7.12.3, Oct. 2012)

    clone --single: limit the fetch refspec to fetched branch

    After running “git clone --single“, the resulting repository has the usual default “+refs/heads/*:refs/remotes/origin/*” wildcard fetch refspec installed, which means that a subsequent “git fetch” will end up grabbing all the other branches.

    Update the fetch refspec to cover only the singly cloned ref instead to correct this.

    • commit 60a5f5f (Junio C Hamano (gitster), Git 2.0.2, July 2014)

    builtin/clone.c: detect a clone starting at a tag correctly

    31b808a (clone --single: limit the fetch refspec to fetched branch, 2012-09-20) tried to see if the given “branch” to follow is actually a tag at the remote repository by checking with “refs/tags/” but it incorrectly used strstr(3); it is actively wrong to treat a “branch” “refs/heads/refs/tags/foo” and use the logic for the “refs/tags/
    ref hierarchy.
    What the code really wanted to do is to see if it starts with “refs/tags/“.


    Update Sept 2016: git clone --single-branch --branch tag will work for chained tags in Git 2.11+ (Q4 2016).

    You could simply do a normal (shallow) clone, and later delete the superfluous branches locally (and also the tracking remote branches). You would still have all the network traffic, but later your disk space is smaller.

    Recent git versions (I have v2.7.3) support shallow cloning of just one branch by calling:

    git clone --depth 1 <repository>
    
    Git Baby is a git and github fan, let's start git clone.