Sparse checkout but exclude or ignore files in root?

2k views Asked by At

The docs for git sparse-checkout state,

By default, when running git sparse-checkout init, the root directory is added as a parent pattern. At this point, the sparse-checkout file contains the following patterns:

/*
!/*/

However, I do not want to include the root in sparse-checkout I want something like,

/directory_i_want
!/*
!/*/

But that doesn't work, returning instead

warning: unrecognized pattern: /*

warning: disabling cone pattern matching

Such that the sparse-checkout only pulls the one directory at the top, and not ever file in the repository's root. How can I achieve this?

1

There are 1 answers

0
Marco Luzzara On

You are seeing that because you have enabled sparseCheckoutCone. From the reference:

The accepted patterns in the cone pattern set are:

  1. Recursive: All paths inside a directory are included.

  2. Parent: All files immediately inside a directory are included.

In addition to the above two patterns, we also expect that all files in the root directory are included.

However it is not automatically enabled, so I wonder why you have it set. Anyway, you clearly cannot use it since you are excluding pretty everything except for directory_i_want. First of all, disable that option:

git config core.sparseCheckoutCone 'false'

Next, restore the initial repo:

git sparse-checkout disable

And in the end, choose the pattern you need with

git sparse-checkout set directory_i_want/

If you now look at the file $GIT_DIR/info/sparse-checkout, you will see a very simple pattern (directory_i_want/) and that is enough. Consider that with the sparse-checkout you tried, the order is important: you cannot put directory_i_want at the start and then negate it with !/*/. It would result in an error saying something like:

error: Sparse checkout leaves no entry on working directory