Reputation: 111856
Is there a good way to get a git hash that works as follows:
git commit -a -m ''
?Background:
I'm writing some development tools that package up builds. I'd like the build package to include some sort of reasonably-robust and completely automatic version id. A git SHA1 hash is more than sufficient for my purposes. Unfortunately, just using git rev-parse HEAD
isn't sufficient because users will commonly run the build before committing, and thus before HEAD
is updated.
Upvotes: 15
Views: 3078
Reputation: 55620
Create a temporary index, write the uncommitted changes to it, then get the sha of that tree.
cp .git/index /tmp/git_index
export GIT_INDEX_FILE=/tmp/git_index
git add -u
git write-tree
unset GIT_INDEX_FILE
This will print out a sha unique to the current state of the git repo including uncommitted changes, but not including new files.
Upvotes: 15
Reputation: 265141
I think you want the hash of a Git tree object, not that of a commit. A commit hash depends on the commit time and will be different every time you compute it.
To create a new tree object from your working copy you can run the write-tree
command:
git add -u && git write-tree && git reset
Note that this will mess up your index (as it will temporarily stage all changes)
Upvotes: 6