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 kind of repository now that it supports sparse checkouts but every example that I can find does the following:
git clone <path>
git config core.sparsecheckout true
echo <dir> > .git/info/sparse-checkout
git read-tree -m -u HEAD
The problem with this sequence of commands is the original clone also does a checkout. If you add -n to the original clone command, then the read-tree command results in the following error:
error: Sparse checkout leaves no entry on working directory
How can do the sparse checkout without checking out all the files first?
Git clone has an option (
--no-checkout
or-n
) that does what you want.In your list of commands, just change:
To this:
You can then use the sparse checkout as stated in the question.
I'm new to git but it seems that if I do git checkout for each directory then it works. Also, the sparse-checkout file needs to have a trailing slash after every directory as indicated. Someone more experience please confirm that this will work.
Interestingly, if you checkout a directory not in the sparse-checkout file it seems to make no difference. They don't show up in git status and git read-tree -m -u HEAD doesn't cause it to be removed. git reset --hard doesn't cause the directory to be removed either. Anyone more experienced care to comment on what git thinks of directories that are checked out but which are not in the sparse checkout file?
I found the answer I was looking for from the one-liner posted earlier by pavek (thanks!) so I wanted to provide a complete answer in a single reply that works on Linux (GIT 1.7.1):
I changed the order of the commands a bit but that does not seem to have any impact. The key is the presence of the trailing slash "/" at the end of the path in step 5.
git 2.9 (June 2016) will generalize the
--no-checkout
option togit 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:Sadly none of the above worked for me so I spent very long time trying different combination of
sparse-checkout
file.In my case I wanted to skip folders with IntelliJ IDEA configs.
Here is what I did:
Run
git clone https://github.com/myaccount/myrepo.git --no-checkout
Run
git config core.sparsecheckout true
Created
.git\info\sparse-checkout
with following contentRun 'git checkout --' to get all files.
Critical thing to make it work was to add
/*
after folder's name.I have git 1.9
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.