I would like to retain (for now) the ability to link Git changesets to workitems stored in TFS.
I already wrote a tool (using a hook from Git) in which I can inject workitemidentifiers into the message of a Git changeset.
However, I would also like to store the identifier of the Git commit (the hash) into a custom TFS workitem field. This way I can examine a workitem in TFS and see what Git changesets are associated with the workitem.
How can I easily retrieve the hash from the current commit from Git?
If you only want the shortened hash:
Further, using %H is another way to get the long hash.
There's always
git describe
as well. By default it gives you --For completeness, since no-one has suggested it yet.
.git/refs/heads/master
is a file that contains only one line: the hash of the latest commit onmaster
. So you could just read it from there.Or, as as command:
Update:
Note that git now supports storing some head refs in the pack-ref file instead of as a file in the /refs/heads/ folder. https://www.kernel.org/pub/software/scm/git/docs/git-pack-refs.html
Commit hash
Abbreviated commit hash
Click here for more
git show
examples.The most succinct way I know:
If you want a specific number of digits of the hash you can add:
Here is another direct-access implementation:
This also works over http which is useful for local package archives (I know: for public web sites it's not recommended to make the .git directory accessable):