3

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

  • 1
    git status -s? – 7ochem Dec 12 '16 at 8:16
3

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-

|improve this answer|||||
  • Yeah, that's exactly the way I do it right now, except that I've written it in python. I was just curious about the 'official way' – Grief Nov 16 '15 at 13:12
2

I wanted to provide a slightly more updated method for doing this for the simple reason that when I tried the primary answer it was unbearably slow, nearly a second between outputted lines. That might be due to running git for windows as opposed to on a *nix machine, but the 'grep' command takes too long to be valid. That being said, the same effect can be achieved given the output of the "--name-status" parameter for both git log and git diff are simple strings, and so using simple string matching instead of the 'grep' tool, my output was almost immediate.

for i in "$@"
do
    if  [[ ${i:0:1} == "M" ]] #grep -q "^M" <<< "$i"
    then echo -e "\e[34m $i \e[0m"
    elif [[ ${i:0:1} == "D" ]] #grep -q "^D" <<< "$i"
    then echo -e "\e[31m $i \e[0m"
    elif [[ ${i:0:1} == "A" ]] #grep -q "^A" <<< "$i"
    then echo -e "\e[32m $i \e[0m"
    else echo -e "$i"
    fi
done

I also added the code for "added" as well as a default so if there is other text (like git log --pretty) it will not be ignored.

Jaeden "Sifo Dyas" al'Raec Ruiner
PS - use the same xargs command with the delimiter but the above script is faster at parsing than grep (on a windows box)

|improve this answer|||||
-1

Try next command:

vimdiff [file1] [file2]
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.