9

I would like to style the bash output for git diff --name-status so that the files with status D, M, and A are different colors.

To style general git bash I use the color options in .gitconfig.

# .gitconfig
[color]
  branch = auto
  diff = auto
  status = auto
[color "branch"]
  current = yellow reverse
  local = yellow
  remote = green
[color "diff"]
  meta = yellow bold
  frag = magenta bold
  old = red bold
  new = green bold
[color "status"]
  added = yellow
  changed = green
  untracked = cyan

To style an output for a command like git log I can use --pretty=format in an alias like below:

# .gitconfig
[alias]
  log = log --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset'

However, I have been unsuccessful using --pretty=format for git diff --name-status.

The output I would like to style is currently not styled and looks like:

$ git diff <branch> --name-status
M       .gitignore
M       README.md
A       diagnostic.js
D       diagnostic.md

Is there a way to style the output git diff --name-status by status type?

2
  • 1
    I have never crossed the git diff --name-status flag, but it looks a lot like git status --short which has built-in colors. Would that do the trick for you? Commented Jul 21, 2017 at 18:50
  • @JoanRieu that would if you could tell git status to show a specific commit. git status --short is great though; this is exactly what I'd want to see from a colorized --name-status option Commented Feb 15, 2023 at 21:52

2 Answers 2

1

I'm not sure you can colorize the output of --name-status without entirely reproducing its output with --pretty=format:...

But if you want file names with colors summarizing changes, the --stats flag can be passed to many commands, including git diff <branch --stat and git log --stat.

It shows file names, with a colorized +++-- suffix, e.g.

$ git diff master --stat                                               
 lib/thing.go      |  5 +++--
 lib/thing_test.go | 23 +++++++++++++++++++++++
 2 files changed, 28 insertions(+), 2 deletion(-)

and the +s and -s will colorize according to your git config

Sign up to request clarification or add additional context in comments.

1 Comment

I don't think it's possible to reproduce the list of files using --pretty... see: git show --pretty (git diff doesn't seem to support --pretty)
-2

If you really really need colour in your life this might work:

# pick your colours
Red='\033[0;31m'; Colour_Off='\033[0m'; Cyan='\033[0;36m'; 

# find another branch (you could enter <branch>)
BRANCH=$(git br|grep -v '*');

# take the output of git diff and pain the first column red and the reset cyan.
git diff $BRANCH --name-status|awk "{print \"$Red\" \$1 \"\t\" \"$Cyan\" \$2 \"$Colour_Off\"}"

You could create an alias for this in bash or a paint.sh

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.