讓Git 多種顏色和自訂log格式輸出
Git多顏色輸出
Git預設的輸出是單一顏色的,不僅不夠美觀,也不容易閱讀。實際上,Git本身就支援用多種顏色來顯示其輸出的資訊,只需在命令列中運行以下命令來修改git的設定,即可開啟多顏色輸出:
git config --global color.status autogit config --global color.diff autogit config --global color.branch autogit 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>]]]) |
Ubuntu完美安裝搭建Git伺服器
GitHub 教程系列文章:
GitHub 使用教程圖文詳解
Git 標籤管理詳解
Git 分支管理詳解
Git 遠程倉庫詳解
Git 本地倉庫(Repository)詳解
Git 伺服器搭建與用戶端安裝
Git 概述
分享實用的GitHub 使用教程
Ubuntu下Git伺服器的搭建與使用指南
Git 的詳細介紹:請點這裡
Git 的:請點這裡
本文永久更新連結地址: