How to configure git sparse-checkout so that it checks out entire repository, with the exception of particular directory?

85 Views Asked by At

I am aware that a very similar question has been asked before, but not only is the provided answer awful and doesn't answer the question, but it seems there is no consensus about the technique as well. And even after searching on the net in general there doesn't seem to be any solution that works.

So here is it again.

How to configure git sparse-checkout so that it checks out the entire repository, with the exception of a particular (top level) directory?

Directory is called Tests_NR in the example.

Please provide an example that works with git version >= 2.37.3, on Windows Server 2016.

If possible, it should not be required to checkout the entire repo beforehand. (And then remove the excluded dir). But rather suppose we start with a clone without working directory. (git clone <url> --no-checkout)

What I have tried:

Create a file named ignore.txt that contains:

/*
!/Tests_NR/

Then

> git sparse-checkout init
> type ignore.txt | git sparse-checkout set --stdin
> git sparse-checkout list
!/Tests_NR
*
> git checkout master

=> Did not work. The working dir only contains the top level files, and not any directory.


Update

Following @phd' s comment, this works for me in a Windows Powershell prompt:

> git sparse-checkout init
> git sparse-checkout set '/*' '!/Tests_NR/'
> git sparse-checkout list
/*
!/Tests_NR/

The following git checkout will effectively get everything except Tests_NR.

Be careful, that when used in a regular Windows cmd, this yields a different result and fails:

>git sparse-checkout init
>git sparse-checkout set '/*' '!/Tests_NR/'
>git sparse-checkout list
'/*'
'!/Tests_NR/'
1

There are 1 best solutions below

0
VonC On

You mentioned that type ignore.txt | git sparse-checkout set --stdin did not work.

It should now work, with Git 2.44 (Q1 2024): "git sparse-checkout"(man) used to set added default patterns even when the patterns are being fed from the standard input, which has been corrected.

See commit 53ded83 (20 Dec 2023) by Junio C Hamano (gitster).
(Merged by Junio C Hamano -- gitster -- in commit 863c596, 08 Jan 2024)

sparse-checkout: use default patterns for 'set' only !stdin

"git sparse-checkout set --no-cone"(man) uses default patterns when none is given from the command line, but it should do so ONLY when --stdin is not being used.

Right now, add_patterns_from_input() called when reading from the standard input is sloppy and does not check if there are extra command line parameters that the command will silently ignore, but that will change soon and not setting this unnecessary and unused default patterns start to matter when it gets fixed.