Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to shallow clone a single branch in git?

Tags:

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.

like image 530
Allen Rohner Avatar asked Feb 08 '11 04:02

Allen Rohner


People also ask

How do I clone a single branch?

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.

Can I git clone a specific 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.

What is git clone 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.


1 Answers

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

like image 118
VonC Avatar answered Sep 21 '22 17:09

VonC