1

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?

asked Nov 8, 2023 at 14:28

1 Answer 1

1

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
answered Nov 8, 2023 at 14:31
Sign up to request clarification or add additional context in comments.

1 Comment

I honestly think something like that is your only way, because git doesn't even have an internal tree object that's "cut down" to the sparse checkout until you create it, since it's only the checkout (i.e. the working directory) that's sparse and not anything else about the repo.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.