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

当前回答

使用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

其他回答

格式选项%ai是我想要的:

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

--format="%ai"

使用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

您可以使用字段截断选项来避免使用太多%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>'

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

注意“date=iso”格式:它并不是iso 8601。 参见commit "466fb67" from Beat Bolli (bbolli),用于Git 2.2.0(2014年11月)

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

Git's "ISO" date format does not really conform to the ISO 8601 standard due to small differences, and it cannot be parsed by ISO 8601-only parsers, e.g. those of XML toolchains. The output from "--date=iso" deviates from ISO 8601 in these ways: a space instead of the T date/time delimiter a space between time and time zone no colon between hours and minutes of the time zone Add a strict ISO 8601 date format for displaying committer and author dates. Use the '%aI' and '%cI' format specifiers and add '--date=iso-strict' or '--date=iso8601-strict' date format names.

请看这个帖子进行讨论。

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]"