ANSI color in git doesn't display correctly

I recently switched to SLES 11. I found a problem for the git command. All ANSI color cannot be displayed. Instead, it shows the ANSI code as follows:

* ESC [33m * commit 0a02124a0fd85c1f0094bcdf15bac1645e3c8630ESC [m

Note: the ansi color in 'ls' works very well.

+59
git bash
Dec 13 2018-11-12T00:
source share
8 answers

Try setting the LESS environment variable to include the -R option:

 LESS=-R git grep ... 

If this works, add export LESS=-R to your ~/.bashrc or ~/.profile or similar shell startup file.

  -R or --RAW-CONTROL-CHARS Like -r, but only ANSI "color" escape sequences are output in "raw" form. Unlike -r, the screen appearance is maintained correctly in most cases. ANSI "color" escape sequences are sequences of the form: ESC [ ... m where the "..." is zero or more color specification characters 
+65
Dec 13 2018-11-11T00:
source share

The problem, as others have noted, is that your terminal is fine, but when Git calls a pager, it does not correctly interpret the ANSI color codes.

I would start by disabling LESS in your environment; it looks like you may have previously installed it for something hiding what Git needs. If that solves, that’s all. If you really have to configure LESS , note that Git starts with FRSX by default, so be careful when changing if you don't need it.

If for any reason you want LESS in your environment to be different from what you want for Git, then the core.pager configuration variable is the ideal way to work with Git and the pager. To quote manpage:

The command that Git will use to display the paged image. May be overridden by the GIT_PAGER environment variable. Note that Git sets the LESS environment variable to FRSX if it is not set when the pager starts. You can change these settings by setting the LESS variable to a different value. Alternatively, these parameters can be overridden on a project basis or globally by setting the core.pager parameter. Setting core.pager does not affect the behavior of the LESS environment variable above, so if you want to override the default settings for w27, you must be explicit. For example, to disable the S parameter in reverse, set core.pager to less -+$LESS -FRX . This will be passed to the shell using Git, which converts the final command to LESS=FRSX less -+FRSX -FRX .

This, combined with some knowledge of the parameters you need, should get you where you want to be. (The backward compatibility ability works by disabling all options currently in LESS and then adding back the ones you want.)

+28
Dec 13 2018-11-11T00:
source share

In git, you can change your pager to use the -R option:

 git config --global core.pager "less -R" 
+19
Jun 26 '13 at 17:57
source share

This did not work for me:

git config --global core.pager less -R

So, instead, I added the following to the ~ / .gitconfig file

  [core] pager = less -R 

To test this, I did

git log --graph --pretty=format:"%C(yellow)%h%Creset%C(blue)%d%Creset %C(white bold)%s%Creset %C(white dim)(by %an %ar)%Creset" --all

+6
Dec 19 '13 at 6:29
source share

OK I understood. This problem is related to the LESS variable.

The following line solves this problem:

 export LESS="-erX" 
+4
Dec 13
source share

I had the same problem. But why do I need to configure git on one computer and not need another? I want to fix the source of the problem because

git config --global core.pager "less -R"

Looks like a workaround for me.

+1
Apr 19 '16 at 9:44
source share

I had a similar problem with ANSI escape sequences in Git for Windows v2.7.1 in the Laravel Artisan and Symfony consoles. Here the mentioned LESS solution did not solve the problem.

Since Git for Windows opens a terminal with bash --login -i , this line, entered after starting the terminal, worked for me:

 bash 
0
Mar 04 '16 at 19:05
source share

Whoever is here, in my case, this was decided by setting an empty line in core.pager instead of resetting it:

 git config --global core.pager '' 

It was from https://git-scm.com/book/en/v2/Customizing-Git-Git-Configuration

0
Jul 18 '19 at 19:57
source share



All Articles