歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> 讓Git 多種顏色和自定義log格式輸出

讓Git 多種顏色和自定義log格式輸出

日期:2017/2/28 13:59:31   编辑:Linux教程

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>]]])

Ubuntu完美安裝搭建Git服務器 http://www.linuxidc.com/Linux/2015-07/120616.htm

GitHub 教程系列文章

GitHub 使用教程圖文詳解 http://www.linuxidc.com/Linux/2014-09/106230.htm

Git 標簽管理詳解 http://www.linuxidc.com/Linux/2014-09/106231.htm

Git 分支管理詳解 http://www.linuxidc.com/Linux/2014-09/106232.htm

Git 遠程倉庫詳解 http://www.linuxidc.com/Linux/2014-09/106233.htm

Git 本地倉庫(Repository)詳解 http://www.linuxidc.com/Linux/2014-09/106234.htm

Git 服務器搭建與客戶端安裝 http://www.linuxidc.com/Linux/2014-05/101830.htm

Git 概述 http://www.linuxidc.com/Linux/2014-05/101829.htm

分享實用的GitHub 使用教程 http://www.linuxidc.com/Linux/2014-04/100556.htm

Ubuntu下Git服務器的搭建與使用指南 http://www.linuxidc.com/Linux/2015-07/120617.htm

Git 的詳細介紹:請點這裡
Git 的下載地址:請點這裡

Copyright © Linux教程網 All Rights Reserved