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?
To get the full SHA:
To get the shortened version:
Here is another way of doing it with :)
Perhaps you want an alias so you don't have to remember all the nifty details. After doing one of the below steps, you will be able to simply type:
Following up on the accepted answer, here are two ways to set this up:
1) Teach git the explicit way by editing the global config (my original answer):
2) Or if you like a shortcut to teach git a shortcut, as recently commented by Adrien:
From here on, use
git lastcommit
to show the last commit's hash.in your home-dir in file ".gitconfig" add the following
then you will have an easier command to remember:
I needed something a little more different: display the full sha1 of the commit, but append an asterisk to the end if the working directory is not clean. Unless I wanted to use multiple commands, none of the options in the previous answers work.
Here is the one liner that does:
git describe --always --abbrev=0 --match "NOT A TAG" --dirty="*"
Result:
f5366ccb21588c0d7a5f7d9fa1d3f85e9f9d1ffe*
Explanation: describes (using annotated tags) the current commit, but only with tags containing "NOT A TAG". Since tags cannot have spaces, this never matches a tag and since we want to show a result
--always
, the command falls back displaying the full (--abbrev=0
) sha1 of the commit and it appends an asterisk if the working directory is--dirty
.If you don't want to append the asterisk, this works like all the other commands in the previous answers:
git describe --always --abbrev=0 --match "NOT A TAG"
Result:
f5366ccb21588c0d7a5f7d9fa1d3f85e9f9d1ffe
If you want the super-hacky way to do it:
Basically, git stores the location of HEAD in .git/HEAD, in the form
ref: {path from .git}
. This command reads that out, slices off the "ref: ", and reads out whatever file it pointed to.This, of course, will fail in detached-head mode, as HEAD won't be "ref:...", but the hash itself - but you know, I don't think you expect that much smarts in your bash one-liners. If you don't think semicolons are cheating, though...