This repo showcases the new git sparse-checkout
feature and:
- how to checkout parts of a repo
- how to make changes only on that checked out part
- how to integrate those changes back to the upstream
Assume, you have an upstream repo that already exists with followin structure:
|____README.md
|____foo.txt
|____child-dir
| |____bar.txt
|____massive-other-dir
| |____foo-bar.txt
Let's clone child-dir
only.
The approach is a little bit different from the regular git clone <remote-url>
approach. We need to init a fresh repo locally:
mkdir clone
cd clone
git init
In that empty repo, we define the remote and fetch all objects but do not check them out. So the folder remains empty.
git remote add -f github git@github.com:HansKre/sparse-checkout.git
Check the history to make sure it is fetched:
git log --oneline
Now, enable the sparse-checkout:
git config core.sparseCheckout true
Next, we specify the folders that we want to be part of the sparse-checkout by adding them to .git/info/sparse-checkout
echo "child-dir" >> .git/info/sparse-checkout
We can finally update our local copy by pulling from upstream:
git pull github master
And yep, it worked:
$ tree
|____child-dir
| |____bar.txt
Git 2.25.0
includes a new experimental git sparse-checkout
command. Here's how to use it:
$ git sparse-checkout init --cone
$ git sparse-checkout set child-dir
$ ls
child-dir
That's much easier!
$ echo "some changes" > child-dir/bar.txt
$ git status
modified: child-dir/bar.txt
git add .
git commit -m "Make changes on sparse-checkout"
git push -u github master
If we go to child-dir/bar.txt, we can see that the changes have been successfully added to the repo.
On larger repos you might find filtering aka partial clones
useful:
git clone --filter=blob:none --no-checkout git@github.com:HansKre/sparse-checkout.git
Now, you can continue with the sparse-checkout
config to pull only desired folders. Or, of course, you could checkout the entire repo but with the filter applied.