用于显示 HEAD 提交 ID 的 Git 命令?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1967967/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 07:34:22  来源:igfitidea点击:

Git command to display HEAD commit id?

gitversion-control

提问by Andrew Arnott

What command can I use to print out the commit id of HEAD?

我可以使用什么命令打印出 HEAD 的提交 ID?

This is what I'm doing by hand:

这是我手工做的:

$ cat .git/HEAD
ref: refs/heads/v3.3
$ cat .git/refs/heads/v3.3
6050732e725c68b83c35c873ff8808dff1c406e1

But I need a script that can reliably pipe the output of some command to a text file such that the text file contains exactly the commit id of HEAD (nothing more or less, and not just a ref). Can anyone help?

但我需要一个脚本,它可以可靠地将某些命令的输出通过管道传输到文本文件,以便文本文件包含 HEAD 的提交 ID(不多也不少,而不仅仅是引用)。任何人都可以帮忙吗?

回答by Randal Schwartz

Use the command:

使用命令:

git rev-parse HEAD

For the short version:

对于简短版本:

git rev-parse --short HEAD

回答by cyb0k

git log -1

for only commit id

仅提交 ID

git log | head -n 1 

回答by Abhijit Mazumder

Old thread, still for future reference...:) even following works

旧线程,仍以供将来参考...... :) 甚至以下作品

git show-ref --head

by default HEAD is filtered out. Be careful about following though ; plural "heads" with a 's' at the end. The following command shows branches under "refs/heads"

默认情况下 HEAD 被过滤掉。不过要小心跟随;复数“heads”以“s”结尾。以下命令显示“refs/heads”下的分支

 git show-ref --heads

回答by JotaBe

You can specify git logoptions to show only the last commit, -1, and a format that includes only the commit ID, like this:

您可以指定git log选项以仅显示最后一次提交,-1以及仅包含提交 ID 的格式,如下所示:

git log -1 --format=%H

git log -1 --format=%H

If you prefer the shortened commit ID:

如果您更喜欢缩短的提交 ID:

git log -1 --format=%h

git log -1 --format=%h

回答by Ali Moreno

Play with Bash:

玩 Bash:

git show HEAD | sed -n 1p | cut -d " " -f 2

回答by Ilya Slyisarenko

According to https://git-scm.com/docs/git-log, for more pretty output in console you can use --decorateargument of git-logcommand:

根据 https://git-scm.com/docs/git-log,要在控制台中获得更漂亮的输出,您可以使用git-log命令的--decorate参数:

git log --pretty=oneline --decorate

will print:

将打印:

2a5ccd714972552064746e0fb9a7aed747e483c7 (HEAD -> master) New commit
fe00287269b07e2e44f25095748b86c5fc50a3ef (tag: v1.1-01) Commit 3
08ed8cceb27f4f5e5a168831d20a9d2fa5c91d8b (tag: v1.1, tag: v1.0-0.1) commit 1
116340f24354497af488fd63f4f5ad6286e176fc (tag: v1.0) second
52c1cdcb1988d638ec9e05a291e137912b56b3af test

回答by Avdhut Mankavale

git rev-parse --abbrev-ref HEAD

git rev-parse --abbrev-ref HEAD

回答by medmik

You can use this command

你可以使用这个命令

$ git rev-list HEAD

$ git rev-list HEAD

You can also use the headUnix command to show the latest nHEADcommits like

您还可以使用headUnix 命令来显示最新的nHEAD提交,例如

$ git rev-list HEAD | head - 2

$ git rev-list HEAD | head - 2