How can I ignore committing timezone information in my commit?

杀马特。学长 韩版系。学妹 提交于 2019-12-18 11:48:10

问题


Recently, I forked a repository hosted by github, with contributors spreading all over the world, and found out that the each commit log contains committer's timezone information.

2013-11-07 02:31:41 +0545 <-- This committer is living in Nepal. Surely.
2013-11-04 12:58:36 -0600 <-- This committer is living in CST or Ecuador or Chili or ...
2013-10-31 10:36:36 +0700 <-- This committer is living in Indonesia or Thai or Mongolia or Laos or Australia or ...
:

I know it's possible to hide this by editing the output form (e.g. git: timezone and timestamp format), but this hides what's actually saved in github's repository, only from my eye. Each committer's timezone is surely saved in github's server.

So my questions:

  1. Why are committer's timezone needed for commits? What is it used for? Isn't UTC time enough?
  2. Are there any options to ignore MY computer's timezone setting when committing? I don't want to set my computer's timezone to UTC, only because git is implicitly committing it.

回答1:


You can use this command to commit in UTC time:

git commit --date="`date --utc +%Y-%m-%dT%H:%M:%S%z`"

You can also alias it to a convenient name:

git config --global alias.commitutc '!git commit --date="$(date --utc +%Y-%m-%dT%H:%M:%S%z)"'

And do git commitutc.

For a more detailed explanation take a look at this blog post.




回答2:


When committing, git stores the Unix timestamp (seconds since 1/1/1970 UTC), and the local offset of the committer. You can override the offset, but you also have to supply the date as well.

git commit --date 1401179025 -0700

Multiple formats are supported, as documented here. I prefer the ISO-8601 format, which is like this:

git commit --date 2014-05-27T01:23:45-07:00

You can set the offset however you like. Use zero for UTC. Personally, I think this is unnecessary. It actually reduces the amount of information in the log. You may only care about the exact moment in time, but perhaps one might also care what time it was for that particular committer. For example, maybe you'd like to know if that person committed early in the morning or late at night. If you don't store the local offset, then that information is lost, and storing it doesn't hurt.

If your primary concern is that viewing the git log doesn't align all of the commits to a single time zone, consider adjusting the log output using the --date options on the log command:

git log --date=local

The above uses the commit offsets to adjust the commit date to your own local time zone.

I didn't see anything that would adjust it to UTC directly, but you could set your own time zone to UTC and then use this command.



来源:https://stackoverflow.com/questions/23874208/how-can-i-ignore-committing-timezone-information-in-my-commit

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