如何通过文件状态为`git diff --name-status`的bash输出添加颜色?

时间:2017-03-01 16:05:28

标签: git bash github git-bash .bash-profile

我想设置git diff --name-status的bash输出样式,以便状态为DMA的文件颜色不同。

要设置常规git bash的样式,我使用color中的.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

要为git log之类的命令设置输出样式,我可以在下面的别名中使用--pretty=format

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

但是,我--pretty=format使用git diff --name-status未成功。

我想要样式的输出目前没有样式,看起来像:

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

有没有办法按状态类型设置输出git diff --name-status的样式?

2 个答案:

答案 0 :(得分:1)

我不确定您是否可以使用--name-status

完全复制其输出而使--pretty=format:...的输出着色

但是,如果您希望文件名的颜色汇总更改,则--stats标记可以传递给许多命令,包括git diff <branch --statgit log --stat

它显示文件名,带有彩色+++--后缀,例如

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

并且+-将根据您的git配置着色

答案 1 :(得分:0)

如果你真的需要生活中的颜色,这可能有用:

# 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\"}"

您可以在bash或paint.sh

中为此创建别名
相关问题