Charlie Salts
Charlie Salts

Reputation: 13488

Output from git log gets lost when piped to file - what am I missing?

I am trying to get some information about some git commits via the command line as part of a larger automated tool I am building. The information I want is available via this git log command:

git log --branches --graph --oneline --parents

which produces this output:

enter image description here

This is great, because this has the hashes and tags that I want, as well as the commit messages. However, when I pipe this to a file, the stuff in the brackets seems to somehow get lost. I'm not too interested in the colour, but I do want just the plain text as I would expect from any *nix-like program.

This is the output I seem to get instead, which omits some of the output I want (eg, the tag information):

enter image description here

I'm not sure how or why this information gets lost when being piped somewhere. I feel like this might be something incredibly simple and obvious.

I experience the same problem whether I do this in Bash on Arch Linux (with the latest version of git) or in the MINGW64 Bash environment in Windows.

Question: How can I completely capture git log's output without losing the information that is being lost when piping to a file?

Upvotes: 19

Views: 3057

Answers (2)

Mark Adelsberger
Mark Adelsberger

Reputation: 45659

You need to add the --decorate option to your log command. Set it either as --decorate=short or --decorate=full.

It appears in your config you've probably got log.decorate set to auto, which means that tags and such are displayed (in short form) when writing to the terminal, but not to a pipe or other file.

Similarly there are config values and command options that govern if (and when) color codes are output; so

git log --branches --graph --oneline --parents --decorate=short --color=always

would output the tags and colors even when redirected to a file.

Note that when scripting you should probably include these options on the command line rather than make assumptions about what config values are set. Depending on what you do with the output, log may or may not be the best command to use in scripting anyway, as git commands are somewhat divided into those meant for human consumption vs those mean for scripting.

Upvotes: 22

ghoti
ghoti

Reputation: 46816

Your git command:

git log --branches --graph --oneline --parents

does not produce the same output for me that you show in your first example. It does, however, produce output similar to the second example. The ref names in the brackets (branches and tags) are included when you add the --decorate option. (Here's the documentation.)

Note that your git log format can be controlled in your ~/.gitconfig file. For example, the format you've shown in your question looks like it might be achieved with options like this:

git log --decorate --graph --all --format='%C(auto,yellow)%h%C(auto,reset) %C(auto,yellow)%p%C(auto,reset) %C(auto,red)%d%C(auto,reset) %s %C(auto,green)%an%C(auto,reset) (%C(auto,cyan)%ar%C(auto,reset))'

If you are trying to automate things, you can specify a format that is better tuned to your requirements. Check the git documentation for pretty-formats for details. In particular, look for the %C format, and the %C(auto,…​) notation, which causes colour controls to be used only if the command is run from a terminal.

If you always want to generate the colour information regardless of whether you're using git log interactively, you can remove each ocurrence of auto, in the above line (or alias).

Upvotes: -2

Related Questions