Let's say I did a sparse checkout of a partial clone to not get everything from a big Git repo:
git clone --filter=blob:none --sparse https://example.com/url/of/my/repo.git
cd repo
git sparse-checkout add Boost/boost_1_77_0
How can I then undo git sparse-checkout add Boost/boost_1_77_0
to remove the directory locally (but not from the server's branch) ?
As of today there is no command to undo
git sparse-checkout add
according to the documentation.Here is a manual solution that requires modifying a config file within the
.git
directory: proceed with care.This solution assumes you are in cone mode which is likely because it is the default mode.
This solution assumes you did do a matching
git sparse-checkout add
i.e. you would have to tweak it if you did a biggeradd
and want to un-add a smaller set of files than what you took withgit sparse-checkout add
.Note that this solution will not reduce the size of the clone as much as theoretically possible because currently local blobs cannot be deleted from a clone, however if you do not use Git LFS there is compression so the space you do not regain is usually less than the size of deleted files (if blobs size is 30% of the files size you will go from 130% of space usage to 30% of space usage).
Manual Solution
.git/info/sparse-checkout
, just in casegit status
. It should not show anything added, changed, deleted or untracked. This is not strictly necessary but recommended to help you understand what is going on in the next steps.git pull
to be up-to-date.Boost/boost_1_77_0
). If you rungit status
you should see it's content removed. Do notgit add
orgit rm
orgit commit
these deletions: they must stay at worktree level..git/info/sparse-checkout
corresponding to the directory (in your exampleBoost/boost_1_77_0/
):Boost/
is now empty) and you do not need the files it contains (see note below), you have two more lines to remove:Boost/
is at the root)git sparse-checkout reapply
. It should run without error.git status
. It should not show any removed file.Note on step 6.: in cone mode you get the files (not directories) which are contained inside all added directories or their parent directories, so be careful to not do this step if you rely on some of these files - or do
git sparse-checkout add
those you need afterward.