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.
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.
Cloning a Single Branch Using git clone The classic git clone command with the --single-branch option will clone only the master branch by default. If you want to clone another branch, you should add the --branch flag with the name of the desired branch.
There are two ways to clone a specific branch. You can either: Clone the repository, fetch all branches, and checkout to a specific branch immediately. Clone the repository and fetch only a single branch.
The git clone –single-branch –branch command clones a specific branch. This command lets you copy the contents of a repository without downloading all the branches on the repository. It is useful if a repository is large and you only want to download the code you will use.
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:
clone --single
: limit the fetch refspec to fetched branchAfter 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.
gitster
), Git 2.0.2, July 2014)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 usedstrstr(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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With