我想在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]"
我想在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]"
除了如其他人所提到的--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
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 Allredgit lg
,但我仍然收到以下错误:fatal: unknown date format format:%Y-%m-%d %H:%M:%S
。我正在Windows上运行git。这是我的git版本:git version 1.9.5.msysgit.1
。那么我需要升级到新版本吗? - Shivagit log -1 --pretty="format:blah-%ad" --date="format:%Y-%m-%d-%H-%M-%S"
,截至本文撰写时输出为:blah-2016-09-14-15-17-55
。参考我的git --version
是git version 2.6.0
。 - Eric Dobbs--date=format:'%Y-%m-%d %H:%M:%S'
放入 .gitconfig
中? - Martin Vegter--date=format-local:"%Y-%m-%d..."
。 实际上,任何日期选项都支持-local
后缀 iso-local
,rfc-local
等... 取自https://dev59.com/W2Mm5IYBdhLWcg3wDrpR#32990722 - Shaun Mitchell其他选项(来自于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
git config log.date iso
的命令,或者对于在此帐户下的所有 git 使用都生效的命令,请使用 git config --global log.date iso
。 - Stéphane Gourichon在长时间寻找一种方法来让 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'
--date=short
参数仅显示日期而不显示时间,格式为 YYYY-MM-DD。 - Paaske| sort | uniq
这样的命令无法正常工作。 - chmaccut -d" " -f1,4-
来去除不需要的日期部分。然后 | sort | uniq
就能正常工作了。 - Ivan Andrus我也需要同样的东西,以下方法适用于我:
git log -n1 --pretty='format:%cd' --date=format:'%Y-%m-%d %H:%M:%S'
--date=format
格式化日期输出,--pretty
指定要打印的内容。
git log --pretty=format:"%h%x09%cd%x09%s" --date=format:'%Y-%m-%d %H:%M:%S %p'
- amateur barista您可以使用字段截断选项来避免出现太多的%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>'
我刚刚注意到这与原帖有些循环引用,但我还是会保留它,以防其他人使用与我相同的搜索参数到达此处。
date=iso
"格式:它并不完全符合ISO 8601标准。bbolli
(Beat Bolli)。
参见此线程以了解更多讨论。由于小差异,Git的"ISO"日期格式实际上并不符合ISO 8601标准,并且无法被仅支持ISO 8601的解析器(例如XML工具链中的解析器)解析。
从"
--date=iso
"输出的内容与ISO 8601存在以下差异:
- 日期/时间分隔符
T
被替换为空格- 时间和时区之间有一个空格
- 时区的小时和分钟之间没有冒号
添加了一个严格的ISO 8601日期格式来显示提交者和作者的日期。
使用'%aI
'和'%cI
'格式说明符,并添加'--date=iso-strict
'或'--date=iso8601-strict
'日期格式名称。
date -d @$(git log -n1 --format="%at") +%Y%m%d%H%M
请注意,这将转换为您的本地时区,如果这对您的使用情况很重要。
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
后缀不能与 raw
或 relative
一起使用。参考。
现在您可以请求使用本地时区的任何日期格式。请参见
johnkeeping
) 提交。 请查看提交记录 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
"的别名。
format-local
也可以工作!因此,将喜爱的格式和本地化的力量结合起来非常完美。 - acgtyrant我需要一个特殊格式的日期。
在 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 theauto
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 usingauto:foo
syntax.
Therefore, 'auto:human
' date mode defaults tohuman
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
选项)
我想要的是格式选项%ai
:
%ai
:作者日期,类似于ISO 8601的格式
--format="%ai"
--date=iso-strict
参数:请参见我的答案。 - VonC--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