Git log tabular formatting
The following command will print the log in a tabular form
git log --pretty=format:'%h|%an|%s' -10 | column -t -s '|'
the column command "columnates" the output, using the "|" as field separator, it will find out the optimal column width given the input, so it would work well even if you have more fields.
On Linux it will work ok as long as you don't use colors, the Linux implementation doesn't handle well the ANSI escape codes.
But in Mac OS X, it will handle colors and you can use any unicode character as field delimiter. I use ∑ since I'm pretty sure it won't occur by chance on the commit text. |
is a bad choice because it could appear in the commit description and also if you use --graph --decorate --all
it will appear as part of the symbols used to draw the graph
You can also do:
git log --pretty=format:'%h %<(20)%an %s' -10
No need for shell magic or post processing with awk
, column
etc.
In Git 1.8.3 and later, there is native support for this in the pretty format, using the %<(N)
syntax to format the next placeholder as N columns wide:
$ git log --pretty=format:'%h %<(20)%an %s' -10
For versions before 1.8.3, the previous version of this answer, retained below, should work.
Here's a solution in Bash using read
to parse the log lines back apart, and printf
to print them out, with a fixed width field for the authors name so the columns will stay lined up. It assumes that |
will never appear in the author's name; you can choose another delimiter if you think that might be a problem.
git log --pretty=format:'%h|%an|%s' -10 |
while IFS='|' read hash author message
do
printf '%s %-20s %s\n' "$hash" "$author" "$message"
done
You can create this as an alias using:
[alias]
mylog = "!git log --pretty=format:'%h|%an|%s' -10 | while IFS='|' read hash author message; do printf '%s %-20s %s\n' \"$hash\" \"$author\" \"$message\"; done"
I'm sure you could do this in fewer characters using awk
but as the saying goes,
Whenever faced with a problem, some people say “Lets use AWK.” Now, they have two problems.
Of course, having said that, I had to figure out how to do it in awk
which is a bit shorter:
git ... | awk -F '|' '{ printf "%s %-20s %s\n", $1, $2, $3 }'