How can I ignore committing timezone information in my commit?

前端 未结 3 1604
攒了一身酷
攒了一身酷 2020-12-29 06:19

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 infor

相关标签:
3条回答
  • 2020-12-29 07:01

    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.

    0 讨论(0)
  • 2020-12-29 07:03

    Why are committer's timezone needed for commits? What is it used for? Isn't UTC time enough?

    The timezone is useful to figure out the local time of the author/committer doing the operation.

    According to https://git-scm.com/docs/git-commit-tree#_date_formats:

    Git internal format
    
    It is <unix timestamp> <time zone offset>, where <unix timestamp> is
    the number of seconds since the UNIX epoch. <time zone offset> is a
    positive or negative offset from UTC. For example CET (which is 1 hour
    ahead of UTC) is +0100.
    
    0 讨论(0)
  • 2020-12-29 07:11

    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.

    0 讨论(0)
提交回复
热议问题