git 日志仅显示一个提交 ID

git log show one commit id only

我需要一些帮助。可以只显示一个 commit id 吗?由于 git log -3 显示 1 - 3 的日志,我只想显示 3。可能的命令会匹配它吗?

我用命令

       git log -3 --pretty=format:"%h"

结果是

       ffbef87
       cf0e073
       1c76c5d

我只想显示 1c76c5d

您可以使用当前提交中的 git show referencing the third parent(即来自 HEAD 的第二个祖先)。此外,git show 接受与 git log:

相同的格式字符串
git show HEAD~2 --pretty=format:"%h" --no-patch

更新 (2016-12-01)

更好的方法是使用 rev-parse plumbing command--short 选项来输出缩写的(7 个字符)提交 SHA-1:

git rev-parse --short HEAD~2

或者您也可以指定提交 SHA-1 的确切长度:

git rev-parse --short=4 HEAD~2

有一个工具:

git log -3 --pretty=format:"%h" | tail -n 1

您可以使用以下标志包含哈希的 n 个字符(而不是默认值):

--abbrev=n 

Unix 哲学的相关文章

1) Make each program do one thing well. To do a new job, build afresh rather than complicate old programs by adding new "features".

2) Expect the output of every program to become the input to another, as yet unknown, program. Don't clutter output with extraneous information. Avoid stringently columnar or binary input formats. Don't insist on interactive input.

... [i.e.]

  • Write programs that do one thing and do it well.
  • Write programs to work together.

https://en.wikipedia.org/wiki/Unix_philosophy

从至少 git 版本 2.3.8 开始,您可以使用 --skip 选项:

   git log -1 --skip 2 --pretty=format:"%h"

不确定 git 的哪个早期版本支持 --skip