Keep color in 'git log' when piping to 'head' (osx)
Asked Answered
E

1

12

I have the following aliases in my bashrc file.

alias gl="git log --graph --abbrev-commit --decorate --date=relative --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(bold yellow)%d%C(reset)' --all"
alias glh="gl | head -n 20"

Both of the aliases used to print the output of git history in color. However with the update of git to git version 2.19.1 glh stopped printing the output in color. gl still prints the output in color. Is there a way to force git log to keep the color even though it is piped to head?

OS:

ProductName:    Mac OS X
ProductVersion: 10.11.6
BuildVersion:   15G22010
Eastertide answered 19/11, 2018 at 19:42 Comment(4)
using --color?Kob
And maybe use git log -n 20 to display 20 commits, instead of head.Prent
Both ways described above worked thank you!Eastertide
--color is better since there are other piped commands that also prevent coloring (regex manipulation in my case).Gamesmanship
B
7

You can either specify --color=always and pipe to another program which must interpret the color escape sequences, or you can get rid of the pipe to head and use git log -n20 directly to display only the last 20 commits. (Note that the latter could give you a different result than using head).

Bugaboo answered 20/9, 2021 at 18:8 Comment(1)
(Though -n option is probably superior anyway) I'd add --color=always should come after git log and may not work in some other git commands. See a bit more comprehensive answer.Uracil

© 2022 - 2024 — McMap. All rights reserved.