How to generate a tree hash of a git sparse checkout?

55 views Asked by At

You can get the hash of a tree in a commit by doing:

git rev-parse 'HEAD^{tree}'

And you can get the hash of the tree in the index by doing:

git write-tree

However, in the case you did a sparse checkout by doing

git sparse-checkout init
git sparse-checkout set toto

Then running git write-tree will still print the hash of the full tree, including the files hidden by the sparse checkout. You can verify that by running git cat-file -p $(git write-tree), which will print all the files and directories at the top level.

How to generate the hash of a tree that includes only the files checked out by a sparse checkout?

1

There are 1 answers

1
Noé Rubinstein On

I found one trick that works, although I don't know what the performance would look like in big repositories: you can git rm all the paths from the index, before doing git add with only the paths that are checked out.

# Remove all files, including the ones not checked out
git rm -r --sparse --cached --quiet .
# Re-add all checked-out files
git add -u
# Then run git write-tree to get a hash
git write-tree