Search code examples
gitcommand-line

How to change Git log date formats


I am trying to display the last commit within Git, but I need the date in a special format.

I know that the log pretty format %ad respects the --date format, but the only --date format I can find is "short". I want to know the others, and whether I can create a custom one such as:

git -n 1 --date=**YYMMDDHHmm** --pretty=format:"Last committed item in this release was by %%an, %%aD, message: %%s(%%h)[%%d]"

Solution

  • The others are (from git help log):

    --date=(relative|local|default|iso|rfc|short|raw)
      Only takes effect for dates shown in human-readable format,
      such as when using "--pretty".  log.date config variable
      sets a default value for log command’s --date option.
    
    --date=relative shows dates relative to the current time, e.g. "2 hours ago".
    
    --date=local shows timestamps in user’s local timezone.
    
    --date=iso (or --date=iso8601) shows timestamps in ISO 8601 format.
    
    --date=rfc (or --date=rfc2822) shows timestamps in RFC 2822 format,
      often found in E-mail messages.
    
    --date=short shows only date but not time, in YYYY-MM-DD format.
    
    --date=raw shows the date in the internal raw git format %s %z format.
    
    --date=default shows timestamps in the original timezone
      (either committer’s or author’s).
    

    There is no built-in way that I know of to create a custom format, but you can do some shell magic.

    timestamp=`git log -n1 --format="%at"`
    my_date=`perl -e "print scalar localtime ($timestamp)"`
    git log -n1 --pretty=format:"Blah-blah $my_date"
    

    The first step here gets you a millisecond timestamp. You can change the second line to format that timestamp however you want. This example gives you something similar to --date=local, with a padded day.


    And if you want permanent effect without typing this every time, try

    git config log.date iso 
    

    Or, for effect on all your git usage with this account

    git config --global log.date iso