如何包装 git commit 评论?

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

How to wrap git commit comments?

git

提问by David

Is there a way to wrap git commit comments (when viewed via git log), so they are not cut off at the end of the line? It seems like there should be a pretty simple solution, but I haven't been able to find one.

有没有办法包装 git commit 注释(通过查看时git log),因此它们不会在行尾被切断?似乎应该有一个非常简单的解决方案,但我一直找不到。

Thanks.

谢谢。

采纳答案by VonC

Edit 2011: the otheranswers(upvoted) highlight the possibility to modify the options less, the default pager used by git.
The remark at the end of my answer still stands: even if you can see long commit message, that doesn't means that other tools having to deal with said (long) message will be able to process them.

编辑 2011:其他答案(已投票)突出了修改选项less的可能性,这是 git 使用的默认寻呼机。
我的回答末尾的评论仍然成立:即使您可以看到长提交消息,这并不意味着必须处理所述(长)消息的其他工具将能够处理它们。



Original answer (January 2010) about commit message format policy:

关于提交消息格式策略的原始答案(2010 年 1 月):

According to this blog, since git log does not do any kind of wrapping, you need to format your comment with an appropriate line length

根据此博客,由于 git log 不进行任何类型的包装,因此您需要使用适当的行长度格式化您的评论

  • git logdoesn't do any special special wrapping of the commit messages.
    With the default pager of less -S, this means your paragraphs flow far off the edge of the screen, making them difficult to read.
    On an 80 column terminal, if we subtract 4 columns for the indent on the left and 4 more for symmetry on the right, we're left with 72 columns.
  • git format-patch --stdoutconverts a series of commits to a series of emails, using the messages for the message body.
    Good email netiquette dictates we wrap our plain text emails such that there's room for a few levels of nested reply indicators without overflow in an 80 column terminal.
  • git log不对提交消息做任何特殊的特殊包装。
    使用默认分页器less -S,这意味着您的段落远离屏幕边缘,使它们难以阅读。
    在 80 列终端上,如果我们为左侧的缩进减去 4 列,并为右侧的对称减去 4 列,则剩下 72 列。
  • git format-patch --stdout使用消息正文的消息将一系列提交转换为一系列电子邮件。
    良好的电子邮件网络规范要求我们包装纯文本电子邮件,以便在 80 列终端中留出若干级别的嵌套回复指示符而不会溢出。

As said here:

正如这里所说

In general, use an editor to create your commit messages rather than passing them on the command line. The format should be:

  • A hard wrap at 72 characters
  • A single, short, summary of the commit
  • Followed by a single blank line
  • Followed by supporting details

通常,使用编辑器来创建提交消息,而不是在命令行上传递它们。格式应该是:

  • 72 个字符的硬包装
  • 提交的单个简短摘要
  • 后跟一个空行
  • 其次是支持细节


All sources (including GitPro book, which goes for 50 characters for the first line, as J?rg W Mittagcomments) insist on the necessity to wrap yourself the comment, certainly because, even if Git was able to deal with long lines, other tools in the processing chain (email, patches, ...) may not.

所有来源(包括GitPro book,第一行有 50 个字符,正如J?rg W Mittag评论的那样)都坚持有必要将评论包装起来,当然是因为,即使 Git 能够处理长行,其他处理链中的工具(电子邮件、补丁等)可能不会。

回答by Robert

Or you can change your pager to use less -R

或者您可以更改您的寻呼机以使用 less -R

$ git config --global core.pager 'less -R'

This will tell less to stop trying to control how the screen is formatted (you can normally scroll right and left during a git logusing arrow keys). And as the less manual says "Thus, various display problems may result, such as long lines being split in the wrong place." Which is what you want, you want the line ending to appear at the right of your screen (the wrong place) instead of where the comment author put it.

这将告诉 less 停止尝试控制屏幕的格式(您通常可以在git log使用箭头键期间左右滚动)。正如less手册所说的“因此,可能会导致各种显示问题,例如长行在错误的地方被拆分。” 这就是您想要的,您希望行尾出现在屏幕右侧(错误的位置)而不是评论作者放置的位置。

Also to note, pressing the right arrow keywithout modifying your pager, will let you see more of the code. Which is my preferred method.

另请注意,在不修改寻呼机的情况下按右箭头键,将使您看到更多代码。这是我的首选方法。

回答by Carl

There doesn't seem to be any perfect way. A workaround I use is just to pipe the output to more(or less, or catetc.):

似乎没有什么完美的方法。我使用的解决方法只是将输出通过管道传输到more(或less,或cat等):

git log | more

That wraps the long lines at least on my system (however, you miss the color formatting).

这至少在我的系统上包裹了长行(但是,您错过了颜色格式)。

回答by waldo

Mentioned in the previous answer is that the default pager (often 'less') is responsible for wrapping and by default it generally chops long lines.

上一个答案中提到的是默认寻呼机(通常是“less”)负责换行,并且默认情况下它通常会截断长行。

To modify this without changing your commit messages (less and bash example):

要在不更改提交消息的情况下修改它(less 和 bash 示例):

$ echo $LESS
-FRSX

This was what I had by default, now to overwrite the LESS environment variable.

这是我的默认设置,现在覆盖 LESS 环境变量。

echo "LESS=-FRX;export LESS" >> ~/.bash_profile
source ~/.bash_profile

回答by MatzeBraun

Note that less -r (as recommended above) leads to less forgetting its line count and you miss commits because your topmost lines will scroll out of sight! The real fix is disabling the -S option that git enables by default if the LESS environment variable is not set.

请注意, less -r (如上所述)会减少忘记其行数的情况,并且您会错过提交,因为最顶层的行会滚动到视线之外!如果未设置 LESS 环境变量,真正的解决方法是禁用 git 默认启用的 -S 选项。

A good fix is changing your git config in the following way:

一个好的解决方法是通过以下方式更改您的 git 配置:

git config --global core.pager 'less -+S'

回答by alexei

At least in git version 1.7.9.5, git logdoes support line wrapping. From git help log:

至少在 git 版本 1.7.9.5 中,git log确实支持换行。从 git 帮助日志:

 PRETTY FORMATS
   %w([<w>[,<i1>[,<i2>]]]): switch line wrapping

So, for example, the following wraps the long subjects at 72 columns:

因此,例如,以下将长主题包装在 72 列中:

alias gl='git log --format="%C(yellow)%h %an %ad%C(reset)%n%w(72,1,2)%s"'

(Agreed that commit formatting conventions should be followed instead of relying on this. However, this might prove useful until the day comes when everybody knows and respects the conventions.)

(同意应该遵循提交格式约定而不是依赖于此。然而,这可能证明有用,直到每个人都知道并尊重约定的那一天到来。)

回答by thomas iota

This has helped me.

这对我有帮助。

git --no-pager log WhateverBranch | head -n40

Typically the branch is large, so, piping it to head and using the -n switch lets you grab only the most recent 40 (or however many) lines of output that you need, and it should wrap (no need to scroll). Be aware this approach also lacks the color formatting.

通常分支很大,因此,通过管道将其连接到头部并使用 -n 开关可以让您仅获取您需要的最新 40 行(或多行)输出,并且它应该换行(无需滚动)。请注意,此方法也缺少颜色格式。

回答by sur

As VonC mentioned, you may want to wrap your commit messages to 72 characters and kill many birds with one stone. This git hook auto-wraps your commit messages and works with any editor: https://github.com/surabhigupta/AutoWrapSeventyTwo

正如 VonC 所提到的,您可能希望将提交消息包装为 72 个字符,并用一块石头杀死许多鸟。这个 git 钩子会自动包装你的提交信息并适用于任何编辑器:https: //github.com/surabhigupta/AutoWrapSeventyTwo

回答by Art Smith

Using this format made my life happier:

使用这种格式让我的生活更快乐:

log --pretty=format:\"%w(80,1,41)%h - %an, %ar : %s\"

Since the fields in the output ahead of the commit message totaled about 39 characters for most of my commits, it makes reading a lot easier.

由于对于我的大多数提交,提交消息之前输出中的字段总计约 39 个字符,因此阅读起来更容易。

回答by user4852948

Personal suggestion is be simple. When you want to see the full lines in the less pager just type -S this will change to folding the lines or back should you wish to view a section that way.

个人建议很简单。当您想在 less 寻呼机中查看完整行时,只需键入 -S,这将更改为折叠行,或者如果您希望以这种方式查看某个部分,则会更改为折叠行或向后折叠。