简体   繁体   中英

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.

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.

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

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

最近的git版本(我有v2.7.3)支持通过调用以下方法对一个分支进行浅层克隆

git clone --depth 1 <repository>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM