我试图在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]"
我试图在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]"
当前回答
其他的是(来自git帮助日志):
--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
或者,影响你所有的git使用此帐户
git config --global log.date iso
其他回答
我也需要同样的东西,并发现以下方法对我有用:
git log -n1 --pretty='format:%cd' --date=format:'%Y-%m-%d %H:%M:%S'
——date=format格式化日期输出,其中——pretty告诉打印什么。
使用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
我需要特殊格式的日期。
在Git 2.21(2019年第一季度)中,引入了一种新的日期格式“——date=human”,它可以根据时间与当前时间的距离改变输出。
"——date=auto"可以在输出到寻呼机或终端时使用这种新格式,否则使用默认格式。
参见Stephen P. Smith(' ')的commit 110a6a1, commit b841d4f(2019年1月29日)和commit 038a878, commit 2fd7c22(2019年1月21日)。 参见Linus Torvalds (Torvalds)的commit acdd377(2019年1月18日)。 (由Junio C Hamano—gitster—在commit ecbe1be中合并,2019年2月7日)
添加“人工”日期格式文档
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
将建议的'auto'模式替换为'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(2019年Q4)简化了代码。
参见Stephen P. Smith(' ')的commit 47b27c9, commit 29f4332(2019年9月12日)。 (由Junio C Hamano—gitster—在commit 36d2fca中合并,2019年10月7日)
停止传递'now'到日期代码
提交b841d4f(将人工格式添加到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 (Q2 2021)中," Git log——format=…"(man)占位符学习了%ah/%ch占位符来请求——date=人工输出。
参见胡哲宁(替代)提交b722d45(2021年4月25日)。 (由Junio C Hamano - gitster -在提交f16a466中合并,2021年5月7日)
Pretty:提供人工日期格式 签署人:胡振宁
添加占位符%ah和%ch来格式化作者日期和提交者日期,如——date=human does,这提供了更多的人性化日期输出。
漂亮格式现在包括在它的手册页:
'%ah'::作者日期,人类样式(类似于的——date=human选项 git rev-list)
漂亮格式现在包括在它的手册页:
'%ch'::提交者日期,人类样式(类似于的——date=human选项 git rev-list)
date -d @$(git log -n1 --format="%at") +%Y%m%d%H%M
请注意,这将转换为您的本地时区,以防这对您的用例很重要。
其他的是(来自git帮助日志):
--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
或者,影响你所有的git使用此帐户
git config --global log.date iso