git log - 仅显示提交消息的前 x 个字符

git log - display only the first x characters of commit's message

我只想在 git log

中显示提交消息的有限数量的字符(比如前 100 个字符)

目前,我使用 git log --oneline 但这会显示消息的第一行。如果消息中的行与行之间没有换行符,那么这行可能会很长。这使我的 git 日志变得丑陋且不易阅读。

我该怎么做?

如果不能显示有限数量的字符,我可以显示消息的真正第一行吗,我的意思是如果它和消息中的第二行之间没有分隔符?

I want to display only a limited number of characters (say the first 100 characters) of the commit message in git log

查看 placeholders 可用于 --format。您对 %<(100) 感兴趣 — 它将长行剪切为给定的字符数;不幸的是,它在给定字符数的右边填充了短行,但这是你能找到的最好的。所以你需要

git log --format='%h %<(100)%s'

can I display the real first line of the message, I mean if there is no break between it and the second line in the message?

不,%s 占位符不是第一行,而是第一段,由两个换行符分隔。下次请使用关于如何编写好的提交消息的最佳实践:

https://chris.beams.io/posts/git-commit/#separate

您可以使用 %B 进行更复杂的处理并从中删除第一行。像这样:

git rev-parse master |
    while read sha1; do
        first_line=$(git --no-pager show -s --format='%B' | head -1)
        echo "$sha1 $first_line"
    done

在 macOS Monterey 版本 12.2.1 上使用 git 版本 2.32.0(Apple Git-132),我发现为了实际剪切到给定的字符数,我必须明确要求它截断输出。

像这样:

git log --format='%h %<(100,trunc)%s'

来自https://git-scm.com/docs/git-log#Documentation/git-log.txt-emltltNgttruncltruncmtruncem

%<(<N>[,trunc|ltrunc|mtrunc])

make the next placeholder take at least N columns, padding spaces on the right if necessary. Optionally truncate at the beginning (ltrunc), the middle (mtrunc) or the end (trunc) if the output is longer than N columns. Note that truncating only works correctly with N >= 2.

PS:我想将其作为现有答案的评论或编辑,但我没有足够的评论代表,而且它说编辑队列已满。无论如何,想补充 phd 所说的内容。