簡體   English   中英

git-log 中的顏色

[英]Color in git-log

當您運行git log --decorate --pretty=oneline時,output 將具有像(HEAD, refs/published/master, master)這樣的條目並帶有顏色。

我的 gitconfig 中還有以下內容:

[color "branch"]
    current = yellow reverse
    local = yellow
    remote = green

在執行如下自定義格式時,如何復制那些 colors?

git log --decorate --stat --graph --pretty=format:"%d %Cgreen%h%Creset (%ar - %Cred%an%Creset), %s%n"

從 git 1.8.3(2013 年 5 月 24 日)開始,您可以使用%C(auto)來裝飾git log格式字符串中的%d

發行說明

 * "git log --format" specifier learned %C(auto) token that tells Git
   to use color when interpolating %d (decoration), %h (short commit
   object name), etc. for terminal output.)

git log --decorate默認會放:

  • 青色的 HEAD
  • 紅色的遠程分支
  • 綠色的標簽

並且可以通過color.decorate配置進行更改。

但是git log --format不提供專門顯示HEAD遠程分支的方法:所有三個都通過%d顯示,可能有一種顏色。


2013 年 5 月更新,正如Elad Shahar (已投票)在下面提到的,git 1.8.3 提供了另一種選擇:

git log –format now sports a %C(auto) token that tells Git to use color when resolving %d (decoration), %h (short commit object name), etc. for terminal output.

This Atlassian blog post comments that this feature is part of several others focused on format ( git rebase , git count-objects ) and colors ( git branch -vv )

這是對先前auto,reset 1.8.2的補充,當 output 不用於終端時,它會自動禁用 colors

%C(auto,blue)Hello%C(auto,reset)

注意:git 2.4+(2015 年第二季度)將更好地重置分支名稱周圍的顏色。
請參閱Junio C Hamano ( gitster )提交 5ee8758

log --decorate :不要將“提交”顏色泄漏到下一個項目中

在“ git log --decorate ”中,您會看到提交 header 如下所示:

commit ... (HEAD, jc/decorate-leaky-separator-color)

其中“ commit... ( ”在color.diff.commit中繪制,“ HEAD ”在color.decorate.head中,“ , ”在color.diff.commit中,分支名稱在color.decorate.branch然后關閉“ ) " 在color.diff.commit中。

如果您想將 HEAD 和本地分支名稱繪制為與正文文本相同的顏色(可能是因為青色和綠色在黑白終端上太暗而無法閱讀),您不必說

[color "decorate"]
    head = black
    branch = black

因為您將無法在黑白終端上重復使用相同的配置。 你會天真地期待

[color "decorate"]
    head = normal
branch = normal

工作,但不幸的是它沒有。
它以與裝飾元素之間的左括號或逗號相同的顏色繪制字符串“ HEAD ”和分支名稱。
這是因為代碼在以自己的顏色打印“前綴”后忘記重置顏色。


請注意,git 2.5(2015 年第二季度)修復了一個錯誤:

請參閱Junio C Hamano ( gitster )提交 429ad20,2015年 5 月 13 日。
(由Junio C Hamano -- gitster --提交 fd70780中合並,2015 年 5 月 22 日)

log :不要過早縮短裝飾名稱

Git 2.4 中的“ log --decorate ”增強顯示當前分支頂端的提交,例如“ HEAD -> master ”,不適用於 --decorate=full。


Git 2.9.x+(2016 年第三季度)將修復另一個錯誤並%C(auto)授予color=auto


Git 2.10.2(2016 年 10 月)通過提交 82b83da (2016 年 9 月 29 日)和由René Scharfe (``)提交的 c99ad27 (2016 年 9 月 17 日)修復了其他錯誤。
(由Junio C Hamano -- gitster --提交 76796d4中合並,2016 年 10 月 28 日)

pretty :如果 output 為空,請避免為%C(auto)添加重置

我們發出一個轉義序列來重置%C(auto)的顏色和屬性,以確保自動着色按預期顯示。
如果 output strbuf 為空,即當%C(auto)出現在格式字符串的開頭時,請停止這樣做,因為不需要重置,我們在 output 中保存了幾個字節。

pretty : 讓%C(auto)重置所有屬性

重置 colors%C(auto)上的屬性以啟用對它們的全自動控制; 否則,諸如粗體或反向之類的屬性仍可能從以前的%C占位符中生效

將它們括起來:

%C(...): color specification, as described in color.branch.* config option

所以%C(yellow reverse)會起作用。

配置選項log.decorate可以啟用/禁用日志中的默認裝飾。

git config --global log.decorate full

完成后,您可以使用color.decorate.*來玩 colors

有些人可能想使用這個: %C(colorname)這不需要更改顏色配置。

示例:將作者姓名塗成黃色

--pretty=format:"%C(yellow)%an%Creset"

常規 ANSI colors 應該可以工作https://en.wikipedia.org/wiki/ANSI_escape_code

  • 黑色的
  • 紅色的
  • 綠色
  • 黃色
  • 藍色的
  • 品紅
  • 青色
  • 白色的

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM