4

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

Grief
  • 253

4 Answers4

4

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-

  • 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
4

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)

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
1

Here is another way to do the same thing. It processes all the lines with one 'sed' command.

color_git_diff.sh:

#!/bin/bash

sed $' s/^M/\033[33mM/ s/^A/\033[32mA/ s/^D/\033[31mD/ s/$/\033[0m/'

You can use it like:

git diff --name-status | ./color_git_diff.sh
K.N.
  • 111
-1

Try next command:

vimdiff [file1] [file2]
Alberto
  • 505