329

Is there a way to color output for git (or any command)?

Consider:

baller@Laptop:~/rails/spunky-monkey$ git status
# On branch new-message-types
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   app/models/message_type.rb
#
no changes added to commit (use "git add" and/or "git commit -a")
baller@Laptop:~/rails/spunky-monkey$ git add app/models

And

baller@Laptop:~/rails/spunky-monkey$ git status
# On branch new-message-types
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   app/models/message_type.rb
#

The output looks the same, but the information is totally different: the file has gone from unstaged to staged for commit.

Is there a way to colorize the output? For example, files that are unstaged are red, staged are green?

Or even Changes not staged for commit: to red and # Changes to be committed: to green?

Working in Ubuntu.

EDIT: Googling found this answer which works great: git config --global --add color.ui true.

However, is there any more general solution for adding color to a command output?

derobert
  • 109,670
B Seven
  • 7,989
  • 1
    You want something that colorizes all command outputs? How would it know which parts to color? – Michael Mrozek Jul 30 '12 at 00:28
  • I guess if there was a way to configure it using regex: Each color could have a start regex. And there could be a default color regex to turn off all colors. And, if text "foo" appears, display it in a certain color... – B Seven Jul 30 '12 at 00:33

12 Answers12

344

You can create a section [color] in your ~/.gitconfig with e.g. the following content

[color]
  diff = auto
  status = auto
  branch = auto
  interactive = auto
  ui = true
  pager = true

You can also fine control what you want to have coloured in what way, e.g.

[color "status"]
  added = green
  changed = red bold
  untracked = magenta bold

[color "branch"] remote = yellow

I hope this gets you started. And of course, you need a terminal which supports colour.

Also see this answer for a way to add colorization directly from the command line.

msouth
  • 135
  • 1
  • 7
Marco
  • 33,548
  • 23
    I think it might be worth putting git config --global color.ui auto (@Evgeny 's answer) at the top of yours...I think that's likely to be what most people are looking for. I've upvoted both...I'm just saying, for the sake of the internet as it comes here, I think a lot of people just want that simple one liner. All the better if they get it, plus your extra goodness. – msouth May 17 '15 at 03:06
  • This worked perfectly for me - in the [color "status"] section I added branch = yellow. – Wayne Werner Oct 26 '16 at 17:06
  • 3
    you can add [color] section to project's .git/config file too – andrej Jul 13 '17 at 09:30
  • And you can just set a configuration variable for a single invocation of a git command: https://stackoverflow.com/a/18304605/134044 – NeilG Sep 03 '19 at 01:01
  • 1
    @msouth putting git config --global color.ui auto on top of this answer actually produces error fatal: bad config line 1 in file ~/.gitconfig. – nougako Mar 13 '22 at 20:46
  • Hi @nougako, I apologize, I don't think what I said was as clear as it could have been. I meant that this stackexchange answer should be edited to add the command git config --global color.ui auto to the beginning of the answer. It doesn't work to add the command to the config, as you correctly pointed out. – msouth Mar 20 '22 at 17:55
294

You probably want to use

git config --global color.ui auto

The auto part says that git will only try and use color on terminals that support it, and you will not get ANSI sequences if you redirect output of git commands to a file for example. Setting it to true is same as auto, and this is also the default since Git 1.8.4.

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.

When color.ui is set to always it will always emit ANSI color characters, even when piping the output like git log | less while when set to auto it will not print colors unless the output is to the terminal.

  • 1
    Can you explain the difference between color.ui true (which auto points to) versus always? I read the docs but I still don’t get the difference. – chharvey May 30 '19 at 15:03
  • 5
    @chharvey color.ui = auto + git diff | less - no colors, color.ui = always + git diff | less - colored output. LESS=-R is implied. – x-yuri Jan 07 '20 at 11:50
34

The accepted answer gives the most common solution. If for any reason you need not to permanently change the configuration, which that solution does, you may override the configuration for a single git command:

git -c color.ui=always <usual git command and options>

For example:

git -c color.ui=always status
git -c color.ui=always diff

Tested: supported on git 2.4.6, not supported on git 1.7.1.

Hamlet
  • 451
  • 8
    For anyone else trying to get color when piping into less like I just was, you can make less pass the color escape characters through to the terminal via less -R. – xdhmoore Jan 11 '17 at 18:16
  • When was this feature added? – Ondra Žižka Oct 03 '18 at 16:47
  • And for completeness: if you pipe it to less and forget to use -R on the command line, you can interactively type -R at it to switch that mode on. :) – Peeja Apr 26 '23 at 20:34
25
git config --global color.ui auto
git config --global color.branch auto
git config --global color.status auto
sakisk
  • 2,873
18
git config --global color.ui always
git config --global color.branch always
git config --global color.diff always
git config --global color.interactive always
git config --global color.status always
git config --global color.grep always
git config --global color.pager true
git config --global color.decorate always
git config --global color.showbranch always
mlibre
  • 1,311
4

or turn off all/most of the colorization off via:

git config --global color.ui false
git config --global color.branch false
git config --global color.diff false
git config --global color.interactive false
git config --global color.status false
git config --global color.grep false
git config --global color.pager false
git config --global color.decorate false
git config --global color.showbranch false
theRiley
  • 159
  • OP wasn't looking to turn off colors, rather to turn on colors. In the OP he stated how to do in for git and wants some way of doing it in a more global sense. If you know of such a method, please edit your answer to explain that. – Chindraba Mar 02 '17 at 04:51
  • sometimes people are looking for something closely related but slightly different - but thanks for the downvote. – theRiley Mar 03 '17 at 16:04
  • 1
    I liked it @theRiley – Richard Lindhout May 02 '17 at 12:32
3

For a colored git diff piped into less, this works:

git -c color.diff=always diff [...] | less -R
jox
  • 133
1

I know the post is four years old but no one responded from my camp, the color blind. If you can distinguish colors, ignore my post.

"git status" for example puts out text that is white on background/black on white background (legible), dark gray for deleted (illegible against a black background but legible against a white background) and medium gray for added (barley legible on black background, illegible on white background). I used to toggle the background of my terminal window to/from white/black so I could read the illegible text. A simple solution is more:

 git status | more

This makes all the text legible on a standard white or black background terminal window.

1

To colorize the output of git diff you can add a color.diff section to ~/.gitconfig. For example:

[color "diff"]
  new = bold italic 154
  old = bold italic 196

Here 154 and 196 are ANSI 256-color codes. For more details see man git config.

1

Some minimalist OSes popular for creating docker containers, like Alpine, may come with stripped down versions of lesss that do not output in color. Install the less package with apk add less command in Alpine.

StevieD
  • 915
1

You can do this with Arbitrary Command Output Colourer. It mostly works, but I haven't figured out how to work around a bug where prompts expecting input aren't shown and you can't simply type the known needed input and press enter to continue in every case.

Example of ~/.acoc.conf for git:

# git
[git/ae]
/.*(error:.*)/                                    red+bold
/.*(warning:.*)/                                  yellow
/.*(hint:.*)/                                     magenta
/.*(up-to-date).*/                                green+bold
/.*(nothing to commit).*/                         green
/^(\+.*)/                                         green
/^(-.*)/                                          red

..which works nicely along with alias git="acoc git" in .bash_profile.

-1

Definitely recommend: diff-so-fancy

  • 1
    Thank you for your answer! It could be made a bit more valuable to future readers if you (briefly) explain how to obtain, configure, and use the tool to solve the problem at hand. Thanks! – Jeff Schaller Feb 21 '21 at 15:45
  • brew install diff-so-fancy then follow the export suggestions linked in my answer. It was very easy to setup. – jasonleonhard Feb 21 '21 at 23:56