Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add color to bash output of `git diff --name-status` by file status?

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?

like image 933
MicFin Avatar asked Mar 01 '17 16:03

MicFin


People also ask

What do the colors mean in git diff?

In the diff view there is a file tree showing all of the changes for that pull request. The colour of a particular file indicates the file change type: Green - ADDED (a new file was added) Blue - MODIFIED (an existing file was modified) Brown - COPIED (an existing file was copied to create a new file)

What is the difference between the git diff and git status?

The main difference between the commands is that git diff is specially aimed at comparisons, and it's very powerful at that: It can compare commits, branches, a single file across revisions or branches, etc. On the other hand, git status is specifically for the status of the working tree.

What does green mean in git diff?

Sep 13, 2021 at 17:16. 1. Git's original system is just two (well, three) colors: default for unchanged, red for deleted, green for added.


1 Answers

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

like image 71
Matthew Avatar answered Sep 28 '22 06:09

Matthew