How to colorify git errors, warnings and fatal messages?

When troubleshooting git issues of users, I keep running into people not noticing error/warning messages from git, and then burning their fingers. Is there any way to colorify errors and warnings git outputs?

  • What affects Git checkout time?
  • What is the best way to clone branches after cloning just trunk in git-svn?
  • Git global hook customization
  • Auorefresh git log after commit when running in the Terminal
  • Is it possible to force git submodules to be updated?
  • Git remote log and changes request
  • subtree naming convention in git
  • Sync 2 Folders in different GIT-Repos
  • Does 'git branch -u (or --set-upstream-to)' lose tracking information of all the existing remote tracking branches?
  • Git - Branch status (frozen, inactive, etc.)
  • On Github, how do you distinguish the good from the bad?
  • Git: How can I put a commit between older commits without changing the files/folders of later commits?
  • 3 Solutions collect form web for “How to colorify git errors, warnings and fatal messages?”

    There is no git buit-in way to do that. Git just prints errors to STDERR and doesn’t care about the fatality of the error or anything. What you can do is color STDERR red. How to do this has been asked on on ServerFault: https://serverfault.com/questions/59262/bash-print-stderr-in-red-color

    There are three basic options:

    1. Run your command like this:

      *git-command* 2> >(while read line; do echo -e "\e[01;31m$line\e[0m" >&2; done)
      
    2. Use a wrapper script (See ServeFault for those), and run commands like

      mywrapper *git-command*
      
    3. Install stderred. This will allow you to make the effect permanent, without modifying your command line. Not sure whether this will work on windows, though.

    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.

    You can use the color config section of git.

    For more information and examples see http://git-scm.com/book/en/Customizing-Git-Git-Configuration#Colors-in-Git or the second part of http://blog.philippmetzler.com/?p=15

    example: (add to your .gitconfig)

    [color]
         interactive = always
    [color "interactive"]
         error = red bold
    
    Git Baby is a git and github fan, let's start git clone.