如何更改 Git 日志日期格式 [英] How to change Git log date formats

查看:18
本文介绍了如何更改 Git 日志日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

推荐答案

其他是(来自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 魔法.

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"

这里的第一步为您提供毫秒时间戳.您可以根据需要更改第二行以格式化该时间戳.此示例为您提供了类似于 --date=local 的内容,但带有填充的日期.

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 

或者,为了影响您使用此帐户使用的所有 git

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

git config --global log.date iso

这篇关于如何更改 Git 日志日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆