git:时区和时间戳格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7651644/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 11:59:41  来源:igfitidea点击:

git: timezone and timestamp format

gittimezoneformattimestamptimezone-offset

提问by SandyBr

From git I can get the timestamp:

从 git 我可以得到时间戳:

"2011-10-04 12:58:36 -0600"

but is there any way to show it as:

但有什么方法可以将其显示为:

"2011-10-04 06:58:36"

So all I want is to get rid of the -0600 timezone offset. How can I do this? Thanks.

所以我想要的就是摆脱 -0600 时区偏移。我怎样才能做到这一点?谢谢。

回答by Lazy Badger

If you ask about git log, you can try and select most correct form from:

如果您询问 git log,您可以尝试从以下选项中选择最正确的形式:

git log --date={relative,local,default,iso,rfc}

--date=localseems to be the best candidate.

--date=local似乎是最佳人选。

To make this permanent, use git config --global log.date local.

要使其永久化,请使用git config --global log.date local.

回答by bond

git log --date=local

Does the trick.

有诀窍。

git config --global log.date local

回答by Christian

TZ=UTC git log --date=local

in order to get non-local-timezone one-timezone output.

为了获得非本地时区的单时区输出。

回答by Fabio Marreco

Unfortunately, using git log --date=localas explained in previous answers changes the output format.

不幸的是,git log --date=local如先前答案中所述,使用会更改输出格式。

To keep the format as asked (YYYY-MM-DD HH:mm) I had to use:

为了保持要求的格式(YYYY-MM-DD HH:mm),我必须使用:

git log --date=iso-local

But that only works on git 2.7 or above.

但这仅适用于 git 2.7 或更高版本。

回答by Will Wright

To get the format (YYYY-MM-DD HH:hh), you can use:

要获取格式 ( YYYY-MM-DD HH:hh),您可以使用:

git log --date=format:%Y-%m-%d\ %H:%M

Works beautifully with Git Standup too: https://github.com/kamranahmedse/git-standup

也可以与 Git Standup 完美配合:https: //github.com/kamranahmedse/git-standup

回答by papaJupe

jveerman's post was really helpful:

jveerman 的帖子真的很有帮助:

If you want to display the git date in YYYY-MM-DD HH:MM:SS format:

如果要以 YYYY-MM-DD HH:MM:SS 格式显示 git 日期:

DATE=$(git log -n 1 --pretty=format:"%ad" --date=iso)
echo "Date: ${DATE::20}"

For log format I was able to add this

对于日志格式,我可以添加这个

[log]
date=format:%Y-%m-%d %H:%M:%S

to my ~/.gitconfig

到我的 ~/.gitconfig

but getting the same nicely formatted date/time added automatically to my commit messages was an ordeal. I found nothing helpful until I added this to the .git/hooks/prepare-commit-msg file:

但是将同样格式良好的日期/时间自动添加到我的提交消息中是一种折磨。在我将它添加到 .git/hooks/prepare-commit-msg 文件之前,我发现没有任何帮助:

DATE=$(git log -n 1 --pretty=format:"%ad" --date=iso)
echo "${DATE::20}" >> 

If you're mainly using the Desktop app, it's lovely to have the exact time of change shown with the commit listing!

如果您主要使用桌面应用程序,那么在提交列表中显示确切的更改时间会很高兴!

Is there any way to make this global, so I don't have to edit each local repo's prepare-commit-msg file ?

有什么方法可以使这个全局化,所以我不必编辑每个本地存储库的 prepare-commit-msg 文件?

回答by Kevin English

A full command line answer:

完整的命令行答案:

TZ=GMT git show -s --format=%cd --date=iso-local

回答by jveerman

If you want to display the git date in YYYY-MM-DD HH:MM:SS format:

如果要以 YYYY-MM-DD HH:MM:SS 格式显示 git 日期:

DATE=$(git log -n 1 --pretty=format:"%ad" --date=iso)
echo "Date: ${DATE::20}"

DATE=$(git log -n 1 --pretty=format:"%ad" --date=iso)
echo "Date: ${DATE::20}"