【问题标题】:Show full Git commit message in console在控制台中显示完整的 Git 提交消息
【发布时间】:2012-02-08 03:20:09
【问题描述】:

我正在尝试在控制台中输出完整的提交消息,并且我能够收到该消息,但是为了看到完整的消息,我必须不断调整控制台窗口的大小以显示更多信息。我在使用 Cygwin 的 Windows 上。

我使用的命令是git log --pretty=full

【问题讨论】:

  • 我在文档页面中找到了这个,但我不知道如何实现它 -w[[,[,]]] Linewrap 输出通过换行每条线的宽度。每个条目的第一行由 indent1 空格缩进,第二行和后续行由 indent2 空格缩进。 width、indent1 和 indent2 分别默认为 76、6 和 9。 link
  • 您引用了git-shortlog 的手册页,它确实 支持换行(但它只显示一行提交消息)。
  • @Sidnious 你是对的,这是来自日志页面的文本 %w([[,[,]]]): switch line wrapping ,就像 git-shortlog(1) 的 -w 选项一样。
  • 啊,好的。当您为提交消息指定自定义格式字符串时,您可以使用它,但您不能将它与--pretty 格式之一一起使用。
  • 另请参阅git log --no-expand-tabs --pretty=full my answer below 中的 git 2.9(2016 年 6 月)

标签: git logging commit


【解决方案1】:

寻呼机来救援

git log | less

确保您没有 -S 的别名为 less

此外,通常认为限制提交消息的宽度是一种很好的做法。我相信一个通用标准是 78 个字符 (IIRC),大多数文本编辑器在确保此类样式规则方面做得很好(自动格式化您的消息)。

更新:作为参考数据点,git-config lists

gui.commitmsgwidth

   Defines how wide the commit message window is in the git-gui(1). "75" 
   is the default.

【讨论】:

  • which lesstype less 怎么说?
  • 不确定我是否理解,但git log | less 输出了提交哈希\n 作者\n 日期\n\n 提交消息(未包装)
  • 无论如何,Git 应该默认生成寻呼机,因为git log 产生的输出长于终端高度。此外,即使您确实打开了 -S,以便“截断”行,您仍然可以通过按左/右箭头看到它们。
  • @Jefromi:我的猜测:默认寻呼机未触发(行数太少)或已配置为使用 -S
  • @sehe:是的,默认情况下它会包含 -S,因为在差异中包装代码有点疯狂。听起来真正的问题是提交消息太宽了。
【解决方案2】:

你也可以使用

git log --format=<format> [hash|HEAD]

其中&lt;format&gt; 可以是以下之一:

占位符是:

# see man git-log PRETTY FORMATS section
%H: commit hash
%h: abbreviated commit hash
%T: tree hash
%t: abbreviated tree hash
%P: parent hashes
%p: abbreviated parent hashes
%an: author name
%aN: author name (respecting .mailmap, see git-shortlog(1) or git-blame(1))
%ae: author email
%aE: author email (respecting .mailmap, see git-shortlog(1) or git-blame(1))
%ad: author date (format respects --date= option)
%aD: author date, RFC2822 style
%ar: author date, relative
%at: author date, UNIX timestamp
%ai: author date, ISO 8601-like format
%aI: author date, strict ISO 8601 format
%cn: committer name
%cN: committer name (respecting .mailmap, see git-shortlog(1) or git-blame(1))
%ce: committer email
%cE: committer email (respecting .mailmap, see git-shortlog(1) or git-blame(1))
%cd: committer date (format respects --date= option)
%cD: committer date, RFC2822 style
%cr: committer date, relative
%ct: committer date, UNIX timestamp
%ci: committer date, ISO 8601-like format
%cI: committer date, strict ISO 8601 format
%d: ref names, like the --decorate option of git-log(1)
%D: ref names without the " (", ")" wrapping.
%e: encoding
%s: subject
%f: sanitized subject line, suitable for a filename
%b: body
%B: raw body (unwrapped subject and body)
%N: commit notes
%GG: raw verification message from GPG for a signed commit
%G?: show "G" for a good (valid) signature, "B" for a bad signature, "U" for a good signature with unknown validity, "X" for a good signature that has expired, "Y" for a good signature made by an expired key, "R"
           for a good signature made by a revoked key, "E" if the signature cannot be checked (e.g. missing key) and "N" for no signature
%GS: show the name of the signer for a signed commit
%GK: show the key used to sign a signed commit
%gD: reflog selector, e.g., refs/stash@{1} or refs/stash@{2 minutes ago}; the format follows the rules described for the -g option. The portion before the @ is the refname as given on the command line (so git log
           -g refs/heads/master would yield refs/heads/master@{0}).
%gd: shortened reflog selector; same as %gD, but the refname portion is shortened for human readability (so refs/heads/master becomes just master).
%gn: reflog identity name
%gN: reflog identity name (respecting .mailmap, see git-shortlog(1) or git-blame(1))
%ge: reflog identity email
%gE: reflog identity email (respecting .mailmap, see git-shortlog(1) or git-blame(1))
%gs: reflog subject
%Cred: switch color to red
%Cgreen: switch color to green
%Cblue: switch color to blue
%Creset: reset color
%C(...): color specification, as described under Values in the "CONFIGURATION FILE" section of git-config(1); adding auto, at the beginning will emit color only when colors are enabled for log output (by
           color.diff, color.ui, or --color, and respecting the auto settings of the former if we are going to a terminal).  auto alone (i.e.  %C(auto)) will turn on auto coloring on the next placeholders until the color is
           switched again.
%m: left (<), right (>) or boundary (-) mark
%n: newline
%%: a raw %
%x00: print a byte from a hex code
%w([<w>[,<i1>[,<i2>]]]): switch line wrapping, like the -w option of git-shortlog(1).
%<(<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.
%<|(<N>): make the next placeholder take at least until Nth columns, padding spaces on the right if necessary
%>(<N>), %>|(<N>): similar to %<(<N>), %<|(<N>) respectively, but padding spaces on the left
%>>(<N>), %>>|(<N>): similar to %>(<N>), %>|(<N>) respectively, except that if the next placeholder takes more spaces than given and there are spaces on its left, use those spaces
%><(<N>), %><|(<N>): similar to % <(<N>), %<|(<N>) respectively, but padding both sides (i.e. the text is centered) -%(trailers): display the trailers of the body as interpreted by git-interpret-trailers(1)

这使您可以更好地控制要提取的内容。例如,在我的用例中,我对实际的提交消息感兴趣,以便我可以运行提交后挂钩。

# get the raw body of the commit
git log --format=%B HEAD

【讨论】:

  • --format=%B--format="%s%n%n%b" 好得多。谢谢!
【解决方案3】:

我用

git lg | fold --width=1000

lg 在 .gitconfig 中定义如下

[alias]
    lg = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative

看起来是这样的:

【讨论】:

    【解决方案4】:

    您只需要禁用寻呼机。

    git --no-pager log
    

    这是我从How do I export a git log to a text file?得到的

    【讨论】:

    • 这是关键。请注意,重定向git log 的输出时会自动禁用寻呼机。因此git log &gt; /tmp/commit.md 到文件,git log | cat 到屏幕,甚至git log | \less 没有或自定义选项。
    【解决方案5】:

    git log 不支持包装提交消息,因此通常的做法是将提交消息包装到大约 72 个字符。更多讨论请见this answer

    不过,您应该可以使用箭头键左右滚动来查看该行的其余部分。可以吗?


    FWIW,我提议对 Git 进行更改,允许 log 等来包装提交消息,如果您没有任何其他需要提前包装它们的话。在 git 邮件列表中查看 herehere 以了解它是否可以发送到任何地方。

    【讨论】:

      【解决方案6】:

      使用git log --pretty=(medium,full,fuller)(意味着不使用pretty=format)时查看更多信息的另一个选项是能够删除在每条日志消息开头添加的空格缩进(4 个空格)(git 2.9,2016 年 6 月):

      请参阅commit fe37a9ccommit 0893eec(2016 年 3 月 29 日)Junio C Hamano (gitster)
      请参阅 Linus Torvalds (torvalds)commit 7cc13c7(2016 年 3 月 16 日)。
      (由 Junio C Hamano -- gitster -- 合并于 commit cafef3d,2016 年 4 月 13 日)

      pretty:默认为选定的漂亮格式启用--expand-tabs

      git log --pretty={medium,full,fuller}”和“git log”默认 在日志消息前添加 4 个空格,因此启用是有意义的 这些格式默认使用新的“expand-tabs”工具。
      添加--no-expand-tabs 选项以覆盖新的默认值。

      doc now reads:

      --expand-tabs=<n>:
      --expand-tabs:
      --no-expand-tabs:
      

      在输出中显示之前,在日志消息中执行选项卡扩展(用足够的空格替换每个选项卡以填充下一个显示列,即“&lt;n&gt;”的倍数)。
      --expand-tabs--expand-tabs=8 的简写,
      --no-expand-tabs--expand-tabs=0 的简写,它禁用标签扩展。

      【讨论】:

        【解决方案7】:

        如果您需要查看单个提交使用的完整多行消息

        git show --format=full [hash|HEAD]
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-12-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-03-19
          • 1970-01-01
          相关资源
          最近更新 更多