我试图在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=(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 --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]"

您可以使用字段截断选项来避免使用太多%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=<格式>,所以如果你想要YYYY-MM-DD,你可以这样做,完全避免%<和%x08:

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

我只是注意到这篇文章相对于最初的帖子有点循环,但我还是把它留着,以防其他人带着和我一样的搜索参数来到这里。

使用Bash和date命令将类似iso的格式转换为您想要的格式。我想要一个组织模式的日期格式(和列表项),所以我这样做了:

echo + [$(date -d "$(git log --pretty=format:%ai -1)" +"%Y-%m-%d %a %H:%M")] \
    $(git log --pretty=format:"%h %s" --abbrev=12 -1)

结果是,例如:

+ [2015-09-13 Sun 22:44] 2b0ad02e6cec Merge pull request #72 from 3b/bug-1474631
date -d @$(git log -n1 --format="%at") +%Y%m%d%H%M

请注意,这将转换为您的本地时区,以防这对您的用例很重要。

Git 2.7 (Q4 2015)将引入-local作为一条指令。 这意味着,除了:

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

你还会有:

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

-local后缀不能与raw或relative一起使用。参考。

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

提交99264 e9, 提交db7bae2, 提交dc6d782, 提交f3c1ba5, 提交f95cecf, 提交4 b1c5e1, 提交8 f50d26, 提交78 a8441, 提交2df4e29(2015年9月03日)由John Keeping (johnkeeping)。

参见commit add00ba, commit 547ed71 (03 Sep 2015) by Jeff King (peff)。 (由Junio C Hamano—gitster—在commit 7b09c45中合并,2015年10月5日)

特别地,上面的最后一个(commit add00ba)提到:

date: make "local" orthogonal to date format: Most of our "--date" modes are about the format of the date: which items we show and in what order. But "--date=local" is a bit of an oddball. It means "show the date in the normal format, but using the local timezone". The timezone we use is orthogonal to the actual format, and there is no reason we could not have "localized iso8601", etc. This patch adds a "local" boolean field to "struct date_mode", and drops the DATE_LOCAL element from the date_mode_type enum (it's now just DATE_NORMAL plus local=1). The new feature is accessible to users by adding "-local" to any date mode (e.g., "iso-local"), and we retain "local" as an alias for "default-local" for backwards compatibility.