bash Git 日志表格格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3631005/
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
Git log tabular formatting
提问by takeshin
I have a simple alias to display few last commits:
我有一个简单的别名来显示一些最后的提交:
log --pretty=format:'%h %an %s' -10
How can I make the results to be displayed in columns, like this:
如何使结果显示在列中,如下所示:
898e8789 Author1 Commit message here
803e8759 Other Author name Commit message here
回答by Brian Campbell
In Git 1.8.3 and later, there is native support for this in the pretty format, using the %<(N)syntax to format the next placeholder as Ncolumns wide:
在 Git 1.8.3 及更高版本中,在漂亮格式中对此有本机支持,使用语法将下一个占位符格式化为N列宽:%<(N)
$ git log --pretty=format:'%h %<(20)%an %s' -10
For versions before 1.8.3, the previous version of this answer, retained below, should work.
对于 1.8.3 之前的版本,此答案的先前版本(保留在下面)应该可以工作。
Here's a solution in Bash using readto parse the log lines back apart, and printfto print them out, with a fixed width field for the authors name so the columns will stay lined up. It assumes that |will never appear in the author's name; you can choose another delimiter if you think that might be a problem.
这是 Bash 中的一个解决方案,read用于解析日志行,并将printf它们打印出来,作者姓名具有固定宽度的字段,因此列将保持对齐。它假定|永远不会出现在作者的名字中;如果您认为这可能有问题,您可以选择另一个分隔符。
git log --pretty=format:'%h|%an|%s' -10 |
while IFS='|' read hash author message
do
printf '%s %-20s %s\n' "$hash" "$author" "$message"
done
You can create this as an alias using:
您可以使用以下方法将其创建为别名:
[alias]
mylog = "!git log --pretty=format:'%h|%an|%s' -10 | while IFS='|' read hash author message; do printf '%s %-20s %s\n' \"$hash\" \"$author\" \"$message\"; done"
I'm sure you could do this in fewer characters using awkbut as the sayinggoes,
我相信你可以用更少的字符来做到这一点,awk但正如俗话所说,
Whenever faced with a problem, some people say “Lets use AWK.” Now, they have two problems.
每当遇到问题时,有人会说“让我们使用 AWK”。现在,他们有两个问题。
Of course, having said that, I had to figure out how to do it in awkwhich is a bit shorter:
当然,话虽如此,我不得不弄清楚如何做到这一点,awk其中有点短:
git ... | awk -F '|' '{ printf "%s %-20s %s\n", , , }'
回答by Jesse
You can also do:
你也可以这样做:
git log --pretty=format:'%h %<(20)%an %s' -10
No need for shell magic or post processing with awk, columnetc.
无需外壳魔法或后处理用awk,column等等。
回答by RubenLaguna
The following command will print the log in a tabular form
以下命令将以表格形式打印日志
git log --pretty=format:'%h|%an|%s' -10 | column -t -s '|'
the columncommand "columnates" the output, using the "|" as field separator, it will find out the optimal column width given the input, so it would work well even if you have more fields.
的列命令“columnates”的输出,采用了“|” 作为字段分隔符,它将找出给定输入的最佳列宽,因此即使您有更多字段,它也能很好地工作。
On Linux it will work ok as long as you don't use colors, the Linux implementation doesn't handle well the ANSI escape codes.
在 Linux 上,只要您不使用颜色,它就可以正常工作,Linux 实现不能很好地处理ANSI 转义码。
But in Mac OS X, it will handle colors and you can use any unicode character as field delimiter. I use ∑ since I'm pretty sure it won't occur by chance on the commit text. |is a bad choice because it could appear in the commit description and also if you use --graph --decorate --allit will appear as part of the symbols used to draw the graph
但在 Mac OS X 中,它将处理颜色,您可以使用任何 unicode 字符作为字段分隔符。我使用 ∑ 因为我很确定它不会偶然出现在提交文本中。|是一个糟糕的选择,因为它可能出现在提交描述中,而且如果您使用--graph --decorate --all它,它将作为用于绘制图形的符号的一部分出现
回答by insysion
In order to get truly tabular format you need to also truncate usernames that are longer than e.g. 20 characters:
为了获得真正的表格格式,您还需要截断超过 20 个字符的用户名:
git log --pretty=format:'%h %<(20,trunc)%an %s' -10
Additionally, if you're outputting to the terminal, you may want to prevent line overflows by either truncating the commit message field:
此外,如果您要输出到终端,您可能希望通过截断提交消息字段来防止行溢出:
git log --pretty=format:'%h %<(20,trunc)%an %<(39,trunc)%s' -10
or wrapping the lines and indenting any overflowing lines to align:
或环绕线条并缩进任何溢出的线条以对齐:
git log --pretty=format:'%w(79, 0, 29)%h %<(20,trunc)%an %s' -10
EDIT: The above example hard-codes the terminal width to be 79 characters. On POSIX systems you can use tput colsto return the width:
编辑:上面的示例将终端宽度硬编码为 79 个字符。在 POSIX 系统上,您可以使用tput cols返回宽度:
git log --pretty=format:'%w(`tput cols`, 0, 29)%h %<(20,trunc)%an %s' -10
or
或者
git log --pretty=format:'%w($((`tput cols`-1)), 0, 29)%h %<(20,trunc)%an %s' -10
Unfortunately these last two break git aliasso it will require a terminal alias (unless you really love typing).
不幸的是,这最后两个中断了,git alias所以它需要一个终端别名(除非你真的很喜欢打字)。
回答by jasonleonhard
How to add a tab?
如何添加标签?
What you are looking for is:
您正在寻找的是:
%x09
%x09
Here is an example
这是一个例子
git log --pretty=format:"%C(magenta)%h %C(cyan)%C(bold)%ad%Creset %C(cyan)%cr%Creset%x09 | %s %C(green)%Creset" --date=short
git log --pretty=format:"%C(magenta)%h %C(cyan)%C(bold)%ad%Creset %C(cyan)%cr%Creset%x09 | %s %C(green)%Creset" --date=short
I wanted the relative date to be tabbed bc it was not a fixed length, this way all columns line up (for anything less than a year ago). Wanted to make it line up all of the time, but this is as far as I got before I ran out of time (ironically).
我希望将相对日期标记为选项卡,因为它不是固定长度,这样所有列都排成一行(对于不到一年前的任何内容)。想让它一直排成一排,但这是我用完时间之前所能达到的(讽刺的是)。
Hope this helps!
希望这可以帮助!
If you have suggestions to improve this answer for the relative year case (not just grepping out the year) happy to hear them too, feel free to edit this answer to add them if you know how to do it.
如果您有建议来改进相对年份案例的此答案(不仅仅是找出年份)也很高兴听到它们,如果您知道如何做,请随时编辑此答案以添加它们。
回答by Yordan Georgiev
The problem with the tabular format is the fact that you might run out of space ...
表格格式的问题在于您可能会耗尽空间...
The following one is my personal best compromise between the essential stuff to display:
以下是我个人在要显示的基本内容之间的最佳折衷:
- who
- did what
- and when
- to be able to dig more by the commit hash for the
how
- WHO
- 做了什么
- 什么时候
- 能够通过提交哈希挖掘更多信息
how
so:
所以:
git log --pretty --format='%h %aI %<(15)%an ::: %s'
or
或者
git log --pretty --format='%h %cI %<(15)%an ::: %s'
if you want to get the commiter time and not the author time

