Question

I have been tinkering with git aliases for some log commands. I have most of what I'd like (credit here), but I'm having trouble with one piece. When I call…

git log --graph --format=format:'%h - [%ar] %s%+d'

…I get…

* ab123f - [6 hours ago] Fix the references
|  (HEAD, origin/master, master)
* bc123f - [8 hours ago] New build syntax
* cd123f - [10 hours ago] Initial import

…where %+d adds a new line and puts the --decorate tags on it if they exist. I would rather have the tags to be in line with the time stamp instead, like so:

* ab123f - [6 hours ago] Fix the references
|          (HEAD, origin/master, master)
* bc123f - [8 hours ago] New build syntax
* cd123f - [10 hours ago] Initial import

How do I accomplish this? I do not want a bonus newline if there are no --decorate tags. I've been experimenting with various format placeholders: %+d, %-d, %+ d (which doesn't work); permutations of %>(<N>), %>>(<N>); and so on, but I can't get it to do what I want.

Colors and further commit info had been removed for simplicity, but they seem to interfere with torek's answer. The full command is below:

git log --graph --format=format:'%C(bold yellow)%h%C(reset) - %C(green)(%ar)%C(reset) %s %C(white)<%an>%C(reset)%C(auto)%+d%C(reset)'
Was it helpful?

Solution

%w as follows seems to do the trick.

git log --graph --format=format:'%h - [%ar] %-s%w(0,0,9)%+d'

Git version 1.8.5.2

OTHER TIPS

Ugh ... using %n%-... almost seems to work, but I still can't get it to do the right thing here.

Aha! '%h - [%ar] %s%n%-w(80,9)%+d' works!

This seems terribly clumsy (add a newline, maybe remove it, wrap lines with indent at 9, add a newline if %d is not empty), but the more obvious versions (with %+w, or %w...%+d without the %n and %- parts) don't work.

You could also use awk—but the columns approach above is cleaner. This might take it all the way to what your looking for.

git log --format=format:'%h - [%ar%] %s %d' | awk -F'(' '{print $1} {if ( $2 != "" ) print "\t  ("$2}'

On the upside, this seems to preserve colors for me.

For curious future visitors, these are what I eventually settled upon:

[alias]
    # Pretty logs
    lg1 = log --graph --date=auto:human --format=tformat:'%C(bold yellow)%h%C(reset) %C(green)%ad%C(reset) %s %C(dim white)%aN%C(reset) %w(0,0,9)%C(auto)%+d%C(reset)'
    lg2 = log --graph --format=tformat:'%C(bold yellow)%h%C(reset) %C(cyan)%aD%C(reset) %C(green)(%ar) %C(dim white)%aN%C(reset)%w(0,0,9)%n %C(brightwhite)%s%C(auto)%+d%C(reset)%w(0,0,10)%+b'
    lg = lg1

    # Monochrome log
    lgs = log --graph --format=format:'%h - [%ar] %s%n%-w(80,9)%+d'
  • git lg or git lg1 for a git log --oneline analogue
  • git lg2 to include the body and full date (but also fuzzy date)
  • git lgs as a simpler fallback in case the colors break

The colors do enough work to distinguish the parts that I no longer wrap most names and times. Also, --date=auto:human wasn't around for the original question, but I like it a lot.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top