git多顏色輸出
git默認的輸出是單一顏色的,不僅不夠美觀,也不容易閱讀。實際上,git本身就支持用多種顏色來顯示其輸出的信息,只需在命令行中運行以下命令來修改git的設置,即可開啟多顏色輸出:
git config --global color.status auto
git config --global color.diff auto
git config --global color.branch auto
git config --global color.interactive auto
執行以上命令后,git的status, diff和branch等諸命令的輸出就都是帶有顏色的了。見下圖示例。

自定義log格式
完成上述步驟后,git log 命令的輸出雖然有了點顏色,但還是顯得枯燥(見下圖)。

不要緊,強大的git提供了自定義log格式的功能,嘗試輸入以下命令:
git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
你將看到類似下圖的輸出:

怎么樣,不賴吧?不過,每次查看log都輸出這么一長串的命令,實在是不太現實。咱們來通過git的命令別名來解決這個問題。輸入以下命令:
git config --global alias.lg "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset'"
上述命令將創建一個命令別名 lg,每次你使用命令 git lg 就相當於輸入了剛才那一長串命令。現在,如果想看美觀的多顏色輸出,就使用 git lg,如果想看普通的log輸出,就使用git log,二者互不干擾。
如果你想讓log輸出某些特定的信息,可以自己調整 --pretty 參數的值,例如下面的命令將只顯示commit的hash,提交時間,提交者姓名:
git log --pretty=format:'%h %ar %an'
把format后面單引號中的內容替換為你想要的格式,即可實現自定義的log輸出格式。這里的%h, %ar等是一些git預定義的占位符,完整的列表如下:
| %H | commit hash |
| %h | commit的短hash |
| %T | tree hash |
| %t | tree的短hash |
| %P | parent hashes |
| %p | parent的短hashes |
| %an | 作者名字 |
| %aN | mailmap中對應的作者名字 (.mailmap對應,詳情參照git-shortlog(1)或者git-blame(1)) |
| %ae | 作者郵箱 |
| %aE | 作者郵箱 (.mailmap對應,詳情參照git-shortlog(1)或者git-blame(1)) |
| %ad | 日期 (–date= 制定的格式) |
| %aD | 日期, RFC2822格式 |
| %ar | 日期, 相對格式(1 day ago) |
| %at | 日期, UNIX timestamp |
| %ai | 日期, ISO 8601 格式 |
| %cn | 提交者名字 |
| %cN | 提交者名字 (.mailmap對應,詳情參照git-shortlog(1)或者git-blame(1)) |
| %ce | 提交者 email |
| %cE | 提交者 email (.mailmap對應,詳情參照git-shortlog(1)或者git-blame(1)) |
| %cd | 提交日期 (–date= 制定的格式) |
| %cD | 提交日期, RFC2822格式 |
| %cr | 提交日期, 相對格式(1 day ago) |
| %ct | 提交日期, UNIX timestamp |
| %ci | 提交日期, ISO 8601 格式 |
| %d | ref名稱 |
| %e | encoding |
| %s | commit信息標題 |
| %f | 過濾commit信息的標題使之可以作為文件名 |
| %b | commit信息內容 |
| %N | commit notes |
| %gD | reflog selector, e.g., refs/stash@{1} |
| %gd | shortened reflog selector, e.g., stash@{1} |
| %gs | reflog subject |
| %Cred | 切換到紅色 |
| %Cgreen | 切換到綠色 |
| %Cblue | 切換到藍色 |
| %Creset | 重設顏色 |
| %C(…) | 制定顏色, as described in color.branch.* config option |
| %m | left, right or boundary mark |
| %n | 換行 |
| %% | 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). |
