git Git漂亮的格式颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15458237/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 15:46:23  来源:igfitidea点击:

Git pretty format colors

git

提问by James Allman

I'm trying to set up pretty format colors for Git. From what I can tell version 1.6.0 only recognizes red, green and blue.

我正在尝试为 Git 设置漂亮的格式颜色。据我所知,1.6.0 版只能识别红色、绿色和蓝色。

$ git log --pretty=format:"%Credred%Creset %Cgreengreen%Creset %Cyellowyellow%Creset %Cblueblue%Creset %Cmagentamagenta%Creset %Ccyancyan%Creset %Cwhitewhite%Creset"
red green %Cyellowyellow%Creset blue %Cmagentamagenta %Ccyancyan %Cwhitewhite

In addition none of the colors work with the parenthesized color format.

此外,没有任何颜色适用于带括号的颜色格式。

Is there a way to list the available pretty format colors for Git?

有没有办法列出 Git 可用的漂亮格式颜色?

Unfortunately this is on a legacy SCO OpenServer 5.0.7 machine and the last version of Git released by SCO Skunkworks was 1.6.0.3.

不幸的是,这是在旧的 SCO OpenServer 5.0.7 机器上,SCO Skunkworks 发布的最新 Git 版本是 1.6.0.3。

回答by Tuxdude

I do not have an old version of gitto verify that the colors other than red, blueand greenare supported.

我没有旧版本来git验证red,blue和以外的颜色green是否受支持。

Although, one thing I noticed even with the recent versions of git(like 1.7.10 I used) is that colors other than red, greenand blueneed to be within parentheses (). For red, greenand blue, the parentheses are optional.

虽然,即使使用最新版本git(如我使用的 1.7.10),我也注意到的一件事是 , 以外的颜色redgreen并且blue需要在括号内()。对于red,greenblue,括号是可选的。

So give this a try:

所以试试这个:

git log --pretty=format:"%Credred%Creset %Cgreengreen%Creset %C(Yellow)yellow%Creset %Cblueblue%Creset %C(magenta)magenta%Creset %C(cyan)cyan%Creset %C(white)white%Creset"

The list of colorsI'm aware of at least are:

颜色列表我知道的至少有:

normal
black
red
green
yellow
blue
magenta
cyan
white

It can be combined with one of these attributes:

它可以与以下属性之一结合使用:

bold
dim
ul
blink
reverse
italic
strike
bright  # (Git 2.26, Q1 2020, example: brightred)

If you're trying to change colors using .gitconfigyou should be able to specify two colors - foregroundand backgroundand you can combine it with an attribute.

如果您尝试使用更改颜色,.gitconfig您应该能够指定两种颜色 -前景背景,您可以将其与属性结合使用。

回答by VonC

Git 2.3.0 (February 2015) will allow (thanks to Jeff Kink (peff)):

Git 2.3.0(2015 年 2 月)将允许(感谢Jeff Kink ( peff)):



parse_color: support 24-bit RGB values

Some terminals (like XTerm) allow full 24-bit RGB color specifications using an extension to the regular ANSI color scheme.
Let's allow users to specify hex RGB colors, enabling the all-important feature of hot pink ref decorations:

parse_color: 支持 24 位 RGB 值

某些终端(如 XTerm)允许使用对常规 ANSI 配色方案的扩展的完整 24 位 RGB 颜色规范。
让我们允许用户指定十六进制 RGB 颜色,启用粉红色参考装饰的所有重要功能

git log --format="%h%C(#ff69b4)%d%C(reset) %s"


  • a better management of color attributes:
  • 更好地管理颜色属性:

parse_color: recognize "no$foo" to clear the $fooattribute

You can turn on ANSI text attributes like "reverse" by putting "reverse" in your color spec. However, you cannot ask to turn reverse off.

For common cases, this does not matter. You would turn on "reverse" at the start of a colored section, and then clear all attributes with a "reset".

However, you may wish to turn on some attributes, then selectively disable others. For example:

parse_color: 识别“ no$foo”来清除$foo属性

您可以通过在颜色规范中放置“reverse”来打开“reverse”等 ANSI 文本属性。但是,您不能要求关闭倒车。

对于常见情况,这无关紧要。您可以在彩色部分的开头打开“反向”,然后使用“重置”清除所有属性。

但是,您可能希望打开某些属性,然后有选择地禁用其他属性。例如:

git log --format="%C(bold ul yellow)%h%C(noul) %s"

underlines just the hash, but without the need to re-specify the rest of the attributes.

This can also help third-party programs, like contrib/diff-highlight, that want to turn some attribute on/off without disrupting existing coloring.

Note that some attribute specifications are probably nonsensical (e.g., "bold nobold"). We do not bother to flag such constructs, and instead let the terminal sort it out.

只强调散列,但无需重新指定其余属性。

这也可以帮助contrib/diff-highlight想要打开/关闭某些属性而不破坏现有着色的第三方程序,例如。

请注意,某些属性规范可能是无意义的(例如,“ bold nobold”)。我们不会费心标记此类构造,而是让终端对其进行整理。



With Git 2.26 (Q1 2020), the basic 7 colors learned the brighter counterparts (e.g. "brightred").

在 Git 2.26(2020 年第一季度)中,基本的 7 种颜色学会了更亮的对应颜色(例如“ brightred”)

See commit c444f03, commit 1751b09, commit 4a28eb0(21 Jan 2020) by Eyal Soha (``).
(Merged by Junio C Hamano -- gitster--in commit 87f17d7, 25 Feb 2020)

请参阅Eyal Soha (``) 的commit c444f03commit 1751b09commit 4a28eb0(2020 年 1 月 21 日
(由Junio C gitsterHamano合并-- --87f17d7 提交中,2020 年 2 月 25 日)

color.c: support bright aixterm colors

Signed-off-by: Eyal Soha

These colors are the bright variants of the 3-bit colors.

Instead of 30-37 range for the foreground and 40-47 range for the background, they live in 90-97 and 100-107 range, respectively.

color.c: 支持明亮的 aixterm 颜色

签字人:Eyal Soha

这些颜色是 3 位颜色的明亮变体。

它们分别位于 90-97 和 100-107 范围内,而不是前景的 30-37 范围和背景的 40-47 范围。

The git configdocumentationnow includes:

git config文档现在包括:

The basic colors accepted are normal, black, red, green, yellow, blue, magenta, cyanand white.
The first color given is the foreground; the second is the background.

接受基本的颜色normalblackredgreenyellowbluemagentacyanwhite
给出的第一种颜色是前景;二是背景。

All the basic colors except normalhave a bright variant that can be specified by prefixing the color with bright, like brightred.

除了normal有一个明亮的变体外,所有的基本颜色都可以通过在颜色前加上 来指定bright,比如brightred

回答by Xiaofei HAN

Share my git log format:

分享我的 git 日志格式:

$ git log --graph --pretty=format:'%C(magenta)%h%Creset -%C(red)%d%Creset %s %C(dim green)(%cr) %C(cyan)<%an>%Creset' --abbrev-commit

You can also add an alias into ~/.gitconfig:

您还可以在~/.gitconfig 中添加别名:

[alias]
    logs  = log --graph --pretty=format:'%C(magenta)%h%Creset -%C(red)%d%Creset %s %C(dim green)(%cr) %C(cyan)<%an>%Creset' --abbrev-commit

then you can use alias as you define in ~/.gitconfig.

那么你可以使用你在中定义的别名~/.gitconfig

$ git logs

回答by daniela-idara

I am using Ubuntu 18.04 and I was able to use Xiaofei HAN's suggestions.

我使用的是 Ubuntu 18.04,我可以使用 Xiaofei HAN 的建议。

For beginner trying to complete this, I had the best luck by editing the .gitconfig.

对于尝试完成此操作的初学者,我通过编辑 .gitconfig 获得了最好的运气。

nano ~/.gitconfig

paste [alias] block from above under the existing text

在现有文本下从上方粘贴 [别名] 块

CTRL + O (to write out) + CTRL + X (to close nano)

CTRL + O(写出)+ CTRL + X(关闭nano)

After doing this, to see the pretty colors you can use

完成此操作后,要查看可以使用的漂亮颜色

git logs

using

使用

'git log' will get you the view without the colors,

'git log' 会让你看到没有颜色的视图,

same for 'git logs --oneline', except that one will show the flourish on the side

'git logs --oneline' 也一样,除了一个会在侧面显示繁荣

thank you, i hope this helps someone

谢谢,我希望这对某人有所帮助

回答by ElMatador

Here are two aliases that I wrote once I understood the syntax of using custom hexadecimal color values as shown in @VonC 's answer.

这是我在理解使用自定义十六进制颜色值的语法后编写的两个别名,如@VonC 的回答所示。

There are four versions, the only difference is the --graph directive and the 'f' versions will also show you files that were added, deleted or modified:

有四个版本,唯一的区别是 --graph 指令,'f' 版本还会显示添加、删除或修改的文件:

[alias]
    lg = log --graph --pretty=format:"%C(#cd9a00)%h\%C(#0080ff)\ <%an>\ %C(#17b062)(%cr)\ %d\%C(#c0d6de)%s"    
    l1 = log --pretty=format:"%C(#cd9a00)%h\%C(#0080ff)\ <%an>\ %C(#17b062)(%cr)\ %d\%C(#c0d6de)%s"    
    lgf = log --name-status --graph --pretty=format:"%C(#cd9a00)%h\%C(#0080ff)\ <%an>\ %C(#17b062)(%cr)\ %d\%C(#c0d6de)%s" 
    l1f = log --name-status --pretty=format:"%C(#cd9a00)%h\%C(#0080ff)\ <%an>\ %C(#17b062)(%cr)\ %d\%C(#c0d6de)%s"

I used this site to pick the exact colors I wanted and then copied the hex value from it:

我使用这个网站来挑选我想要的确切颜色,然后从中复制十六进制值:

https://www.colorhexa.com/

https://www.colorhexa.com/

For Windows, open up your .gitconfig file that resides in your "C:\Users\YourUserName" Folder and add an alias section as depicted above.

对于 Windows,打开位于“C:\Users\YourUserName”文件夹中的 .gitconfig 文件并添加如上所示的别名部分。

Hope you like them.

希望你喜欢他们。