如何更改Git日志日期格式

ar3*_*ar3 133 git command-line

我试图在Git中显示最后一次提交,但我需要一个特殊格式的日期.

我知道日志漂亮格式%ad尊重--date格式,但--date我能找到的唯一格式是"短".我想知道其他人,以及我是否可以创建一个自定义的,例如:

git -n 1 --date=**YYMMDDHHmm** --pretty=format:"Last committed item in this release was by %%an, %%aD, message: %%s(%%h)[%%d]"
Run Code Online (Sandbox Code Playgroud)

Ben*_*red 158

除此之外--date=(relative|local|default|iso|iso-strict|rfc|short|raw),正如其他人所提到的,您还可以使用自定义日志日期格式

--date=format:'%Y-%m-%d %H:%M:%S'
Run Code Online (Sandbox Code Playgroud)

这输出类似的东西2016-01-13 11:32:13.

注意:如果您查看下面链接的提交,我相信您至少Git v2.6.0-rc0需要这个才能工作.

我无法在任何地方的文档中找到这个(如果有人知道在哪里找到它,请评论)所以我最初通过反复试验找到了占位符.

在我搜索关于此的文档时,我发现了一个Git本身的提交,表明格式是直接提供给strftime.查找strftime(此处此处)我找到的占位符与列出的占位符相匹配.

占位符包括:

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'" 
Run Code Online (Sandbox Code Playgroud)

  • @Shiva:我最近遇到了一个案例,但它不起作用,可能与你遇到的情况相同.我正在使用配对机器和旧版本的Git.当我更新Git时,自定义日期格式开始工作.如果你看看上面链接的提交,我相信你至少需要v2.6.0-rc0.给出的例子正是我使用的.在一个完整的命令中,它将类似于`git config --global alias.lg"log --graph --decorate -30 --all --date-order --date = format:'%Y-%m-% d%H:%M:%S' - 优质=格式:'%C(青色)%h%Creset%C(黑色粗体)%ad%Creset%C(自动)%d%s'"` (4认同)
  • 如何将 `--date=format:'%Y-%m-%d %H:%M:%S'` 放入 `.gitconfig` 中? (2认同)

dme*_*sky 156

其他人是(来自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).
Run Code Online (Sandbox Code Playgroud)

我知道没有内置的方法来创建自定义格式,但你可以做一些shell魔术.

timestamp=`git log -n1 --format="%at"`
my_date=`perl -e "print scalar localtime ($timestamp)"`
git log -n1 --pretty=format:"Blah-blah $my_date"
Run Code Online (Sandbox Code Playgroud)

这里的第一步为您提供毫秒时间戳.您可以更改第二行以根据需要格式化该时间戳.这个例子给你类似的东西--date=local,有一个填充的日子.


如果你想要永久性的效果,而不是每次都输入,请尝试

git config log.date iso 
Run Code Online (Sandbox Code Playgroud)

或者,使用此帐户对您的所有git使用产生影响

git config --global log.date iso
Run Code Online (Sandbox Code Playgroud)

  • 如果你想要永久性的效果,而不是每次都输入这个,尝试类似`git config log.date iso`或者,对于你使用这个帐户的所有git使用效果`git config --global log.date iso` (13认同)
  • **您可以使用自己的格式.**见[Ben Allred的回答](http://stackoverflow.com/a/34778736/1858327).`--format:<args>`将`<args>`传递给strftime. (9认同)

Bru*_*eis 36

经过漫长的时间寻找一种方式来获得git log输出格式日期YYYY-MM-DD的方式,将工作中less,我想出了以下格式:

%ad%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08
Run Code Online (Sandbox Code Playgroud)

随着开关--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'
Run Code Online (Sandbox Code Playgroud)

这给出了类似的东西:

2013-05-24 bruno This is the message of the latest commit.
2013-05-22 bruno This is an older commit.
...
Run Code Online (Sandbox Code Playgroud)

我所做的是创建一个别名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'
Run Code Online (Sandbox Code Playgroud)

  • 那么`--date = short只显示日期而不是时间,以YYYY-MM-DD格式显示 (7认同)
  • 这是荒谬和令人敬畏的同时.我使用`%C(粗体青色)%ai%x08%x08%x08%x08%x08%x08%x08%x08%x08%C(重置)%C(粗体绿色)(%ar%x08%x08% x08%x08)%C(重置)`获取格式`2015-01-19 11:27(11天)`.+1 (5认同)
  • 在zsh和bash中,这将显示终端上的输出,但如果将其输出到任何内容,则"退格"数据仍然存在.这意味着像`| 排序| uniq`不起作用. (3认同)
  • 这对我很有用.使用6%x08可以完全删除我的尾随时区. (2认同)
  • 现在这个答案http://stackoverflow.com/a/34778736/907576更合适(自Git v2.6.0-rc0) (2认同)

小智 24

您可以使用字段截断选项来避免这么多%x08字符.例如:

git log --pretty='format:%h %s%n\t%<(12,trunc)%ci%x08%x08, %an <%ae>'
Run Code Online (Sandbox Code Playgroud)

相当于:

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>'
Run Code Online (Sandbox Code Playgroud)

在眼睛上更容易一些.

更好的是,对于这个特殊的例子,使用%cd将尊重--date=<format>,所以如果你想YYYY-MM-DD,你可以做到这一点,%<%x08完全避免:

git log --date=short --pretty='format:%h %s%n\t%cd, %an <%ae>'
Run Code Online (Sandbox Code Playgroud)

我刚刚注意到这对于原始帖子来说有点循环,但我会留下它,以防其他人带着与我相同的搜索参数到达这里.


小智 21

我需要同样的东西,发现以下为我工作:

git log -n 1 --pretty='format:%cd' --date=format:'%Y-%m-%d %H:%M:%S'
Run Code Online (Sandbox Code Playgroud)

--date=format日期输出的格式--pretty告诉要打印的内容.

  • 这对我不起作用.我得到"未知的日期格式" (2认同)

Von*_*onC 15

请注意" date=iso"格式:它不完全是 ISO 8601.
请参阅Beat Bolli()的提交" 466fb67 " ,获取Git 2.2.0(2014年11月)bbolli

漂亮:提供严格的ISO 8601日期格式

由于差异很小,Git的"ISO"日期格式并不真正符合ISO 8601标准,并且它不能由仅ISO 8601的解析器解析,例如XML工具链的解析器.

" --date=iso" 的输出在以下方面偏离ISO 8601:

  • 空格而不是T日期/时间分隔符
  • 时区和时区之间的空间
  • 时区的小时和分钟之间没有冒号

添加严格的ISO 8601日期格式以显示提交者和作者日期.
使用' %aI'和' %cI'格式说明符并添加' --date=iso-strict'或' --date=iso8601-strict'日期格式名称.

请参阅此主题以供讨论.


Che*_*tah 11

date -d @$(git log -n1 --format="%at") +%Y%m%d%H%M
Run Code Online (Sandbox Code Playgroud)

请注意,这将转换为您当地的时区,以防您的用例.


Von*_*onC 6

我需要特殊格式的日期。

在 Git 2.21(2019 年第一季度)中,引入了一种新的日期格式“ --date=human”,它根据时间与当前时间的距离来改变其输出

" --date=auto" 可用于在输出到寻呼机或终端时使用这种新格式,否则使用默认格式。

请参阅Stephen P. Smith (``) 的commit 110a6a1commit b841d4f(2019 年 1 月 29 日)和commit 038a878commit 2fd7c22(2019 年 1 月 21 日
请参阅Linus Torvalds ( )提交的 acdd377(2019 年 1 月 18 日(由Junio C Hamano合并-- --ecbe1be 提交中,2019 年 2 月 7 日)torvalds
gitster

添加“人类”日期格式文档

以类似于人们在其他上下文中书写日期的格式显示日期和时间信息。
如果未指定年份,则读者推断给出的日期在当前年份

通过不显示冗余信息,读者可以专注于不同的信息
补丁根据从执行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
Run Code Online (Sandbox Code Playgroud)

更换提出了“ auto”与模式“ auto:

除了添加“ human”格式之外,补丁还添加了auto可以在配置文件中使用的关键字作为指定人类格式的替代方法。删除 'auto' 会清理 ' human' 格式界面。

添加了在foo使用auto:foo语法使用寻呼机时指定模式“ ”的功能。
因此,如果我们正在使用寻呼机,则“ auto:human”日期模式默认为human
所以你可以这样做:

git config --add log.date auto:human
Run Code Online (Sandbox Code Playgroud)

git log除非您正在编写脚本,否则您的“ ”命令将显示人类易读的格式。


Git 2.24(2019 年第四季度)简化了代码。

提交 47b27c9提交 29f4332(2019 年 9 月 12 日)由Stephen P. Smith (``)提交
(由Junio C gitsterHamano合并-- --提交 36d2fca,2019 年 10 月 7 日)

退出传递“现在”到日期代码

Commit b841d4f (Add humanformat to test-tool, 2019-01-28, Git v2.21.0-rc0) 添加了get_time()允许$GIT_TEST_DATE_NOW在环境中覆盖当前时间的功能。
所以我们不再需要在cmd__date().

因此,我们可以停止通过now日期函数向下传递“ ”参数,因为没有人使用它们。
请注意,我们确实需要确保所有先前使用 " now" 参数的调用方都正确使用get_time().


在 Git 2.32(2021 年第二季度)中,“ git log --format=...man占位符学习%ah/%ch占位符来请求--date=human输出。

请参阅ZheNing Hu ( )提交的 b722d45 (2021 年 4 月 25 日)(由Junio C Hamano合并-- --提交 f16a466 中,2021 年 5 月 7 日)adlternative
gitster

pretty: 提供人工日期格式

签字人:胡哲宁

添加占位符%ah%ch格式化作者日期和提交者日期,就像--date=humando 一样,这提供了更多人性化的日期输出。

pretty-formats现在包括在其手册页中

' %ah':: 作者日期,人性化风格(如 的--date=human选项 git rev-list

pretty-formats现在包括在其手册页中

' %ch':: 提交者日期,人性化风格(如 的--date=human选项 git rev-list


Tho*_*ner 5

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

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

--format="%ai"
Run Code Online (Sandbox Code Playgroud)


Von*_*onC 5

Git 2.7(2015年第四季度)将-local作为指导进行介绍。
这意味着,除了:

--date=(relative|local|default|iso|iso-strict|rfc|short|raw)
Run Code Online (Sandbox Code Playgroud)

您还将拥有:

--date=(default-local|iso-local|iso-strict-local|rfc-local|short-local)
Run Code Online (Sandbox Code Playgroud)

-local后缀不能使用rawrelative参考

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

参见Jeff King(提交的commit add00ba547ed71(2015年9月3日(通过合并JUNIOÇ滨野- -提交7b09c45,2015年10月5日)peff
gitster

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

date:使“ local”与日期格式正交:

我们大多数的“ --date”模式都与日期格式有关:我们显示哪些项目以及以什么顺序显示。
但是“ --date=local”有点奇怪。这意味着“以常规格式显示日期,但使用本地时区”。
我们使用的时区与实际格式正交,因此没有理由不能拥有“本地化iso8601”等。

该补丁local向“ struct date_mode” 添加了一个“ ”布尔字段,并DATE_LOCALdate_mode_type枚举中删除了该元素(现在只是DATE_NORMALplus local=1)。
通过-local在任何日期模式(例如“ iso-local”)中添加“ ” ,用户可以使用该新功能,并且为了向后兼容,我们保留“ local”作为“ ”的别名default-local

  • 我必须注意,`format-local` 也有效!所以把喜欢的格式和本地的力量融合在一起是非常完美的。 (2认同)