如何更改git日志日期格式?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (3255)

我试图在git中显示最后一次提交,但我需要一个特殊格式的日期。

我知道日志的格式%ad--date,但唯一--date我能找到的格式是“短”。我想知道其他的,以及是否可以创建一个自定义的,例如:

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

其他的是(来自)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).

据我所知,没有内置的方式来创建自定义格式,但是您可以使用一些shell魔术。

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

这里的第一步给你一个毫秒的时间戳。可以将第二行更改为格式化时间戳。这个例子给出了类似于--date=local


如果你不想每次输入这些就能产生永久的效果,那就试试。

git config log.date iso 

或者,对于使用此帐户的所有git使用都有影响。

git config --global log.date iso
用户回答回答于

--date=(relative|local|default|iso|iso-strict|rfc|short|raw),正如其他人所提到的,还可以使用自定义日志日期格式

--date=format:'%Y-%m-%d %H:%M:%S'

这输出了类似于2016-01-13 11:32:13

注:如果查看链接到下面的COMMIT,我相信至少需要Gitv2.6.0-rc0才能工作。

我在任何地方的文档中都找不到这个(如果有人知道在哪里找到它,请评论),所以我最初通过尝试和错误找到了占位符。

在我搜索这方面的文档时,它指示格式直接被传送到strftimestrftime我找到的占位符与列出的占位符匹配。占位符包括

%a        Abbreviated weekday name
%A        Full weekday name
%b        Abbreviated month name
%B        Full month name
%c        Date and time representation appropriate for locale
%d        Day of month as decimal number (01 – 31)
%H        Hour in 24-hour format (00 – 23)
%I        Hour in 12-hour format (01 – 12)
%j        Day of year as decimal number (001 – 366)
%m        Month as decimal number (01 – 12)
%M        Minute as decimal number (00 – 59)
%p        Current locale's A.M./P.M. indicator for 12-hour clock
%S        Second as decimal number (00 – 59)
%U        Week of year as decimal number, with Sunday as first day of week (00 – 53)
%w        Weekday as decimal number (0 – 6; Sunday is 0)
%W        Week of year as decimal number, with Monday as first day of week (00 – 53)
%x        Date representation for current locale
%X        Time representation for current locale
%y        Year without century, as decimal number (00 – 99)
%Y        Year with century, as decimal number
%z, %Z        Either the time-zone name or time zone abbreviation, depending on registry settings; no characters if time zone is unknown
%%        Percent sign

扫码关注云+社区

领取腾讯云代金券