如何更改 Git 日志日期格式

发布于 2024-12-11 18:57:16 字数 323 浏览 0 评论 0 原文

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

我知道日志漂亮格式 %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]"

I am trying to display the last commit within Git, but I need the date in a special format.

I know that the log pretty format %ad respects the --date format, but the only --date format I can find is "short". I want to know the others, and whether I can create a custom one such as:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

恋你朝朝暮暮 2024-12-18 18:57:16

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

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

In addition to --date=(relative|local|default|iso|iso-strict|rfc|short|raw), as others have mentioned, you can also use a custom log date format 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

This outputs something like 2016-01-13 11:32:13.

NOTE: If you take a look at the commit linked to below, I believe you'll need at least Git v2.6.0-rc0 for this to work.

In a full command it would be something like:

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

I haven't been able to find this in documentation anywhere (if someone knows where to find it, please comment) so I originally found the placeholders by trial and error.

In my search for documentation on this I found a commit to Git itself that indicates the format is fed directly to strftime. Looking up strftime (here or here) the placeholders I found match the placeholders listed.

The placeholders include:

%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
醉城メ夜风 2024-12-18 18:57:16

其他的是(来自 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).

据我所知,没有内置方法可以创建自定义格式,但您可以执行一些 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 

Or,以影响此帐户的所有 git 使用情况

git config --global log.date iso

The others are (from 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).

There is no built-in way that I know of to create a custom format, but you can do some shell magic.

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

The first step here gets you a millisecond timestamp. You can change the second line to format that timestamp however you want. This example gives you something similar to --date=local, with a padded day.


And if you want permanent effect without typing this every time, try

git config log.date iso 

Or, for effect on all your git usage with this account

git config --global log.date iso
圈圈圆圆圈圈 2024-12-18 18:57:16

经过很长时间寻找一种方法来获取git logYYYY-MM-DD格式输出日期,以less工作的方式code>,我想出了以下格式:

%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'

After a long time looking for a way to get git log output the date in the format YYYY-MM-DD in a way that would work in less, I came up with the following format:

%ad%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08

along with the switch --date=iso.

This will print the date in ISO format (a long one), and then print 14 times the backspace character (0x08), which, in my terminal, effectively removes everything after the YYYY-MM-DD part. For example:

git log --date=iso --pretty=format:'%ad%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08%aN %s'

This gives something like:

2013-05-24 bruno This is the message of the latest commit.
2013-05-22 bruno This is an older commit.
...

What I did was create an alias named l with some tweaks on the format above. It shows the commit graph to the left, then the commit's hash, followed by the date, the shortnames, the refnames and the subject. The alias is as follows (in ~/.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'
jJeQQOZ5 2024-12-18 18:57:16

我需要同样的东西,并发现以下内容对我有用:

git log -n1 --pretty='format:%cd' --date=format:'%Y-%m-%d %H:%M:%S'

--date=format 格式化日期输出,其中 --pretty 告诉打印什么。

I needed the same thing and found the following working for me:

git log -n1 --pretty='format:%cd' --date=format:'%Y-%m-%d %H:%M:%S'

The --date=format formats the date output where the --pretty tells what to print.

妞丶爷亲个 2024-12-18 18:57:16

您可以使用字段截断选项来避免出现太多 %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< /code>,你可以这样做并完全避免 %<%x08

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

我只是注意到这相对于原始帖子有点循环,但我会保留它,以防其他人使用与我相同的搜索参数到达这里 做过。

You can use the field truncation option to avoid quite so many %x08 characters. For example:

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

is equivalent to:

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>'

And quite a bit easier on the eyes.

Better still, for this particular example, using %cd will honor the --date=<format>, so if you want YYYY-MM-DD, you can do this and avoid %< and %x08 entirely:

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

I just noticed this was a bit circular with respect to the original post but I'll leave it in case others arrived here with the same search parameters I did.

笙痞 2024-12-18 18:57:16

2014 :注意“date=iso”格式:它不完全 ISO 8601
请参阅 466fb67” /bbolli" rel="nofollow noreferrer">击败博利(bbolli),对于 Git 2.2.0(2014 年 11 月)来说

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

由于微小的差异,Git 的“ISO”日期格式并不真正符合 ISO 8601 标准,并且它无法被仅支持 ISO 8601 的解析器(例如 XML 工具链的解析器)解析。

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

  • 用空格代替 T 日期/时间分隔符
  • 时间和时区之间的空格
  • 时区的小时和分钟之间没有冒号

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

请参阅 此帖子进行讨论。


对于 Git 2.45(2024 年第 2 季度),批次 10,日期的输出格式为“< code>iso-strict”已被调整以显示祖鲁时区时间,后缀为“Z”,而不是“+00:00”。

请参阅 提交 69e2bee(2024 年 3 月 13 日),作者:击败 Bolli (bbolli)
(由 Junio C Hamano -- gitster -- 合并于 提交 1f49f75,2024 年 3 月 21 日)

日期:制作“iso -strict" 符合 UTC 时区

报告人:Michael Osipov
签署人:Beat Bolli

ISO 8601-1:2020-12 规定零时区偏移必须用“Z”后缀表示,而不是数字“+00:00” .
将相应的特殊情况添加到 show_date() 和一个新测试。

更改脚本可能依赖的既定输出格式始终是有问题的,但在这里我们选择更严格地遵守已发布的标准。

2016-06-15T14:13:20Z

2014 : Be aware of the "date=iso" format: it isn't exactly ISO 8601.
See commit "466fb67" from Beat Bolli (bbolli), for Git 2.2.0 (November 2014)

pretty: provide a strict ISO 8601 date format

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.

See this thread for discussion.


With Git 2.45 (Q2 2024), batch 10, the output format for dates "iso-strict" has been tweaked to show a time in the Zulu timezone with "Z" suffix, instead of "+00:00".

See commit 69e2bee (13 Mar 2024) by Beat Bolli (bbolli).
(Merged by Junio C Hamano -- gitster -- in commit 1f49f75, 21 Mar 2024)

date: make "iso-strict" conforming for the UTC timezone

Reported-by: Michael Osipov
Signed-off-by: Beat Bolli

ISO 8601-1:2020-12 specifies that a zero timezone offset must be denoted with a "Z" suffix instead of the numeric "+00:00".
Add the correponding special case to show_date() and a new test.

Changing an established output format which might be depended on by scripts is always problematic, but here we choose to adhere more closely to the published standard.

2016-06-15T14:13:20Z
恋你朝朝暮暮 2024-12-18 18:57:16
date -d @$(git log -n1 --format="%at") +%Y%m%d%H%M

请注意,如果这对您的用例很重要,这将转换为您当地的时区。

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

Note that this will convert to your local timezone, in case that matters for your use case.

何以畏孤独 2024-12-18 18:57:16

我需要特殊格式的日期。

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

当输出发送到寻呼机或终端时,可以使用“--date=auto”来使用这种新格式,否则使用默认格式。

请参阅提交110a6a1提交 b841d4f(2019 年 1 月 29 日),以及 提交 038a878, 提交 2fd7c22(2019 年 1 月 21 日),作者:斯蒂芬·P·史密斯(``)
请参阅 提交 acdd377(2019 年 1 月 18 日),作者:莱纳斯·托瓦兹(托瓦尔兹
(由 Junio C Hamano -- gitster -- 合并于 提交ecbe1be,2019 年 2 月 7 日)

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

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

通过不显示冗余信息,读者可以专注于不同的信息
该补丁根据执行命令时运行 git 命令的计算机上的日期推断出的信息来报告相对日期。

虽然该格式通过删除推断信息对人类更有用,但没有任何东西使它真正适合人类。
如果尚未实现“relative”日期格式,则使用“relative”是合适的。

添加人类日期格式测试。

使用人类时,根据参考日期和本地计算机日期之间的时间差,多个字段会被抑制。

  • 如果差异小于一年,年份字段将被隐藏。
  • 如果时间少于一天;月份和年份是
    压制。
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

将建议的“auto”模式替换为“auto:

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

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

git config --add log.date auto: human

并且您的“git log”命令将显示人类可读的格式,除非
你正在编写脚本。


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

请参阅提交47b27c9提交 29f4332(2019 年 9 月 12 日),作者:斯蒂芬·P·史密斯(``)
(由 Junio C Hamano -- gitster -- 合并于 提交36d2fca,2019 年 10 月 7 日)

停止传递“现在”日期代码

提交b841d4f(添加 human 格式进行测试-工具,2019-01-28,Git v2.21.0-rc0)添加了 get_time() 函数,允许环境中的 $GIT_TEST_DATE_NOW 覆盖当前时间。
因此我们不再需要在 cmd__date() 中解释该变量。

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


使用 Git 2.32(2021 年第 2 季度),“git日志--format=..."(man) 占位符学习 %ah/%ch 占位符来请求 --date =人类输出。

请参阅 提交 b722d45(2021 年 4 月 25 日),作者:胡哲宁(替代方案
(由 Junio C Hamano -- gitster -- 合并于 提交f16a466,2021 年 5 月 7 日)

pretty:提供人类日期格式

签字人:胡哲宁

添加占位符 %ah%ch 以格式化作者日期和提交者日期,就像 --date= human 所做的那样,这提供了更加人性化的数据输出。

pretty-formats 现在包含在其 手册页

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

漂亮-格式现在包含在其手册页

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

I need the date in a special format.

With Git 2.21 (Q1 2019), a new date format "--date=human" that morphs its output depending on how far the time is from the current time has been introduced.

"--date=auto" can be used to use this new format when the output is going to the pager or to the terminal and otherwise the default format.

See commit 110a6a1, commit b841d4f (29 Jan 2019), and commit 038a878, commit 2fd7c22 (21 Jan 2019) by Stephen P. Smith (``).
See commit acdd377 (18 Jan 2019) by Linus Torvalds (torvalds).
(Merged by Junio C Hamano -- gitster -- in commit ecbe1be, 07 Feb 2019)

Add 'human' date format documentation

Display date and time information in a format similar to how people write dates in other contexts.
If the year isn't specified then, the reader infers the date is given is in the current year.

By not displaying the redundant information, the reader concentrates on the information that is different.
The patch reports relative dates based on information inferred from the date on the machine running the git command at the time the command is executed.

While the format is more useful to humans by dropping inferred information, there is nothing that makes it actually human.
If the 'relative' date format wasn't already implemented, then using 'relative' would have been appropriate.

Add human date format tests.

When using human several fields are suppressed depending on the time difference between the reference date and the local computer date.

  • In cases where the difference is less than a year, the year field is suppressed.
  • If the time is less than a day; the month and year is
    suppressed.
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

Replace the proposed 'auto' mode with 'auto:'

In addition to adding the 'human' format, the patch added the auto keyword which could be used in the config file as an alternate way to specify the human format. Removing 'auto' cleans up the 'human' format interface.

Added the ability to specify mode 'foo' if the pager is being used by using auto:foo syntax.
Therefore, 'auto:human' date mode defaults to human if we're using the pager.
So you can do:

git config --add log.date auto:human

and your "git log" commands will show the human-legible format unless
you're scripting things.


Git 2.24 (Q4 2019) simplified the code.

See commit 47b27c9, commit 29f4332 (12 Sep 2019) by Stephen P. Smith (``).
(Merged by Junio C Hamano -- gitster -- in commit 36d2fca, 07 Oct 2019)

Quit passing 'now' to date code

Commit b841d4f (Add human format to test-tool, 2019-01-28, Git v2.21.0-rc0) added a get_time() function which allows $GIT_TEST_DATE_NOW in the environment to override the current time.
So we no longer need to interpret that variable in cmd__date().

Therefore, we can stop passing the "now" parameter down through the
date functions, since nobody uses them.
Note that we do need to make sure all of the previous callers that took a "now" parameter are correctly using get_time().


With Git 2.32 (Q2 2021), "git log --format=..."(man) placeholders learned %ah/%ch placeholders to request the --date=human output.

See commit b722d45 (25 Apr 2021) by ZheNing Hu (adlternative).
(Merged by Junio C Hamano -- gitster -- in commit f16a466, 07 May 2021)

pretty: provide human date format

Signed-off-by: ZheNing Hu

Add the placeholders %ah and %ch to format author date and committer date, like --date=human does, which provides more humanity date output.

pretty-formats now includes in its man page:

'%ah':: author date, human style (like the --date=human option of
git rev-list)

pretty-formats now includes in its man page:

'%ch':: committer date, human style (like the --date=human option of
git rev-list)

蓝咒 2024-12-18 18:57:16

Git 2.7(2015 年第 4 季度)将引入 -local 作为指令。
这意味着,除了:

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

您还将拥有:

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

-local 后缀不能与 rawrelative 一起使用。 参考

您现在可以使用请求任何日期格式当地时区
请参阅

请参阅提交add00ba提交 547ed71(2015 年 9 月 3 日),作者:杰夫·金(peff
(由 Junio C Hamano -- gitster -- 合并于 提交7b09c45,2015 年 10 月 5 日)

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

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

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

此补丁将“local”布尔字段添加到“struct date_mode”,并从 date_mode_type 中删除 DATE_LOCAL 元素 枚举(现在只是 DATE_NORMAL 加上 local=1)。
用户可以通过将“-local”添加到任何日期模式(例如“iso-local”)来访问新功能,并且我们保留“local< /code>”作为“default-local”的别名以实现向后兼容性。


Git 2.7 (Q4 2015) will introduce -local as an instruction.
It means that, in addition to:

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

you will also have:

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

The -local suffix cannot be used with raw or relative. Reference.

You now can ask for any date format using the local timezone.
See

See commit add00ba, commit 547ed71 (03 Sep 2015) by Jeff King (peff).
(Merged by Junio C Hamano -- gitster -- in commit 7b09c45, 05 Oct 2015)

In particular, the last from above (commit add00ba) mentions:

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.

你げ笑在眉眼 2024-12-18 18:57:16

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

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

--format="%ai"

The format option %ai was what I wanted:

%ai: author date, ISO 8601-like format

--format="%ai"
绿萝 2024-12-18 18:57:16

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

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

Use Bash and the date command to convert from an ISO-like format to the one you want. I wanted an org-mode date format (and list item), so I did this:

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

And the result is for example:

+ [2015-09-13 Sun 22:44] 2b0ad02e6cec Merge pull request #72 from 3b/bug-1474631
苏辞 2024-12-18 18:57:16
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 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]"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文