iconoclast
iconoclast

Reputation: 22670

Why is my git log output getting mangled when I pipe it?

If I let Git use its built-in head-style log truncation, I get exactly what I expect:

$ git log --color=always --graph -5                                                                               

* 5abf1e7b0aba45e895bbe5b235f2326ad808ad30 (HEAD -> feature/blahblah, origin/feature/blahblah) blah blah last commit message
* c7b065e3d06ed8066ded283c2feec17e9f5a95d6 whitelist the new field
* 6b4332b951b2b37dec4c0fc67defb778dbdc29d7 add blah blah model
* 323c7570388f13aad8144292ebeb31187eb1c742 add blah blah to the database
* bceae36bda53b48679556eb00ee8321f1c8392e6 (origin/feature/yadayada, feature/yadayada) add blah blah to each blah blah

But if I use head, it removes the branch markers (which I want!):

$  git log --color=always --graph | head -5                                                                        

* 5abf1e7b0aba45e895bbe5b235f2326ad808ad30 blah blah last commit message
* c7b065e3d06ed8066ded283c2feec17e9f5a95d6 whitelist the new field
* 6b4332b951b2b37dec4c0fc67defb778dbdc29d7 add blah blah model
* 323c7570388f13aad8144292ebeb31187eb1c742 add blah blah to the database
* bceae36bda53b48679556eb00ee8321f1c8392e6 add blah blah to each blah blah

How can I avoid losing these branch markers when I pipe the output of git-log to another tool?

(I'm trying to pipe output to fzf, and in order to get things the way I want them, it needs to be reversed, but Git does not allow --graph and --reverse to be used together, so I must reverse the output with an outside utility like tac (actually gtac on macOS), which causes me to lose the branch markers, which is one of three main reasons I'm writing my own function instead of using the fcs example here.)

Upvotes: 3

Views: 271

Answers (1)

torek
torek

Reputation: 489638

You're referring specifically to the (HEAD -> feature/foo) decorations, which sometimes appear, and sometimes don't, somehow magically automatically detecting whether you have piped git log output through some other program.

These decorations are added under control of various flags, as described in the git log manual page:

--decorate[=short|full|auto|no]

    Print out the ref names of any commits that are shown. If short is specified, the ref name prefixes refs/heads/, refs/tags/ and refs/remotes/ will not be printed. If full is specified, the full ref name (including prefix) will be printed. If auto is specified, then if the output is going to a terminal, the ref names are shown as if short were given, otherwise no ref names are shown. The default option is short.

(the boldface here is mine).

Older versions of Git are not as fancy, and some older versions default to no rather than auto or short, but the option has been around for a long time. Moreover, you can configure, in your per-user git config --global settings, your own default. You probably either have a version of Git that defaults to auto (this one is my bet) or have configured your setting to auto.

Upvotes: 4

Related Questions