如何在 Git 中检索当前提交的哈希?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/949314/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to retrieve the hash for the current commit in Git?
提问by Sardaukar
I would like to retain (for now) the ability to link Git changesets to workitems stored in TFS.
我想(暂时)保留将 Git 变更集链接到存储在 TFS 中的工作项的能力。
I already wrote a tool (using a hook from Git) in which I can inject workitemidentifiers into the message of a Git changeset.
我已经写了一个工具(使用来自 Git 的钩子),我可以在其中将 workitemidentifiers 注入 Git 变更集的消息中。
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.
但是,我还想将 Git 提交(哈希)的标识符存储到自定义 TFS 工作项字段中。通过这种方式,我可以检查 TFS 中的工作项并查看哪些 Git 变更集与该工作项相关联。
How can I easily retrieve the hash from the current commit from Git?
如何轻松地从 Git 的当前提交中检索哈希?
回答by Jakub Nar?bski
To turn arbitrary extended object reference into SHA-1, use simply git-rev-parse, for example
要将任意扩展对象引用转换为 SHA-1,只需使用git-rev-parse,例如
git rev-parse HEAD
or
或者
git rev-parse --verify HEAD
Sidenote:If you want to turn references(branchesand tags) into SHA-1, there is git show-ref
and git for-each-ref
.
旁注:如果您想将引用(分支和标签)转换为 SHA-1,则有git show-ref
和git for-each-ref
。
回答by outofculture
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.
此外,使用 %H 是另一种获取长哈希的方法。
回答by Paul Pladijs
Another one, using git log:
另一个,使用 git log:
git log -1 --format="%H"
It's very similar to the of @outofculture though a bit shorter.
它与@outofculture 的非常相似,但要短一些。
回答by Alexander
To get the full SHA:
要获得完整的 SHA:
$ git rev-parse HEAD
cbf1b9a1be984a9f61b79a05f23b19f66d533537
To get the shortened version:
要获得缩短的版本:
$ git rev-parse --short HEAD
cbf1b9a
回答by Deestan
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 on master
. So you could just read it from there.
为了完整起见,因为还没有人提出建议。 .git/refs/heads/master
是一个只包含一行的文件:最新提交的哈希值master
。所以你可以从那里阅读它。
Or, as as command:
或者,作为命令:
cat .git/refs/heads/master
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
请注意,git 现在支持将一些 head refs 存储在 pack-ref 文件中,而不是作为 /refs/heads/ 文件夹中的文件。 https://www.kernel.org/pub/software/scm/git/docs/git-pack-refs.html
回答by John Tyree
There's always git describe
as well. By default it gives you --
也总是有的git describe
。默认情况下,它给你——
john@eleanor:/dev/shm/mpd/ncmpc/pkg (master)$ git describe --always
release-0.19-11-g7a68a75
回答by ecwpz91
回答by Robert Munteanu
Use git rev-list --max-count=1 HEAD
用 git rev-list --max-count=1 HEAD
回答by Henk
If you need to store the hash in a variable during a script, you can use
如果您需要在脚本期间将哈希存储在变量中,您可以使用
last_commit=$(git rev-parse HEAD)
Or, if you only want the first 10 characters (like github.com does)
或者,如果您只想要前 10 个字符(如 github.com 那样)
last_commit=$(git rev-parse HEAD | cut -c1-10)
回答by Fordi
If you want the super-hacky way to do it:
如果你想要超级hacky的方式来做到这一点:
cat .git/`cat .git/HEAD | cut -d \ -f 2`
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.
基本上,git 将 HEAD 的位置以 .git/HEAD 的形式存储在 .git/HEAD 中ref: {path from .git}
。这个命令读出它,切掉“ref:”,并读出它指向的任何文件。
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...
当然,这将在分离头模式下失败,因为 HEAD 不会是“ref:...”,而是哈希本身 - 但你知道,我认为你不会期望你的 bash 有那么多聪明之处-衬垫。如果你不认为分号在作弊,那么......
HASH="ref: HEAD"; while [[ $HASH == ref\:* ]]; do HASH="$(cat ".git/$(echo $HASH | cut -d \ -f 2)")"; done; echo $HASH