Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 439
Charlie Salts Avatar asked Nov 21 '17 13:11

Charlie Salts


People also ask

What is the output of git log?

By default, git log includes merge commits in its output. But, if your team has an always-merge policy (that is, you merge upstream changes into topic branches instead of rebasing the topic branch onto the upstream branch), you'll have a lot of extraneous merge commits in your project history.

What is skipping in git log?

Skipping is showing because git is using less as a pager by default. less "skips" line when you scroll up or down manually.

What happens when you run git log?

The git log command displays all of the commits in a repository's history. By default, the command displays each commit's: Secure Hash Algorithm (SHA)


1 Answers

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.

like image 100
Mark Adelsberger Avatar answered Oct 22 '22 11:10

Mark Adelsberger