How to colorify git errors, warnings and fatal messages?

Since I didn’t find a suitable way to color error messages, my solution is to add an additional warning when git returns an error code (!=0).

To do it, add this to your ~/.bashrc or ~/.bash_profile

# Wrap git. On errors, print an additional line in red.
git(){
    command git "$@"
    local exitCode=$?
    if [ $exitCode -ne 0 ]; then
        printf "\033[0;31mERROR: git exited with code $exitCode\033[0m\n"
        return $exitCode
    fi
}

Here is the result:
enter image description here

Note that coloring stderr in red does not work very well because git logs many things in stderr, not only errors. And some errors are output in standard output.

Leave a Comment