git: timezone and timestamp format

不想你离开。 提交于 2019-11-27 08:32:45
Lazy Badger

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

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

--date=local seems to be the best candidate.

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

git log --date=local

Does the trick.

git config --global log.date local
Christian
TZ=UTC git log --date=local

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

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

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

git log --date=iso-local

But that only works on git 2.7 or above.

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

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

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

jveerman's post was really helpful:

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

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

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:

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

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 ?

A full command line answer:

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

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!