I\'m working with a repository with a very large number of files that takes hours to checkout. I\'m looking into the possibility of whether Git would work well with this kin
I had a similar use case, except I wanted to checkout only the commit for a tag and prune the directories. Using --depth 1
makes it really sparse and can really speed things up.
mkdir myrepo
cd myrepo
git init
git config core.sparseCheckout true
git remote add origin <url> # Note: no -f option
echo "path/within_repo/to/subdir/" > .git/info/sparse-checkout
git fetch --depth 1 origin tag <tagname>
git checkout <tagname>
Works in git 2.28
git clone --filter=blob:none --no-checkout --depth 1 --sparse <project-url>
cd <project>
git sparse-checkout init --cone
Specify the files and folders you want to clone
git sparse-checkout add <folder>/<innerfolder> <folder2>/<innerfolder2>
git checkout
Updated answer 2020:
There is now a command git sparse-checkout, that I present in detail with Git 2.25 (Q1 2020)
nicono's answer illustrates its usage:
git sparse-checkout init --cone # to fetch only root files
git sparse-checkout add apps/my_app
git sparse-checkout add libs/my_lib
It has evolved with Git 2.27 and knows how to "reapply" a sparse checkout, as in here.
Note that with Git 2.28, git status will mention that you are in a sparse-checked-out repository
Original answer: 2016
git 2.9 (June 2016) will generalize the --no-checkout
option to git worktree add
(the command which allows to works with multiple working trees for one repo)
See commit ef2a0ac (29 Mar 2016) by Ray Zhang (OneRaynyDay).
Helped-by: Eric Sunshine (sunshineco), and Junio C Hamano (gitster).
(Merged by Junio C Hamano -- gitster -- in commit 0d8683c, 13 Apr 2016)
The git worktree man page now includes:
--[no-]checkout:
By default,
add
checks out<branch>
, however,--no-checkout
can be used to suppress checkout in order to make customizations, such as configuring sparse-checkout.
Please note that this answer does download a complete copy of the data from a repository. The git remote add -f
command will clone the whole repository. From the man page of git-remote:
With
-f
option,git fetch <name>
is run immediately after the remote information is set up.
Try this:
mkdir myrepo
cd myrepo
git init
git config core.sparseCheckout true
git remote add -f origin git://...
echo "path/within_repo/to/desired_subdir/*" > .git/info/sparse-checkout
git checkout [branchname] # ex: master
Now you will find that you have a "pruned" checkout with only files from path/within_repo/to/desired_subdir present (and in that path).
Note that on windows command line you must not quote the path, i.e. you must change the 6th command with this one:
echo path/within_repo/to/desired_subdir/* > .git/info/sparse-checkout
if you don't you'll get the quotes in the sparse-checkout file, and it will not work
Steps to sparse checkout only specific folder:
1) git clone --no-checkout <project clone url>
2) cd <project folder>
3) git config core.sparsecheckout true [You must do this]
4) echo "<path you want to sparce>/*" > .git/info/sparse-checkout
[You must enter /* at the end of the path such that it will take all contents of that folder]
5) git checkout <branch name> [Ex: master]
Git clone has an option (--no-checkout
or -n
) that does what you want.
In your list of commands, just change:
git clone <path>
To this:
git clone --no-checkout <path>
You can then use the sparse checkout as stated in the question.