How to colorify git errors, warnings and fatal messages?

前端 未结 4 1921
傲寒
傲寒 2020-12-01 13:59

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 colori

4条回答
  •  北海茫月
    2020-12-01 14:36

    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:

    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.

提交回复
热议问题