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
Inigo
  • 12,186
  • 5
  • 41
  • 70
NickSoft
  • 3,215
  • 5
  • 27
  • 48
  • 2
    You can use VonC's example but for refspec use %C(auto)%d to get the references automatically colored – skajfes Aug 18 '15 at 07:04
  • appending an answer into one's question is confusing. Better to use stack overflow correctly by accepting the correct answer. – Max MacLeod Jul 03 '19 at 14:52

5 Answers5

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
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Note: Use `%ad` instead of `%an` to get back your short dates. – VonC Sep 15 '11 at 11:46
  • 2
    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 moment – NickSoft Sep 15 '11 at 12:02
  • 1
    @NickSoft: on the color issue for tag and branches, see http://stackoverflow.com/questions/5889878/color-in-git-log/5892582#5892582 – VonC Sep 15 '11 at 12:04
  • 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? – NickSoft Sep 15 '11 at 12:28
  • @NickSoft: it doesn't seem possible, not to my knowledge at least. – VonC Sep 15 '11 at 12:50
  • +1 For being pretty close to my requirements and for the simplicity of the solution. – NickSoft Sep 15 '11 at 13:53
  • +1, Love it. I'm collecting useful formats in my `[pretty]` config section, and this one's my default :) – orip Jul 15 '12 at 08:03
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

NickSoft
  • 3,215
  • 5
  • 27
  • 48
5

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

git config --global log.decorate full
Henrik Gustafsson
  • 51,180
  • 9
  • 47
  • 60
  • 2
    `log.decorate=full` causes the ref names to be printed with their prefixes (`refs/heads/`, etc.); I find `log.decorate=short` more useful. – musiphil Feb 13 '14 at 23:48
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.

Chris
  • 111
  • 1
  • 5
1

To log your commits neatly and formatted use

git log --pretty=format:"%h - %an, %ar : %s"  
  • see commits on branch
SanRaph
  • 369
  • 3
  • 7