如何为 Git 控制台着色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10998792/
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
How to color the Git console?
提问by MauroPorras
I recently saw that the git
console in Windows is colored, e.g. Green for additions, red for deletions, etc. How do I color my git
console like that?
我最近看到git
Windows中的控制台是彩色的,例如绿色表示添加,红色表示删除等。我如何为git
控制台着色?
To install it, I used the command: $ sudo apt-get install git-core
要安装它,我使用了以下命令: $ sudo apt-get install git-core
回答by Joel Purra
As notedby @VonC, color.ui
defaults to auto
since Git 1.8.4
正如@VonC所指出的,默认为自 Git 1.8.4color.ui
auto
From the Unix & Linux Stackexchange question How to colorize output of git?and the answer by @Evgeny:
来自 Unix & Linux Stackexchange 问题How to colorize output of git? 以及@Evgeny的回答:
git config --global color.ui auto
The
color.ui
is a meta configuration that includes all the variouscolor.*
configurations available withgit
commands. This is explained in-depth ingit help config
.
这
color.ui
是一个元配置,包括所有color.*
可用的git
命令配置。这在 中进行了深入解释git help config
。
So basically it's easier and more future proof than setting the different color.*
settings separately.
所以基本上它比color.*
单独设置不同的设置更容易,更适合未来。
In-depth explanation from the git config
documentation:
git config
文档中的深入解释:
color.ui
: This variable determines the default value for variables such ascolor.diff
andcolor.grep
that control the use of color per command family. Its scope will expand as more commands learn configuration to set a default for the--color
option. Set it toalways
if you want all output not intended for machine consumption to use color, totrue
orauto
if you want such output to use color when written to the terminal, or tofalse
ornever
if you prefer git commands not to use color unless enabled explicitly with some other configuration or the--color
option.
color.ui
:此变量确定变量的默认值,例如color.diff
和color.grep
控制每个命令系列的颜色的使用。随着更多命令学习配置以设置--color
选项的默认值,其范围将扩大。将它设置为always
,如果你想不用于机器消耗的用色都输出,true
或者auto
如果你想这样的输出使用的颜色,当写入终端,或者false
或者never
,如果你喜欢的git命令不要使用颜色,除非有一些明确启用其他配置或--color
选项。
回答by KingCrunch
For example see https://web.archive.org/web/20080506194329/http://www.arthurkoziel.com/2008/05/02/git-configuration/
例如见https://web.archive.org/web/20080506194329/http://www.arthurkoziel.com/2008/05/02/git-configuration/
The interesting part is
有趣的部分是
Colorized output:
git config --global color.branch auto git config --global color.diff auto git config --global color.interactive auto git config --global color.status auto
彩色输出:
git config --global color.branch auto git config --global color.diff auto git config --global color.interactive auto git config --global color.status auto
回答by Victor
Add to your .gitconfig file next code:
将下一个代码添加到您的 .gitconfig 文件中:
[color]
ui = auto
[color "branch"]
current = yellow reverse
local = yellow
remote = green
[color "diff"]
meta = yellow bold
frag = magenta bold
old = red bold
new = green bold
[color "status"]
added = yellow
changed = green
untracked = cyan
回答by Tanmay
Git automatically colors most of its output if you ask it to. You can get very specific about what you want colored and how; but to turn on all the default terminal coloring, set color.ui to true:
如果您要求,Git 会自动为其大部分输出着色。您可以非常具体地了解您想要什么颜色以及如何上色;但要打开所有默认终端着色,请将 color.ui 设置为 true:
git config --global color.ui true
回答by Chuntao Lu
In your ~/.gitconfig
file, simply add this:
在您的~/.gitconfig
文件中,只需添加以下内容:
[color]
ui = auto
It takes care of all your git commands.
它会处理您所有的 git 命令。
回答by VonC
In Ubuntu or any other platform (yes, Windows too!); starting git1.8.4, which was released 2013-08-23, you won't have to do anything:
在 Ubuntu 或任何其他平台(是的,Windows 也是如此!);开始git1.8.4,这是公布的2013年8月23日,你会不会做任何事情:
Many tutorials teach users to set "color.ui" to "auto" as the first thing after you set "
user.name/email
" to introduce yourselves to Git. Now the variable defaults to "auto
".
很多教程教用户设置“color.ui”为“auto”,作为
user.name/email
对Git的自我介绍后的第一件事。现在变量默认为“auto
”。
So you will see colors by default.
所以默认情况下你会看到颜色。
回答by Deepak Dixit
GIT uses colored output by default but on some system like as CentOS it is not enabled . You can enable it like this
GIT 默认使用彩色输出,但在某些系统(如 CentOS)上未启用。你可以像这样启用它
git config --global color.ui true
git config --global color.ui false
git config --global color.ui auto
You can choose your required command from here .
您可以从此处选择所需的命令。
Here --globalis optional to apply action for every repository in your system . If you want to apply coloring for current repository only then you can do something like this -
这里--global是可选的,可以对系统中的每个存储库应用操作。如果您只想为当前存储库应用着色,则可以执行以下操作 -
git config color.ui true
回答by atupal
Another way is to edit the .gitconfig
(create one if not exist), for instance:
另一种方法是编辑.gitconfig
(如果不存在则创建一个),例如:
vim ~/.gitconfig
and then add:
然后添加:
[color]
diff = auto
status = auto
branch = auto
回答by VonC
With Git 2.18, you have more control on how you want to specify colors in the console.
The "git config
" command uses separate optionse.g. "--int
", "--bool
", etc. to specify what type the caller wants the value to be interpreted as.
使用 Git 2.18,您可以更好地控制如何在控制台中指定颜色。
" git config
" 命令使用单独的选项,例如 " --int
"、" --bool
" 等来指定调用者希望将值解释为什么类型。
A new "--type=<typename>
" option has been introduced, which would make it cleaner to define new types.
--type=<typename>
引入了一个新的 " " 选项,这将使定义新类型更清晰。
See commit fb0dc3b(18 Apr 2018), and commit 0a8950b(09 Apr 2018) by Taylor Blau (ttaylorr
).
(Merged by Junio C Hamano -- gitster
--in commit e3e042b, 08 May 2018)
请参阅提交 fb0dc3b(2018 年 4 月 18 日)和Taylor Blau ( )提交的 0a8950b(2018 年 4 月 9 日)。(由Junio C Hamano合并-- --在提交 e3e042b 中,2018 年 5 月 8 日)ttaylorr
gitster
builtin/config.c
: support--type=<type>
as preferred alias for--<type>
git config
has long allowed the ability for callers to provide a 'type specifier', which instructsgit config
to (1) ensure that incoming values can be interpreted as that type, and (2) that outgoing values are canonicalized under that type.In another series, we propose to extend this functionality with
--type=color
and--default
to replace--get-color
.However, we traditionally use
--color
to mean "colorize this output", instead of "this value should be treated as a color".Currently,
git config
does not support this kind of colorization, but we should be careful to avoid squatting on this option too soon, so thatgit config
can support--color
(in the traditional sense) in the future, if that is desired.In this patch, we support
--type=<int|bool|bool-or-int|...>
in addition to--int
,--bool
, and etc.
This allows the aforementioned upcoming patch to support querying a color value with a default via--type=color --default=...
, without squandering--color
.We retain the historic behavior of complaining when multiple, legacy-style
--<type>
flags are given, as well as extend this to conflicting new-style--type=<type>
flags.--int --type=int
(and its commutative pair) does not complain, but--bool --type=int
(and its commutative pair) does.
builtin/config.c
: 支持--type=<type>
作为首选别名--<type>
git config
长期以来一直允许调用者提供“类型说明符”,它指示git config
(1) 确保传入值可以解释为该类型,以及 (2) 传出值在该类型下规范化。在另一个系列中,我们建议扩展此功能
--type=color
并--default
替换--get-color
.然而,我们传统上使用
--color
的意思是“着色这个输出”,而不是“这个值应该被视为一种颜色”。目前,
git config
不支持这种着色,但我们应该小心避免过早地占用此选项,以便 将来git config
可以支持--color
(传统意义上的),如果需要的话。在这个补丁中,
--type=<int|bool|bool-or-int|...>
除了--int
、--bool
、 等之外,我们还支持。
这允许前面提到的补丁支持使用默认 via 查询颜色值--type=color --default=...
,而不会浪费--color
。我们保留了在给出多个遗留样式
--<type>
标志时抱怨的历史行为,并将其扩展到冲突的新样式--type=<type>
标志。--int --type=int
(及其交换对)不会抱怨,但--bool --type=int
(及其交换对)会抱怨。
So before you had --bool
and --int
, now (documentation):
因此,在您拥有--bool
和之前--int
,现在(文档):
--type <type>
'
git config
' will ensure that any input or output is valid under the given type constraint(s), and will canonicalize outgoing values in<type>
's canonical form.Valid
<type>
's include:
- '
bool
': canonicalize values as either "true
" or "false
".- '
int
': canonicalize values as simple decimal numbers. An optional suffix of 'k
', 'm
', or 'g
' will cause the value to be multiplied by 1024, 1048576, or 1073741824 upon input.- '
bool-or-int
': canonicalize according to either 'bool
' or 'int
', as described above.- '
path
': canonicalize by adding a leading~
to the value of$HOME
and~user
to the home directory for the specified user. This specifier has no effect when setting the value (but you can usegit config section.variable ~/
from the command line to let your shell do the expansion.)- '
expiry-date
': canonicalize by converting from a fixed or relative date-string to a timestamp. This specifier has no effect when setting the value.
'
git config
' 将确保任何输入或输出在给定的类型约束下都是有效的,并且将以<type>
' 的规范形式规范化传出值。有效
<type>
的包括:
- '
bool
': 将值规范化为 "true
" 或 "false
"。- '
int
':将值规范化为简单的十进制数。'k
'、'm
' 或 'g
'的可选后缀将导致该值在输入时乘以 1024、1048576 或 1073741824。- '
bool-or-int
':根据 'bool
' 或 'int
' 进行规范化,如上所述。- '
path
':通过在指定用户的主目录~
的值$HOME
和~user
主目录中添加前导来规范化。该说明符在设置值时无效(但您可以git config section.variable ~/
从命令行使用,让您的 shell 进行扩展。)- '
expiry-date
':通过从固定或相对日期字符串转换为时间戳来规范化。此说明符在设置值时无效。
--bool::
--int::
--bool-or-int::
--path::
--expiry-date::
Historical options for selecting a type specifier. Prefer instead `--type`,
(see: above).
Note that Git 2.22 (Q2 2019) explains "git config --type=color ...
" is meant to replace "git config --get-color
", but there is a slight difference that wasn't documented, which is now fixed.
请注意,Git 2.22(2019 年第 2 季度)解释说“ git config --type=color ...
”是为了替换“ git config --get-color
”,但有一个没有记录的细微差别,现在已修复。
See commit cd8e759(05 Mar 2019) by Jeff King (peff
).
(Merged by Junio C Hamano -- gitster
--in commit f6c75e3, 20 Mar 2019)
请参阅Jeff King ( ) 的commit cd8e759(05 Mar 2019 )。(由Junio C Hamano合并-- --在提交 f6c75e3 中,2019 年 3 月 20 日)peff
gitster
config
: document--type=color
output is a complete lineEven though the newer "
--type=color
" option to "git config
" is meant to be upward compatible with the traditional "--get-color
" option, unlike the latter, its output is not an incomplete line that lack the LF at the end.
That makes it consistent with output of other types like "git config --type=bool
".Document it, as it sometimes surprises unsuspecting users.
config
: 文档--type=color
输出是完整的一行尽管较新的 "
--type=color
" 选项 "git config
" 旨在与传统的 "--get-color
" 选项向上兼容,但与后者不同的是,它的输出不是结尾缺少 LF 的不完整行。
这使它与其他类型(如“git config --type=bool
”)的输出一致。记录它,因为它有时会让毫无戒心的用户感到惊讶。
This now reads:
现在这样写:
--type=color [--default=<default>]
is preferred over--get-color
(but note that--get-color
will omit the trailing newline printed by--type=color
).
--type=color [--default=<default>]
优先于--get-color
(但请注意,--get-color
将省略由 打印的尾随换行符--type=color
)。
You can see git config --type=bool
used with Git 2.26 (Q1 2020) to replace "git config --bool
" calls in sample templates.
您可以看到git config --type=bool
与 Git 2.26(2020 年第一季度)一起使用以替换git config --bool
示例模板中的“ ”调用。
See commit 81e3db4(19 Jan 2020) by Lucius Hu (lebensterben
).
(Merged by Junio C Hamano -- gitster
--in commit 7050624, 30 Jan 2020)
请参阅Lucius Hu ( )提交的 81e3db4(2020 年 1 月 19 日)。(由Junio C Hamano合并-- --在提交 7050624 中,2020 年 1 月 30 日)lebensterben
gitster
templates
: fix deprecated type option--bool
Signed-off-by: Lucius Hu
The
--bool
option togit-config
is marked as historical, and users are recommended to use--type=bool
instead.
This commit replaces all occurrences of--bool
in the templates.Also note that, no other deprecated type options are found, including
--int
,--bool-or-int
,--path
, or--expiry-date
.
templates
:修复不推荐使用的类型选项--bool
签字人:Lucius Hu
该
--bool
选项git-config
被标记为历史,并建议用户使用--type=bool
来代替。
此提交替换--bool
模板中所有出现的。还要注意的是,没有其他过时的类型选项中找到,其中包括
--int
,--bool-or-int
,--path
,或--expiry-date
。
回答by gajanan malvade
refer here: https://nathanhoad.net/how-to-colours-in-git/
参考这里:https: //nathanhoad.net/how-to-colours-in-git/
steps:
脚步:
Open ~/.gitconfig for editing
vi ~/.gitconfig
Paste following code:
[color] ui = auto [color "branch"] current = yellow reverse local = yellow remote = green [color "diff"] meta = yellow bold frag = magenta bold old = red bold new = green bold [color "status"] added = yellow changed = green untracked = cyan
Save the file.
打开 ~/.gitconfig 进行编辑
vi ~/.gitconfig
粘贴以下代码:
[color] ui = auto [color "branch"] current = yellow reverse local = yellow remote = green [color "diff"] meta = yellow bold frag = magenta bold old = red bold new = green bold [color "status"] added = yellow changed = green untracked = cyan
保存文件。
Just change any file in your local repo and do
只需更改本地存储库中的任何文件即可
git status