How to configure Mac OS X term so that git has color?

MacosGitCommand Line-Interface

Macos Problem Overview


I've seen a Mac OS X git demo online in which it's configured to have multiple colors.

For example, his prompt is amber, his ls directory is purple and his git diff output has ~ 4 colors (pink, light green, red, pale yellow).

Can you tell me how can I configure Mac OS X terminal to achieve that? It's definitely Mac OS X Terminal.app, not iTerm.

Macos Solutions


Solution 1 - Macos

William Purcell's answer only enables color for the 'git diff' command. Do this to enable colors for all git commands:

$ git config --global color.ui true

Solution 2 - Macos

To display color in the output of git diff, you need to configure git. Try running

$ git config --global color.diff true

to set your $HOME/.gitconfig appropriately.

Solution 3 - Macos

This is what I use in my .profile file. Works like a charm because it allows me to see the current git branch as well as its state through the color. If you want to modify it please note that it's important to escape color codes in order to avoid line feed problems in long lines.

# Setting GIT prompt
c_cyan=`tput setaf 6`
c_red=`tput setaf 1`
c_green=`tput setaf 2`
c_sgr0=`tput sgr0`

branch_color ()
{
	if git rev-parse --git-dir >/dev/null 2>&1
	then
		color=""
		if git diff --quiet 2>/dev/null >&2 
		then
			color=${c_green}
		else
			color=${c_red}
		fi
	else
		return 0
 	fi
	echo -n $color
}

parse_git_branch ()
{
	if git rev-parse --git-dir >/dev/null 2>&1
	then
		gitver="["$(git branch 2>/dev/null| sed -n '/^\*/s/^\* //p')"]"
	else
		return 0
	fi
echo -e $gitver
}

#It's important to escape colors with \[ to indicate the length is 0
PS1='\u@\[${c_red}\]\W\[${c_sgr0}\]\[\[$(branch_color)\]$(parse_git_branch)\[${c_sgr0}\]$ '

Solution 4 - Macos

It is not normally something you configure the terminal to do... The terminal is unaware of what it is showing but try this in your shell (if you're using bash, in some other shells you don't export but call setenv or something else):

export CLICOLOR=1
export TERM=xterm-color

You can then use LSCOLORS generator to setup something that you can export using something like:

export LSCOLORS=fxfxcxdxbxegedabagacad

(the above should give you purple directories)

When you're done and satisfied with the result, add the three lines to either your /etc/bashrc or the .bashrc file in your user's home directory.

Edit: Also, in your terminal, make sure the checkbox "Display ANSI colors" (on the "Text" page) is checked.

Solution 5 - Macos

Open the terminal app, then open the preferences dialogue either through the menu (Terminal -> Preferences) or by pressing Command+,. Once the preferences dialogue opens, select the terminal style from the pane on the left, select Text from the button bar, than make sure the "Display ANSI colors" check box is checked.

That will enable the colors on the terminal. To get colors in the output on the terminal, you will need to embed ANSI color commands in the data being sent to the terminal. How this is done is dependent on the commands. For example (as was shown above) the ls command has a colors option. For the color codes, do a google lookup for "ansi color".

Solution 6 - Macos

For colored ls output I would recommend installing the gnu coreutils and using that version of ls instead. For either version of ls you'll need to pass the correct flag to it, which is --color for the gnu version or -G for the standard OS X version. So you can do something like

alias ls='ls --color'

in your .bashrc.

To color your prompt you'll need to use the correct colors codes for your terminal, but mine uses

PROMPT="$(print '%{\e[0;38m%}%{\e[1;1m%]%}[%m:%c] %n%%%{\e[0m%}') "

to produce

[hostname:directory] username%

in bold white.

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
QuestionyinglcsView Question on Stackoverflow
Solution 1 - MacosphloopyView Answer on Stackoverflow
Solution 2 - MacosWilliam PursellView Answer on Stackoverflow
Solution 3 - MacosMarc MView Answer on Stackoverflow
Solution 4 - MacosFredrikView Answer on Stackoverflow
Solution 5 - MacoscodeyView Answer on Stackoverflow
Solution 6 - MacosAmokView Answer on Stackoverflow