我试图在Git中显示最后一次提交,但我需要特殊格式的日期。
我知道log pretty格式%ad尊重——date格式,但我能找到的唯一——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格式,但我能找到的唯一——date格式是“short”。我想知道其他的,以及我是否可以创建一个自定义的,如:
git -n 1 --date=**YYMMDDHHmm** --pretty=format:"Last committed item in this release was by %%an, %%aD, message: %%s(%%h)[%%d]"
当前回答
date -d @$(git log -n1 --format="%at") +%Y%m%d%H%M
请注意,这将转换为您的本地时区,以防这对您的用例很重要。
其他回答
除了——date=(relative|local|default|iso|iso-strict|rfc|short|raw),还可以使用自定义日志日期格式with
--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 log -n1 --pretty='format:%cd' --date=format:'%Y-%m-%d %H:%M:%S'
——date=format格式化日期输出,其中——pretty告诉打印什么。
其他的是(来自git帮助日志):
--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 log -n1 --format="Last committed item in this release was by %an, `git log -n1 --format=%at | awk '{print strftime("%y%m%d%H%M",$1)}'`, message: %s (%h) [%d]"
在花了很长时间寻找一种方法来让git日志以YYYY-MM-DD格式输出日期,以一种更少的方式工作后,我想出了以下格式:
%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'