How to color the Git console?

GitColorsConsole

Git Problem Overview


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?

To install it, I used the command: $ sudo apt-get install git-core

Git Solutions


Solution 1 - Git

As noted by @VonC, color.ui defaults to auto since Git 1.8.4


From the Unix & Linux Stackexchange question How to colorize output of git? and the answer by @Evgeny:

git config --global color.ui auto

> The color.ui is a meta configuration that includes all the various color.* configurations available with git commands. This is explained in-depth in git help config.

So basically it's easier and more future proof than setting the different color.* settings separately.

In-depth explanation from the git config documentation:

> color.ui: This variable determines the default value for variables such as color.diff and color.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 to always if you want all output not intended for machine consumption to use color, to true or auto if you want such output to use color when written to the terminal, or to false or never if you prefer git commands not to use color unless enabled explicitly with some other configuration or the --color option.

Solution 2 - Git

For example see 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

Solution 3 - Git

Add to your .gitconfig file next 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

Solution 4 - Git

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:

> 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".

So you will see colors by default.

Solution 5 - Git

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 config --global color.ui true

Solution 6 - Git

In your ~/.gitconfig file, simply add this:

[color]
  ui = auto

It takes care of all your git commands.

Solution 7 - Git

Another way is to edit the .gitconfig (create one if not exist), for instance:

vim ~/.gitconfig

and then add:

[color]
  diff = auto
  status = auto
  branch = auto

Solution 8 - Git

GIT uses colored output by default but on some system like as CentOS it is not enabled . You can enable it like this

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 --global is 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 -

 git config color.ui  true 

Solution 9 - Git

Improving Git 'branch-type' colors on Windows 10:

showing command git branch -avv for all examples...


1. Git Default color scheme:

git config --system --remove-section color.branch
enter image description here


2. Branch colors given in another answer on this page:

git config --system color.branch.local    "yellow"
git config --system color.branch.current  "yellow reverse"
git config --system color.branch.remote   "green"
enter image description here


3. Possibly improved branch color scheme:

git config --system color.branch.local    "yellow"
git config --system color.branch.current  "brightwhite yellow"
git config --system color.branch.remote   "normal green"
git config --system color.branch.upstream "brightwhite cyan"
enter image description here


4. Make your own:

Specify the foreground color plus an (optional) background color. The set of colors that have an effect in Windows 10 are given according to the following regular expression (yes, you can use brightblack and it is fact quite useful):

(normal|(bright)?(black|red|green|yellow|blue|magenta|cyan|white))

As far as I can tell, the dim option (see docs link at the bottom) does nothing in the Windows 10 console, and the bold option has the same effect as a bright* color. Now recall the config syntax shown in the examples:

git config <config-type> color.branch.<slot>  "<fg> <bg>"

The parameter <config-type> is typically --system or --global. If omitted, the specified color(s) will only be applied to the current repo. Using the color names just detailed, you can set the foreground <fg> and background <bg> colors for specific branch types, where <slot> is one of the following:

  • current (the current branch)
  • local (a local branch)
  • remote (a remote-tracking branch in refs/remotes/)
  • upstream (upstream tracking branch)
  • plain

As always, you can display your entire git configuration, which will include any of the options that you've set as described here, via the following command:

git config -l --show-origin

Note there's also a further set of config options which pertain to log output colors (not discussed here):

git config --system color.decorate.(branch|remoteBranch|tag|stash|HEAD|grafted) <color>

reference: https://git-scm.com/docs/git-config

Solution 10 - Git

Well, if you are not satisfied with the default setting, you can use ANSI escape code to help you set the color, and if you want to modify some text, you can write bash to help you. see as below:

Eaxmplae

# .gitconfig

[alias]
	st-color = "!f() { \
        echo -n -e '\\033[38;2;255;0;01m\\033[4m' ;\
	    git status -s | grep ' D' | \
	    sed -e 's/^ ./DELETE:/' ; \
	    echo -n -e '\\033[m' ;\
	    \
	    echo -n -e '\\033[48;2;128;128;128m' ;\
        echo -n -e '\\033[38;2;0;255;01m' ;\
	    git status -s | grep ' [AM]' | \
	    sed -e 's/^ ./NEW OR MODIFY:/' ; \
	    echo -n -e '\\033[m' ;\
        \
        echo -n -e '\\033[38;2;255;0;255m' ;\
        echo Rename ;\
	    git status -s | grep 'R ' | \
	    sed -e 's/^..//' ; \
	    echo -n -e '\\033[m' ;\
	}; f"

demo

enter image description here

Explanation

  1. you can write the long script on .gitconfig use the syntax as below:

    [alias]
        your-cmd = !f() { \
            \
        }; f"
    
  2. echo -n -e (see more echo)

    • -n = Do not output a trailing newline.
    • -e Enable interpretation of the following backslash-escaped
  3. \\033[38;2;255;0;0m\\033[4m (see more SGR parameters)

    • \\033[38;2;255;0;0m : 38 mean fore color. 255;0;0 = Red | r;g;b
    • \\033[4m : underline
  4. grep : The grep command is used to search text.

  5. sed -e 's/be_replace_string/new_string/' replace string to new string.

Solution 11 - Git

With Git 2.18, you have more control on how you want to specify colors in the console.
The "git config" command uses separate options e.g. "--int", "--bool", etc. to specify what type the caller wants the value to be interpreted as.

A new "--type=<typename>" option has been introduced, which would make it cleaner to define new types.

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)

> ## 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 instructs git 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 that git 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.

So before you had --bool and --int, now (documentation):

--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 use git 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.

    --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.

See commit cd8e759 (05 Mar 2019) by Jeff King (peff).
(Merged by Junio C Hamano -- gitster -- in commit f6c75e3, 20 Mar 2019)

> ## config: document --type=color output is a complete line

> Even 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.

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


You can see git config --type=bool used with Git 2.26 (Q1 2020) to replace "git config --bool" calls in sample templates.

See commit 81e3db4 (19 Jan 2020) by Lucius Hu (lebensterben).
(Merged by Junio C Hamano -- gitster -- in commit 7050624, 30 Jan 2020)

> ## templates: fix deprecated type option --bool
> Signed-off-by: Lucius Hu

> The --bool option to git-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.

Solution 12 - Git

refer here: https://nathanhoad.net/how-to-colours-in-git/

steps:

  1. Open ~/.gitconfig for editing

    vi ~/.gitconfig

  2. 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
    
  3. Save the file.

Just change any file in your local repo and do

git status

Solution 13 - Git

Let's suppose you want the current branch to be yellow and all other branches to be cyan bold. I am considering you want these changes to be done locally i.e in your current repository and not for all the repositories present in your system. Go inside .git file using "cd .git" then open "config" file. Inside the config file type the below content without changing anything else in the config file.

 [color]
         ui=true
    [color "branch"]
         local=cyan bold 
         current=yellow bold

And then save the config file. Open the git console and perform git branch. You shall see the difference

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMauroPorrasView Question on Stackoverflow
Solution 1 - GitJoel PurraView Answer on Stackoverflow
Solution 2 - GitKingCrunchView Answer on Stackoverflow
Solution 3 - GitVictorView Answer on Stackoverflow
Solution 4 - GitVonCView Answer on Stackoverflow
Solution 5 - GitTanmayView Answer on Stackoverflow
Solution 6 - GitSkiptomyluView Answer on Stackoverflow
Solution 7 - GitatupalView Answer on Stackoverflow
Solution 8 - GitDeepak DixitView Answer on Stackoverflow
Solution 9 - GitGlenn SlaydenView Answer on Stackoverflow
Solution 10 - GitCarsonView Answer on Stackoverflow
Solution 11 - GitVonCView Answer on Stackoverflow
Solution 12 - Gitgajanan malvadeView Answer on Stackoverflow
Solution 13 - GitFaizan MakandarView Answer on Stackoverflow