26

I enabled a sparse checkout

git init
git remote add <url>
git config --global core.sparseCheckout true
echo "assets" >> .git/info/sparse-checkout
git pull origin master

Git checked out only the assets folder for me

But now, I want to enable full branch checkout again (checkout folders/files), but it doesn't seem to work.

1) first I disabled config

git config --global core.sparseCheckout false

2) removed the entries in .git/info/sparse-checkout

But git didn't checkout any of the other folders/files for me, it seems it is sticking to 'assets' folder.

Could anyone tell me how can I reset/disable this sparse checkout without creating a new repo.?

iampolo
  • 363
  • 1
  • 4
  • 6

3 Answers3

30

Update with git 2.25+ (Q1 2020), as I mentioned in "Git sparse checkout with exclusion", you now have the git sparse-checkout command.

More precisely, as noted in Tao's answer:

git sparse-checkout disable

Disable the core.sparseCheckout config setting, and restore the working directory to include all files.
Leaves the sparse-checkout file intact so a later git sparse-checkout init command may return the working directory to the same state.


Original answer: 2016

You can see an example of "undoing" a sparse checkout in this script by Roscoe A. Bartlett:

git read-tree is important.

echo "Undoing sparse checkout"

# Get the full tree back
echo "*" > $SC_FILE
git config core.sparsecheckout true
git read-tree --reset -u HEAD

# Wipe out all traces of sparse checkout support
rm $SC_FILE
git config core.sparsecheckout false

The article "Adventures in Git - SparseCheckouts" by Rich Somerfield propose a similar option (also valid for submodules):

echo "/*" > .git/info/sparse-checkout
echo "/*" > .git/modules/<MODULEPATH>/info/sparse-checkout
git read-tree -mu HEAD
git config core.sparseCheckout false

braham-snyder adds in the comments that updating a .git/info/sparse-checkout (to checkout and track additional files) can be achieved with

git read-tree --dry-run HEAD
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • tested, this works fine to me. The key is to issue "git read-tree -mu" which I missed. – iampolo Mar 25 '16 at 04:15
  • 1
    FWIW: I arrived here because I merely wanted git to recognize my updates to my existing `.git/info/sparse-checkout` (to checkout and track additional files) -- running only `git read-tree --dry-run HEAD` appeared to achieve that – Braham Snyder Oct 01 '17 at 18:01
  • 1
    @braham-snyder Good point, thank you. I have included your comment in the answer for more visibility. – VonC Oct 01 '17 at 18:03
13

While VonC's answer is certainly correct and will help with the imminent problem, I feel the need to elaborate and explain the underlying issue.

Background

Git's sparse-checkout makes use of the skip-worktree bit, which basically tells git to consider the file in your working directory to be "up to date", regardless of the true state.

When using sparse-checkout git will apply this bit to all files which do not match the patterns described in your sparse-checkout file. When disabling sparse-checkout, or deleting the pattern file, this bits will still be set and the files won't return. You can read about it here.

As such you have to remove the skip-worktree bit manually from the files in question. The easiest approach certainly being the suggestions from VonC.

But why?

The reasoning behind this is quite simple. The skip-worktree bit is not exclusively used for sparse-checkout but it's rather a tool in git's toolkit. Other processes make use of the same bit, or a user might even use it own his own (personally I use it regularly to ignore changes to configuration files when debugging).


On a sidenote: You can actually get a list of the files which have been flagged with the skip-worktree bit, by using git ls-files -v. This will list all files under version control; the files with the skip-worktree bit are prefixed with a S.

If you only want to list the skip-worktree flagged files you can easily parse and grep the list with the following command: git ls-files -v | grep '^S' | cut -d' ' -f2.

Sascha Wolf
  • 18,810
  • 4
  • 51
  • 73
7

While it is still experimental as of git 2.27, there is now a command that handles this more transparently/intuitively:

git sparse-checkout disable

My understanding is that you shouldn't have to set core.sparseCheckout manually anymore, it should be enabled, configured and disabled using this new git sparse-checkout command.

https://git-scm.com/docs/git-sparse-checkout

Tao
  • 13,457
  • 7
  • 65
  • 76