2014-02-17 33 views
17

我有一个git日志别名,每个提交作为一行打印。由于有些人在提交日志中写了太长的一行,很多提交都换行。我如何格式化git日志输出以剪切50个字符后的注释?如何让git log减少长评论?

我在git-log手册页中发现了这个,但它只会填充短评论,而不是长篇评论。

%<(<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. 

回答

35

这不仅是所需要的字符在文档中说清楚但下面的示例将主题行切为50个字符:

git log --oneline --format="%h %<(50,trunc)%s" 

格式说明是%<和它的参数需要在括号内。在这种情况下,50个字符并截断多余。

例如,在msysGit仓库产量执行此:

C:\src\msysgit>git log -n 5 --format="%h [%<(12,trunc)%aN] [%<(12,trunc)%cN] %<(50,trunc)%s" 

218ed04 [Sebastian ..] [Sebastian ..] Merge pull request #154 from csware/tortoisegitp.. 
8a920b9 [Sven Stric..] [Sven Stric..] Installer: Detect TortoiseGitPlink from Tortoise.. 
448e125 [dscho  ] [dscho  ] Merge pull request #152 from csware/syscommand 
db8d1bf [Sven Stric..] [Sven Stric..] Perl readline creates empty sys$command files if.. 
753d3d6 [Johannes S..] [Johannes S..] Git for Windows 1.8.5.2-preview20131230 
+0

谢谢! “trunc”部分是我正在寻找的。 –

+0

谢谢 - 已经30分钟试图找出这个魔法 - 固定宽度:) – Gishu

+0

这不适合我。我有git --version => 1.7.10.4(在Debian 7.9 Wheezy中)。从http://git-scm.com/docs/git-log/1.8.3我收集这个功能是在1.8.3中添加的(即1.8.2.2的手册页没有提及它)。 – Ted

0

试试这个:

git log --pretty=oneline 

希望它能帮助。

+0

或者只是'git的日志--oneline' – Graeme

+3

他们不”用长评论解决我的问题,如果评论太长,他们仍然会换行。 –

1
git log --oneline 

将显示剥离提交头(有码)

git log --pretty=oneline 

将向你展示完整的提交头(有码)

+0

这适用于1.7 git,但不适用于2.2 git。我试过了。我认为@patthoyts'建议'--format'解决方案可能是您唯一的替代git 2.x. – fbicknel

+0

可能是,但这些正在1.9上工作。 – Bilal

0

(我似乎完全无法格式化评论适当所以已经张贴此作为一个答案,但真的是@ patthoyts的回应评论。 )

什么是可爱约trunc是它垫,所以你可以使用它像这样:
git log --pretty=format:"%ad %<(50,trunc)%s %h" --date=short --reverse
产生一个更简单的(至少我的眼睛)概述。

$ git log --pretty=format:"%ad %<(50,trunc)%s %h" --date=short --reverse
2015-06-15 initial commit 5099ede 2015-06-16 Layout - Responsive grid added. 6534242 2015-06-17 HTML - H1/Title updated <title>Testpage</title.. 88ea464 2015-06-18 Updating the Hotfix changes a8fbc47

提示 - 添加的,比方说,TRUNC一个alias,以方便自己。
git config --global alias.trunc 'log --pretty=format:"%ad %<(50,trunc)%s %h" --date=short --reverse'

3

迟到了,但这些选项将做到这一点:

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

或(例如)

$ echo $LESS 
-R 
$ export LESS=-RS