ar3 ar3 - 1 month ago 9x
Git Question

Git Log Date Formats

I am trying to display the last commit within git. I need the date in special format however. I know that the log pretty format %ad respect the --date format, but the only --date format I can find is short. I want to know the others, and if 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]"


The others are (from git help log):

  Only takes effect for dates shown in human-readable format,
  such as when using "--pretty". 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 millis 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.