Reputation: 1276
With git, how can I get the tree hash of the staged changes? That is, what the tree hash of the commit (not the commit hash) would be if I committed the changes?
Upvotes: 5
Views: 1109
Reputation: 490098
The easiest way is to commit the tree:
git commit-tree
You don't have to make a complete commit—though of course that will also work. You just need the snapshot that the commit would have.
Fortunately, the way git commit
works is that it builds the commit in several stages. At one time, git commit
was a simple shell script, that ran these other more-basic Git commands:1
git write-tree
: this takes no arguments, and—if it succeeds—makes a tree object from whatever is in the index right now, and prints the hash ID to its standard output.
git commit-tree
: this takes several parameters (as many parent hash IDs as you choose, and one tree hash ID) and a commit message, and builds a commit object. The commit's snapshot is the tree whose hash ID you gave it, which comes from step 1. The command prints the hash ID of the new commit object to its standard output.
git update-ref
: this updates a reference, such as a branch name. It takes at least two arguments: the name to update, and the new value (or a flag to indicate "delete the name").
All you want from this is step #1.
1git update-ref
might be newer than the commit shell script, since in the bad old days, writing a ref just meant using an appropriate echo
command. The symbolic HEAD
ref was just a symbolic link. References were never packed. Once references got fancier and needed locking, git update-ref
became necessary.
Upvotes: 8