如何更改 Git 日志日期格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7853332/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change Git log date formats
提问by ar3
I am trying to display the last commit within Git, but I need the date in a special format.
我试图在 Git 中显示最后一次提交,但我需要特殊格式的日期。
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:
我知道日志漂亮格式%ad
尊重--date
格式,但--date
我能找到的唯一格式是“短”。我想知道其他人,以及我是否可以创建一个自定义的,例如:
git -n 1 --date=**YYMMDDHHmm** --pretty=format:"Last committed item in this release was by %%an, %%aD, message: %%s(%%h)[%%d]"
回答by Ben Allred
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=(relative|local|default|iso|iso-strict|rfc|short|raw)
,正如其他人所提到的,您还可以使用自定义日志日期格式
--date=format:'%Y-%m-%d %H:%M:%S'
This outputs something like 2016-01-13 11:32:13
.
这会输出类似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-rc0for this to work.
注意:如果您查看链接到下面的提交,我相信您至少Git v2.6.0-rc0需要它才能工作。
In a full command it would be something like:
在完整的命令中,它将类似于:
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'"
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 itselfthat indicates the format is fed directly to strftime
. Looking up strftime
(hereor here) the placeholders I found match the placeholders listed.
在我搜索这方面的文档时,我发现了对 Git 本身的提交,表明格式直接提供给strftime
. 查找strftime
(此处或此处)我找到的占位符与列出的占位符匹配。
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
In a full command it would be something like
在完整的命令中,它类似于
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'"
回答by dmedvinsky
The others are (from git help log
):
其他人是(来自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.
我不知道创建自定义格式的内置方法,但您可以使用一些 shell 魔法。
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.
这里的第一步为您提供毫秒时间戳。您可以根据需要更改第二行以格式化该时间戳。此示例为您提供了类似于--date=local
, 带有填充日期的内容。
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 使用情况
git config --global log.date iso
回答by Bruno Reis
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:
经过漫长的时间寻找一种方式来获得git log
输出格式日期YYYY-MM-DD
的方式,将工作中less
,我想出了以下格式:
%ad%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08
along with the switch --date=iso
.
随着开关--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:
这将以 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'
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):
我所做的是创建一个别名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'
回答by dvhart
You can use the field truncation option to avoid quite so many %x08
characters. For example:
您可以使用字段截断选项来避免太多%x08
字符。例如:
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:
更好的是,对于这个特定的例子, using%cd
将尊重--date=<format>
,所以如果你想要YYYY-MM-DD
,你可以这样做%<
并%x08
完全避免:
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.
我只是注意到这相对于原始帖子有点循环,但我会留下它,以防其他人使用与我相同的搜索参数到达这里。
回答by lac_dev
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.
在--date=format
其中格式化日期输出--pretty
告诉打印什么。
回答by VonC
Be aware of the "date=iso
" format: it isn't exactlyISO 8601.
See commit "466fb67" from Beat Bolli (bbolli
), for Git 2.2.0 (November 2014)
请注意“ date=iso
”格式:它不完全是ISO 8601。
请参阅来自Beat Bolli ( ) 的提交“ 466fb67” ,用于 Git 2.2.0(2014 年 11 月)bbolli
pretty: provide a strict ISO 8601 date format
漂亮:提供严格的 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 的“ISO”日期格式并不真正符合 ISO 8601 标准,并且它不能被 ISO 8601-only 解析器解析,例如 XML 工具链的解析器。
"
--date=iso
"的输出在以下方面偏离 ISO 8601:
- 一个空格而不是
T
日期/时间分隔符- 时间和时区之间的空间
- 时区的小时和分钟之间没有冒号
添加严格的 ISO 8601 日期格式以显示提交者和作者日期。
使用“%aI
”和“%cI
”格式说明符并添加“--date=iso-strict
”或“--date=iso8601-strict
”日期格式名称。
See this threadfor discussion.
请参阅此线程进行讨论。
回答by Cheetah
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.
请注意,如果这对您的用例很重要,这将转换为您的本地时区。
回答by ThorSummoner
The format option %ai
was what I wanted:
格式选项%ai
是我想要的:
%ai
: author date, ISO 8601-like format
%ai
: 作者日期,类似 ISO 8601 的格式
--format="%ai"
回答by VonC
Git 2.7 (Q4 2015) will introduce -local
as an instruction.
It means that, in addition to:
Git 2.7(2015 年第四季度)将-local
作为说明引入。
这意味着,除了:
--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.
该-local
后缀不能使用raw
或relative
。参考。
You now can ask for any date format using the local timezone. See
您现在可以使用本地时区请求任何日期格式。看
- commit 99264e9,
- commit db7bae2,
- commit dc6d782,
- commit f3c1ba5,
- commit f95cecf,
- commit 4b1c5e1,
- commit 8f50d26,
- commit 78a8441,
- commit 2df4e29(03 Sep 2015) by John Keeping (
johnkeeping
).
- 提交 99264e9,
- 提交 db7bae2,
- 提交 dc6d782,
- 提交 f3c1ba5,
- 提交 f95cecf,
- 提交 4b1c5e1,
- 提交 8f50d26,
- 提交 78a8441,
- 由John Keeping (
johnkeeping
)提交 2df4e29(2015 年 9 月 3 日)。
See commit add00ba, commit 547ed71(03 Sep 2015) by Jeff King (peff
).
(Merged by Junio C Hamano -- gitster
--in commit 7b09c45, 05 Oct 2015)
请参阅提交 add00ba,提交 547ed71(2015 年 9 月 3 日)由Jeff King ( peff
)。
(由Junio C gitster
Hamano合并-- --在commit 7b09c45,2015 年 10 月 5 日)
In particular, the last from above (commit add00ba) mentions:
特别是,上面的最后一个 (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 theDATE_LOCAL
element from thedate_mode_type
enum (it's now justDATE_NORMAL
pluslocal=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.
date
: 使 "local
" 与日期格式正交:我们的大多数“
--date
”模式都是关于日期的格式:我们显示哪些项目以及以什么顺序显示。
但是“--date=local
”有点古怪。这意味着“以正常格式显示日期,但使用本地时区”。
我们使用的时区与实际格式正交,我们没有理由不能“本地化 iso8601”等。这个补丁
local
向“struct date_mode
”添加了一个“ ”布尔字段,并DATE_LOCAL
从date_mode_type
枚举中删除元素(现在只是DATE_NORMAL
pluslocal=1
)。
用户可以通过将“-local
”添加到任何日期模式(例如“iso-local
”)来访问新功能,为了向后兼容,我们保留“local
”作为“ ”的别名default-local
。
回答by VonC
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.
在 Git 2.21(2019 年第一季度)中,引入了一种新的日期格式“ --date=human
”,它根据时间与当前时间的距离来改变其输出。
"--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.
" --date=auto
" 可用于在输出到寻呼机或终端时使用这种新格式,否则使用默认格式。
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)
请参阅Stephen P. Smith (``) 的commit 110a6a1、commit b841d4f(29 Jan 2019) 和commit 038a878、commit 2fd7c22(21 Jan 2019 )。
请参阅Linus Torvalds ( ) 的commit acdd377(2019 年 1 月 18 日)。(由Junio C Hamano合并-- --在ecbe1be 提交中,2019 年 2 月 7 日)torvalds
gitster
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 thegit
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.
添加“人类”日期格式文档
以类似于人们在其他上下文中书写日期的格式显示日期和时间信息。
如果未指定年份,则读者推断给出的日期在当前年份。通过不显示冗余信息,读者可以专注于不同的信息。
补丁根据从执行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
## Replace the proposed '
auto
' mode with 'auto:
'In addition to adding the '
human
' format, the patch added theauto
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 usingauto:foo
syntax.
Therefore, 'auto:human
' date mode defaults tohuman
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.
## 将建议的 '
auto
' 模式替换为'auto:
'除了添加“
human
”格式之外,补丁还添加了auto
可以在配置文件中使用的关键字作为指定人类格式的替代方法。删除 'auto' 会清理 'human
' 格式界面。添加了在
foo
使用auto:foo
语法使用寻呼机时指定模式“ ”的功能。
因此,如果我们正在使用寻呼机,则“auto:human
”日期模式默认为human
。
所以你可以这样做:git config --add log.date auto:human
git log
除非您正在编写脚本,否则您的“ ”命令将显示人类易读的格式。
Git 2.24 (Q4 2019) simplified the code.
Git 2.24(2019 年第四季度)简化了代码。
See commit 47b27c9, commit 29f4332(12 Sep 2019) by Stephen P. Smith (``).
(Merged by Junio C Hamano -- gitster
--in commit 36d2fca, 07 Oct 2019)
见提交 47b27c9,提交 29f4332(2019 年 9 月 12 日)由Stephen P. Smith (``)提交。
(由Junio C gitster
Hamano合并-- --在commit 36d2fca,2019 年 10 月 7 日)
Quit passing 'now' to date code
Commit b841d4f(Add
human
format to test-tool, 2019-01-28, Git v2.21.0-rc0) added aget_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 incmd__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 usingget_time()
.
退出传递“现在”到日期代码
Commit b841d4f(Add
human
format to test-tool, 2019-01-28, Git v2.21.0-rc0) 添加了get_time()
允许$GIT_TEST_DATE_NOW
在环境中覆盖当前时间的功能。
所以我们不再需要在cmd__date()
.因此,我们可以停止通过
now
日期函数向下传递“ ”参数,因为没有人使用它们。
请注意,我们确实需要确保所有先前使用 "now
" 参数的调用方都正确使用get_time()
.