How to retrieve the hash for the current commit in Git?

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 turn arbitrary extended object reference into SHA-1, use simply git-rev-parse , for example

git rev-parse HEAD

or

git rev-parse --verify HEAD

Sidenote: If you want to turn references ( branches and tags ) into SHA-1, there is git show-ref and git for-each-ref .


If you only want the shortened hash:

git log --pretty=format:'%h' -n 1

Further, using %H is another way to get the long hash.


Another one, using git log:

git log -1 --format="%H"

It's very similar to the of @outofculture though a bit shorter.

链接地址: http://www.djcxy.com/p/8782.html

上一篇: 在撤销我的开发者证书之前需要指导

下一篇: 如何检索Git中当前提交的散列?