为了避免更改 git 配置,您可以通过传递配置变量来仅为当前命令启用颜色-c
.
For the status
命令,变量是color.status
:
git -c color.status=always status | less -REX
For diff
, show
, log
and grep
命令,变量是color.ui
:
git -c color.ui=always diff | less -REX
注意-c
必须来before the status
or diff
争论,而不是之后。
或者,对于diff
, show
, log
and grep
命令,您可以使用--color=always
after命令:
git diff --color=always | less -REX
注意:正如史蒂文所说,如果您尝试提取有意义的数据,那么您可以使用而不是解析颜色来提取含义--porcelain
以获得更适合解析器的输出。
git status --porcelain | awk ...
然后,如果您愿意,可以稍后重新引入颜色。
要获取用户配置的颜色,您可以使用git config --get-colour
:
reset_color="$(tput sgr0)"
remote_branch_color="$(git config --get-color color.branch.remote white)"
echo "Pushing to ${remote_branch_color}${branch_name}${reset_color}"
更多例子here https://github.com/joeytwiddle/dotfiles/blob/3081c5d034c6c11907b9b290d97f815848fceb68/j/config/auto.sh/git_aliases#L1430-L1432.