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

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.


Solution 1:

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:

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:

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:

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:

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