DESCRIPTION. This command is used to create sparse checkouts, which change the working tree from having all tracked files present to only having a subset of those files. It can also switch which subset of files are present, or undo and go back to having all tracked files present in the working copy.
If you get stuck, run git sparse-checkout disable to return to a full working directory. The init subcommand sets the necessary Git config options and fills the sparse-checkout file with patterns that mean “only match files in the root directory”.
It checks the branches and updates the files in the working directory to match the version already available in that branch, and it forwards the updates to Git to save all new commit in that branch.
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
In 2020 there is a simpler way to deal with sparse-checkout without having to worry about .git files. Here is how I did it:
git clone <URL> --no-checkout <directory>
cd <directory>
git sparse-checkout init --cone # to fetch only root files
git sparse-checkout set apps/my_app libs/my_lib # etc, to list sub-folders to checkout
git checkout # or git switch
Note that it requires git version 2.25 installed. Read more about it here: https://github.blog/2020-01-17-bring-your-monorepo-down-to-size-with-sparse-checkout/
UPDATE:
The above git clone
command will still clone the repo with its full history, though without checking the files out. If you don't need the full history, you can add --depth parameter to the command, like this:
# create a shallow clone,
# with only 1 (since depth equals 1) latest commit in history
git clone <URL> --no-checkout <directory> --depth 1
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With