如何配置'git log'以显示'提交日期'

207

如何配置git log以显示提交日期而非作者日期


27
每个提交都有两个相关联的日期 - 作者日期和提交日期(使用“git show --pretty=fuller HEAD”查看示例)。对于本地开发,这些通常是相同的,但对于通过电子邮件或其他机制添加的补丁,它们可能会不同。其中,作者日期是生成补丁的日期,而提交日期是其实际应用于存储库的日期。 - twalberg
4个回答

228
有几种选项可以pretty print日期。可能最简单的方法是使用其中一个预先制作的--pretty格式之一,例如git log --pretty=fuller - 这将显示两个日期。如果您只想看到一个日期,但要使其成为提交日期,则可以使用git log --format=<some stuff>。定义格式的allowable codes中的所有内容都记录在git help log中。提交日期是%cd%cD%cr%ct%ci之一,具体取决于您喜欢的格式。

如果这是您经常要做的事情,请将其放入别名中或编写辅助脚本以节省输入时间。


%cd 中我们能获取年份和月份吗? - alper

117
您可以使用--pretty=format并使用%cr来获取相对于提交时间的日期。
例如:
$ git log --graph --pretty=format:'%C(auto)%h%d (%cr) %cn <%ce> %s'

您可以在 git 中定义别名来使其更易使用。我在我的.gitconfig文件中定义了以下内容:

[alias]
# see `git help log` for detailed help.
#   %h: abbreviated commit hash
#   %d: ref names, like the --decorate option of git-log(1)
#   %cn: commiter name
#   %ce: committer email
#   %cr: committer date, relative
#   %ci: committer date, ISO 8601-like format
#   %an: author name
#   %ae: author email
#   %ar: author date, relative
#   %ai: author date, ISO 8601-like format
#   %s: subject
# my awesome git log replacement
lol  = log --graph --pretty=format:\"%C(auto)%h%d%Creset %C(cyan)(%cr)%Creset %C(green)%cn <%ce>%Creset %s\"
# same as above, but ISO date
lold = log --graph --pretty=format:\"%C(auto)%h%d%Creset %C(cyan)(%ci)%Creset %C(green)%cn <%ce>%Creset %s\"
# using build-in standards
lol2 = log --oneline --graph --decorate
# shows branches and their last commits
lol3 = log --all --graph --decorate --oneline --simplify-by-decoration

在Linux或类似系统中,您可以使用单引号'代替双引号"

[alias]
lol = log --graph --pretty=format:'%C(auto)%h%d%Creset %C(cyan)(%cr)%Creset %C(green)%cn <%ce>%Creset %s'

使用此命令,只需运行git lol或其他变体即可查看漂亮的输出。

以下是运行git lol --simplify-by-decoration的输出:

git lol 输出

  • 看起来很好。:)
  • lollog更容易键入,听起来也更好。
    • 如果需要,还可以访问常规的git log
  • 通过不同的颜色,您的眼睛可以快速扫描内容。
  • 对于有许多贡献者的大型项目/组织,名称和电子邮件非常有用。
  • 对哈希/引用使用默认着色,因为它已经非常好看。

以下是在ISO格式中使用日期的git lold的输出。有助于查看提交的确切日期/时间,并轻松查看贡献者的时区。

enter image description here

编辑2020-06:添加了截屏。使用%C(auto)(自动/默认着色)替换%h(提交哈希)和%d(引用名称)。除电子邮件外,还添加了%cn(提交者名称)。


1
我遇到了解析错误:git log --graph --pretty=format:\"%C(yellow)%h%Creset%C(cyan)%C(bold)%d%Creset %C(cyan)(%cr)%Creset %C(green)%ce%Creset %s\"
bash: 附近有意外的标记 `('
- Frak
2
@frakman1 — 你需要取消转义上面那行的双引号才能在终端中运行。 - stites
3
修正后的行:git log --graph --pretty=format:"%C(yellow)%h%Creset%C(cyan)%C(bold)%d%Creset %C(cyan)(%cr)%Creset %C(green)%ce%Creset %s" - RedSands

20

我更喜欢这种格式,不包括作者姓名,包含提交的实际日期。

git log --graph --pretty=format:"%C(yellow)%h%x09%Creset%C(cyan)%C(bold)%ad%Creset  %C(green)%Creset %s" --date=short

“实际日期”是指作者创建该提交内容的第一个版本的日期。如果此后进行了变基或重新提交,则可以使用“%c”格式来查找所见到的最终提交日期。 “--short-date”选项与“%ai”和“%ci”的“iso”日期格式输出是同义词。 - user608724

12

或许对某些人有用。我正在寻找带有日期和时间戳记以及作者姓名的内容。

在此输入图片描述

git log --graph --pretty=format:"%C(yellow)%h%x09%Creset%C(cyan)%C(bold)%ad%Creset %C(yellow)%cn%Creset  %C(green)%Creset %s" --date=default

由于问题涉及到显示提交日期,您需要将“%ad%”更改为“%cd%”以查看提交日期。“ad”代表作者日期。 - rupinderjeet

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