2014-02-17 91 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