如何更改Git日志日期格式

213

我想在Git中显示最新的提交信息,但需要以特定格式显示日期。

我知道log pretty格式%ad支持--date参数,但是我只能找到 "short" 这个日期格式,我想知道其他可用的日期格式,是否可以创建自定义日期格式,例如:

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

2
我总是觉得官方的Linux内核Git文档是这类问题的绝佳资源。 - user456814
5
注意:现在(2014年11月,Git 2.2),您可以使用 --date=iso-strict参数:请参见我的答案 - VonC
7
从Git 2.7(2015年第四季度)开始,您可以要求使用本地时区来格式化任何日期!请参见下面的回答。这意味着除了--date=(relative|local|default|iso|iso-strict|rfc|short|raw)之外,您还将拥有:--date=(relative-local|default-local|iso-local|iso-strict-local|rfc-local|short-local|raw-local) - VonC
12个回答

287

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

--date=format:'%Y-%m-%d %H:%M:%S'       # committer's timezone
--date=format-local:'%Y-%m-%d %H:%M:%S' # current user's timezone

这会输出类似于2016-01-13 11:32:13的东西。

注意:如果你查看下面链接的提交,我相信你至少需要Git v2.6.0-rc0才能使其工作。

在完整的命令中它将是这样的:

git config --global alias.lg "log --graph --decorate -30 --all --topo-order --date=format-local:'%Y-%m-%d %H:%M:%S' --pretty=format:'%C(cyan)%h%Creset %C(black bold)%ad%Creset%C(auto)%d %s'"

我无法在文档中找到这个信息(如果有人知道在哪里找到,请评论),因此我最初是通过试错找到这些占位符的。

在寻找此文档时,我发现了一个提交给 Git 自身的 提交记录,指示格式直接提供给了 strftime。查看 strftime (这里这里),我找到的占位符与列出的占位符匹配。

这些占位符包括:

%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
%%      Percent sign

4
@Shiva:我最近遇到了一个类似的问题,可能是你也遇到了同样的事情。我正在使用旧版的 Git 配对机。当我升级 Git 后,自定义日期格式开始工作。如果您查看上面链接的提交,我认为您需要至少 v2.6.0-rc0 版本。给出的示例正是我所使用的。在完整的命令中,它将类似于 git config --global alias.lg "log --graph --decorate -30 --all --date-order --date=format:'%Y-%m-%d %H:%M:%S' --pretty=format:'%C(cyan)%h%Creset %C(black bold)%ad%Creset%C(auto)%d %s'" - Ben Allred
1
谢谢Ben!我按原样粘贴了你的命令并运行了git lg,但我仍然收到以下错误:fatal: unknown date format format:%Y-%m-%d %H:%M:%S。我正在Windows上运行git。这是我的git版本:git version 1.9.5.msysgit.1。那么我需要升级到新版本吗? - Shiva
1
提供完整的示例(以便我以后可以找到它):git log -1 --pretty="format:blah-%ad" --date="format:%Y-%m-%d-%H-%M-%S",截至本文撰写时输出为:blah-2016-09-14-15-17-55。参考我的git --versiongit version 2.6.0 - Eric Dobbs
2
我该如何将 --date=format:'%Y-%m-%d %H:%M:%S' 放入 .gitconfig 中? - Martin Vegter
1
应该更彻底地搜索谷歌‍♂️ 对于其他也在寻找格式化的本地时间的人,您可以使用--date=format-local:"%Y-%m-%d..."。 实际上,任何日期选项都支持-local后缀 iso-localrfc-local等... 取自https://dev59.com/W2Mm5IYBdhLWcg3wDrpR#32990722 - Shaun Mitchell
显示剩余13条评论

210

其他选项(来自于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

18
如果您想要永久生效而不需要每次输入此命令,请尝试类似于 git config log.date iso 的命令,或者对于在此帐户下的所有 git 使用都生效的命令,请使用 git config --global log.date iso - Stéphane Gourichon
15
您可以使用自己的格式。参见Ben Allred的答案--format:<args>将把<args>传递给strftime。 - Captain Man

39

在长时间寻找一种方法来让 git log 命令以 YYYY-MM-DD 的日期格式输出,并且在 less 中也能正常使用后,我想出了以下的格式:

%ad%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08

使用开关--date=iso,可以将日期以ISO格式(长格式)打印出来,然后打印14次退格字符(0x08)。在我的终端中,这样做可以有效地删除YYYY-MM-DD部分之后的所有内容。例如:

git log --date=iso --pretty=format:'%ad%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08%aN %s'

这将得到类似于以下内容:

2013-05-24 bruno This is the message of the latest commit.
2013-05-22 bruno This is an older commit.
...
我所做的是创建一个名为l的别名,并对上述格式进行一些调整。它将提交图形显示在左侧,然后是提交的哈希,接着是日期、短名称、引用名称和主题。该别名如下(位于~/.gitconfig):
[alias]
        l = log --date-order --date=iso --graph --full-history --all --pretty=format:'%x08%x09%C(red)%h %C(cyan)%ad%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08 %C(bold blue)%aN%C(reset)%C(bold yellow)%d %C(reset)%s'

2
这对我非常有效。使用6个%x08正好可以去除尾随的时区。 - Penghe Geng
10
--date=short 参数仅显示日期而不显示时间,格式为 YYYY-MM-DD。 - Paaske
3
在 zsh 和 bash 中,这会将输出显示在终端上,但如果将其管道传输到其他程序,则“退格”数据仍然存在。这意味着像 | sort | uniq 这样的命令无法正常工作。 - chmac
你可以使用 cut -d" " -f1,4- 来去除不需要的日期部分。然后 | sort | uniq 就能正常工作了。 - Ivan Andrus
6
这既荒谬又令人惊叹。我正在使用“%C(bold cyan)%ai%x08%x08%x08%x08%x08%x08%x08%x08%x08%C(reset) %C(bold green)(%ar%x08%x08%x08%x08)%C(reset)”格式,以获取“2015年01月19日 11:27(11天)”的时间格式。 +1 - naught101
2
现在这个答案 https://dev59.com/Y2sz5IYBdhLWcg3wg4Cv#34778736 更加适用(自 Git v2.6.0-rc0 起) - radistao

37

我也需要同样的东西,以下方法适用于我:

git log -n1 --pretty='format:%cd' --date=format:'%Y-%m-%d %H:%M:%S'

--date=format 格式化日期输出,--pretty 指定要打印的内容。


2
这对我不起作用。我得到了“未知日期格式”。 - Jim
你使用的 Git 版本是哪个? - lac_dev
使用 https://medium.com/@katopz/how-to-upgrade-git-ff00ea12be18 进行更新。 - amateur barista
1
git log --pretty=format:"%h%x09%cd%x09%s" --date=format:'%Y-%m-%d %H:%M:%S %p' - amateur barista
@amateurbarista,这并没有显示最后一次提交,请仔细阅读请求。ar3 正试图显示最后一次提交,而不是最近几次提交。 - lac_dev

26

您可以使用字段截断选项来避免出现太多的%x08字符。例如:

git log --pretty='format:%h %s%n\t%<(12,trunc)%ci%x08%x08, %an <%ae>'

等同于:

git log --pretty='format:%h %s%n\t%ci%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08, %an <%ae>'

这样看起来更加舒适。

对于这个例子来说,更好的方法是使用%cd,它会遵循--date=<format>参数,所以如果你想要YYYY-MM-DD的格式,你可以这样做而不需要用到%<%x08

git log --date=short --pretty='format:%h %s%n\t%cd, %an <%ae>'

我刚刚注意到这与原帖有些循环引用,但我还是会保留它,以防其他人使用与我相同的搜索参数到达此处。


18
请注意"date=iso"格式:它并不完全符合ISO 8601标准。
参见Git 2.2.0(2014年11月)的提交"466fb67",作者是bbolliBeat Bolli)。

pretty: 提供严格的ISO 8601日期格式

由于小差异,Git的"ISO"日期格式实际上并不符合ISO 8601标准,并且无法被仅支持ISO 8601的解析器(例如XML工具链中的解析器)解析。

从"--date=iso"输出的内容与ISO 8601存在以下差异:

  • 日期/时间分隔符T被替换为空格
  • 时间和时区之间有一个空格
  • 时区的小时和分钟之间没有冒号

添加了一个严格的ISO 8601日期格式来显示提交者和作者的日期。
使用'%aI'和'%cI'格式说明符,并添加'--date=iso-strict'或'--date=iso8601-strict'日期格式名称。

参见此线程以了解更多讨论。

13
date -d @$(git log -n1 --format="%at") +%Y%m%d%H%M

请注意,这将转换为您的本地时区,如果这对您的使用情况很重要。


谢谢。我用 %ct 进行了微调。 - cagney

9

Git 2.7 (2015年第四季度) 将会引入 -local 命令。
这意味着,除了:

--date=(relative|local|default|iso|iso-strict|rfc|short|raw)

你还将拥有:

--date=(default-local|iso-local|iso-strict-local|rfc-local|short-local)
< p > < em > -local 后缀不能与 rawrelative 一起使用。参考

现在您可以请求使用本地时区的任何日期格式。请参见

请查看提交记录 add00ba, 提交记录 547ed71 (2015年9月3日) 作者为Jeff King (peff)
(由Junio C Hamano -- gitster --提交记录 7b09c45中合并, 2015年10月5日)

特别地,上述最后一个提交记录 (add00ba) 提到:

date: 将"local"变为日期格式的正交部分:

我们的大多数"--date"模式都涉及日期的格式:我们显示哪些项目以及以什么顺序显示。
但"--date=local"有点另类。它表示“按常规格式显示日期,但使用本地时区”。
我们使用的时区与实际格式正交,因此我们可以没有理由有“本地化的iso8601”等格式。

此补丁将一个"local"布尔字段添加到"struct date_mode"中,并从date_mode_type枚举中删除DATE_LOCAL元素(现在只剩下DATE_NORMAL加上local = 1)。
用户可以通过将"-local"添加到任何日期模式(例如,"iso-local")来访问新功能,并为向后兼容性保留"local"作为"default-local"的别名。


2
我必须指出format-local也可以工作!因此,将喜爱的格式和本地化的力量结合起来非常完美。 - acgtyrant

7

我需要一个特殊格式的日期。

在 Git 2.21 (2019年第一季度) 中,引入了一个新的日期格式 "--date=human",它会根据时间离当前时间的远近改变其输出。

当输出将要发送到分页器或终端时,可以使用 "--date=auto" 来使用这种新格式,否则使用默认格式。

请查看提交 110a6a1提交 b841d4f(2019年1月29日),以及提交 038a878提交 2fd7c22(2019年1月21日)由Stephen P. Smith(“”)撰写。
请查看提交 acdd377(2019年1月18日)由Linus Torvalds(torvalds撰写。
(由Junio C Hamano -- gitster --提交 ecbe1be中合并,2019年2月7日)

添加 "人类可读" 日期格式的文档

显示日期和时间信息的格式类似于人们在其他情境中书写日期的方式。
如果没有指定年份,则读者推断给定的日期是当前年份
通过不显示冗余信息,读者可以集中注意力在不同的信息上
该补丁报告相对日期,基于从运行git命令的机器上推断的信息,在执行命令时报告相对日期。
尽管通过删除推断的信息,该格式对人类更有用,但并没有使其真正成为人类可读的。
如果'relative'日期格式尚未实现,则使用'relative'将是合适的。

添加human日期格式测试。

当使用human时,根据参考日期和本地计算机日期之间的时间差,会禁止几个字段。
  • 在时间差小于一年的情况下,会省略年份字段。
  • 如果时间少于一天,则省略月份和年份。
check_date_format_human 18000       "5 hours ago"       #  5 hours ago
check_date_format_human 432000      "Tue Aug 25 19:20"  #  5 days ago
check_date_format_human 1728000     "Mon Aug 10 19:20"  #  3 weeks ago
check_date_format_human 13000000    "Thu Apr 2 08:13"   #  5 months ago
check_date_format_human 31449600    "Aug 31 2008"       # 12 months ago
check_date_format_human 37500000    "Jun 22 2008"       #  1 year, 2 months ago
check_date_format_human 55188000    "Dec 1 2007"        #  1 year, 9 months ago
check_date_format_human 630000000   "Sep 13 1989"       # 20 years ago

将建议的“auto”模式替换为“auto:”。

In addition to adding the 'human' format, the patch added the auto keyword which could be used in the config file as an alternate way to specify the human format. Removing 'auto' cleans up the 'human' format interface.

Added the ability to specify mode 'foo' if the pager is being used by using auto:foo syntax.
Therefore, 'auto:human' date mode defaults to human if we're using the pager.
So you can do:

git config --add log.date auto:human

and your "git log" commands will show the human-legible format unless you're scripting things.


Git 2.24(2019年第四季度)简化了代码。

请参见提交记录47b27c9提交记录29f4332(由Stephen P. Smith(“”)于2019年9月12日提交)。
(由Junio C Hamano -- gitster --合并于提交记录36d2fca,2019年10月7日)

停止向日期代码传递“now”

提交 b841d4f (添加 human 格式到测试工具,2019年1月28日,Git v2.21.0-rc0)添加了一个 get_time() 函数,它允许环境中的 $GIT_TEST_DATE_NOW 覆盖当前时间。
因此,我们不再需要在 cmd__date() 中解释该变量。

因此,我们可以停止通过日期函数传递 "now" 参数,因为没有人使用它们。
请注意,我们确实需要确保所有以前带有 "now" 参数的调用者正确使用 get_time()


在 Git 2.32 (Q2 2021) 中, "git log --format=..."(man) 占位符学会了使用 %ah/%ch 占位符请求 --date=human 输出。

请看提交 b722d45(2021年4月25日)由ZheNing Hu(adlternative
(由Junio C Hamano -- gitster --提交 f16a466中合并,2021年5月7日)

pretty:提供人类可读的日期格式

签署者:ZheNing Hu

请在格式中添加占位符%ah%ch,以格式化作者日期和提交者日期,就像--date=human一样,提供更人性化的日期输出。现在,pretty-formats在其手册页面中包含以下内容:
'%ah':作者日期,人性化风格(类似于git rev-list--date=human选项)
并在其手册页面中包含以下内容:

'%ch':: 提交者日期,人类风格(类似于git rev-list命令的--date=human选项)


5

我想要的是格式选项%ai

%ai:作者日期,类似于ISO 8601的格式

--format="%ai"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接