First do the following
- Clone this repo with
git clone --filter=blob:none --no-checkout
git sparse-checkout init
git sparse-checkout add README.md
git sparse-checkout reapply
git checkout
Now there should be just a single README.md
in project root.
Now, say you want to add a new file stuff.txt
in folder1
:
-
mkdir folder1
-
touch folder1/stuff.txt
-
A naive
git add folder1/stuff.txt
would produce the following error:The following paths and/or pathspecs matched paths that exist outside of your sparse-checkout definition, so will not be updated in the index: folder1/stuff.txt hint: If you intend to update such entries, try one of the following: hint: * Use the --sparse option. hint: * Disable or modify the sparsity rules. hint: Disable this message with "git config advice.updateSparsePath false"
-
To actually add the file, follow the first hint provided by git:
git add --sparse folder1/stuff.txt
-
However, after you do a
git pull
,folder1/stuff.txt
will vanish.- If
stuff.txt
is actually a folder, you can tell git to keep it withgit sparse-checkout add stuff.txt
andgit sparse-checkout reapply
. - Otherwise, force adding it will make ALL files under
folder1
available.
- If
Above is tested with git version 2.38.4
.