Colorize filenames based on status for git diff –name-status

color-managementcolorsgit

I often use git diff --name-status BRANCH_NAME to get the list of modified files. Is it possible to colorize the output of that command similar to the output of git status – green for added files, blue for modified, red for deleted and so on.

It's not a duplicate of How to colorize output of git? since I want to colorize different part of the output and changes in config files proposed there are not applicable

Best Answer

I dont know of any official way to do it, but I wrote this just now and it works for me.
Put the bash script below into a file called: color_git_diff.sh (or whatever you want)

#!/usr/bin/env bash

for i in "$@"
do
    if  grep -q "^M" <<< "$i"
    then
        echo -e "\e[33m $i \e[0m"
    elif  grep -q "^D" <<< "$i"
    then
        echo -e "\e[31m $i \e[0m"
    fi
done

to apply the script you would call it with:

git diff --name-status | xargs --delimiter="\n" ./color_git_diff.sh

Obviously you wouldnt want to call this everytime so you would need to put it into your bashrc and assign a function or an alias to it - or something like that.
This only produces colorised output for modified or deleted files and I made modified files yellow - I dont know what the ansi escape code is for blue off the top of my head - I think maybe \e[36m ? anyway if you want to use it you can add it yourself-

Related Question