Skip to content

HansKre/sparse-checkout

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Description

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

The challange

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

How to clone child-dir only (the OLD way)

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

Clone child-dir with git version 2.25.0

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!

Make changes

$ 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.

Filtering aka partial clones

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.

Useful links

Bring your monorepo down to size with sparse-checkout

About

Test repo for sparse-checkout

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published