A git log history graph with one line per commit, in color, with dates
Asked Answered
B

5

23

I need to have format like:

git log --decorate --graph --oneline --date-order

but I need it also:

  1. to contain date (short)
  2. to have the same colors

I tried:

git log --decorate --graph --oneline --date-order \
--date=short --pretty=format:"%h %ad %s"

but it's harder to read (no colors) and does not include branches/tags


The closest simple solution is(thanks VonC):

git log --graph --pretty=format:'%C(yellow)%h%Creset \
-%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' \
--abbrev-commit --date=short
Basophil answered 15/9, 2011 at 11:33 Comment(2)
You can use VonC's example but for refspec use %C(auto)%d to get the references automatically coloredStaysail
appending an answer into one's question is confusing. Better to use stack overflow correctly by accepting the correct answer.Smorgasbord
A
16

You can try:

alias.lgb=log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=short --branches

It has different color, but you can change them easily.

for instance:

git log --graph --pretty=format:'%Cred%h -%d %s (%cr) <%an>%Creset' --abbrev-commit --date=short --branches
Airsickness answered 15/9, 2011 at 11:36 Comment(7)
Note: Use %ad instead of %an to get back your short dates.Airsickness
that looks pretty nice, except that branches do not have different color depending on if they are local or remote and also tags have the same color as branches. it's very useful to distinguish these. Also HEAD is light blue - shows you where you are at the momentBasophil
@NickSoft: on the color issue for tag and branches, see https://mcmap.net/q/12731/-color-in-git-log/…Airsickness
well they say it's impossible, which doesn't quite work for me. isn't there any way to add date when using --oneline and not custom format?Basophil
@NickSoft: it doesn't seem possible, not to my knowledge at least.Airsickness
+1 For being pretty close to my requirements and for the simplicity of the solution.Basophil
+1, Love it. I'm collecting useful formats in my [pretty] config section, and this one's my default :)Meeting
B
6

Well, "impossible" means that there is no easy way and I'll have to do it myself. I worried too much that I always make things the hard way, when there is actually easier way.

Here is a bash+php script. I tried to make it with sed, but I failed.

I named this script git-gd and put it in a bin directory that's in path /usr/local/bin/ and I use it with git: git gd or git gd <options>

#!/bin/bash

GIT="/usr/local/bin/git"
PHP="/bin/php"
GIT_DATES="$GIT log --date=short --abbrev-commit --pretty=format:%C(yellow)%h_%C(green)[%ad]%Creset --branches --color=always $*"
#if you have have alias g
GIT_GRAPH="$GIT g --color=always"
#or
#GIT_GRAPH="$GIT log --decorate --graph --oneline --date-order --color=always"
PHP_SCRIPT='
  $exps = explode("\n", $_SERVER["argv"][1]);
  $lines = file("php://stdin");
  $s = array();
  $r=$s;
  foreach($exps as $exp){
   $exp = trim($exp);
   list($commit,)=explode("_", $exp);
   $s[] = $commit;
   $r[] = str_replace("_", " ", $exp);
  }
  foreach($lines as $line){
    $line = str_replace($s, $r, $line);
    echo $line ;
  }
  '

DATES=`$GIT_DATES`
$GIT_GRAPH $* |$PHP -r "$PHP_SCRIPT" -- "$DATES"

I'll wait a bit for simpler solution and I'll accept my own answer

Basophil answered 15/9, 2011 at 13:51 Comment(0)
P
5

In non-ancient versions of git you can configure git log to enable decorations thusly:

git config --global log.decorate full
Parfitt answered 23/3, 2012 at 17:47 Comment(1)
log.decorate=full causes the ref names to be printed with their prefixes (refs/heads/, etc.); I find log.decorate=short more useful.Jinx
C
2

In addition in your git config you can add two lines like this:

[format]
  pretty = %Cblue%h%Creset %Cgreen[%ar]%Creset (%an) %s 

This just means you can type git log and it will always be formatted.

Costard answered 16/7, 2013 at 14:44 Comment(0)
N
1

To log your commits neatly and formatted use

git log --pretty=format:"%h - %an, %ar : %s"  
  • see commits on branch
Neddie answered 17/8, 2021 at 9:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.