I'd like to get a git hash of the current repo state with some changes present but without actually committing anything. In other words, is there a way to know in advance what the hash would look like if the changes got commited at some point later? We can assume that the commit messages are going to be the same.
-
1Why not commit the changes? Relevant https://stackoverflow.com/questions/35430584/how-is-the-git-hash-calculated? – evolutionxbox Mar 24 '21 at 21:28
-
@evolutionxbox If that were an option, I wouldn't post on SO :) – Petras Purlys Mar 25 '21 at 11:26
1 Answers
In general, this is not possible, because the commit object contains both an author and committer identifier and timestamp. As a consequence, the hash of the object is dependent on the time it was created and who created it, so there are many possible commit hashes depending on when the commit actually gets created.
If the commit is being signed, the signature will also create a timestamp, and it's also possible that the signature may contain random data, depending on the algorithm and version of GnuPG, so the actual value may end up being unknowable ahead of time.
If you want to know the root tree hash, then that's easier. You can add the desired changes to the index and run git write-tree
. That will create a tree object and print its hash to standard output, but it's otherwise unreferenced, so if you never actually end up using it, it'll be pruned eventually. You can even set GIT_INDEX_FILE
to a temporary file and then run git add
to add to a temporary index and then run git write-tree
, which will use that temporary index file.
If you really want to know without creating any objects in the repository, then you'll need to use something like libgit2 to do that.

- 64,793
- 6
- 84
- 100