24

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.
Community
  • 1
  • 1
Izumi Kawashima
  • 1,197
  • 11
  • 25
  • 6
    #1 is off-topic, but "sometimes it's useful". For #2, to set the timezone for a specific command, say e.g. `TZ=UTC git commit` – jthill May 26 '14 at 16:57
  • 2
    @jthill - that might set the `TZ` environment var, but that isn't a cross-platform way to set the timezone for a specific command. Git runs on Windows too, ya know. – Matt Johnson-Pint May 27 '14 at 21:34
  • 1
    @MattJohnson Well, it works on Git for Windows (aka msysgit) for `git`, `gitk`, `date`, `ls`, and I've no doubt everything else there. The subject being git, and the suggested method working for the whole git toolbox, I don't see what you could be referring to. Can you be more specific? – jthill May 27 '14 at 22:01
  • @jthill - Windows doesn't use the `TZ` env variable. You're talking about bash or cygwin or similar commands, not git specific parameters. Personally, I use GitHub for windows, which launches a powershell window for msysgit. The `TZ` var doesn't work there, and you certainly can't supply command-specific env var settings that way in powershell. Just saying, the OP was asking for a git-specific answer and there are many ways to run git where your suggestion won't work. – Matt Johnson-Pint May 27 '14 at 22:27
  • 1
    @MattJohnson Git for Windows most certainly does use TZ. GitHub for Windows isn't Git. If it can't do that, that's its problem, not git's. GitHub for Windows might not be cross-platform, but fortunately Git is. – jthill May 27 '14 at 23:13
  • Possible duplicate of [How to set the correct local time zone in git bash?](https://stackoverflow.com/questions/22766678/how-to-set-the-correct-local-time-zone-in-git-bash) – user Oct 29 '17 at 10:32

5 Answers5

13

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.

Saeb Amini
  • 23,054
  • 9
  • 78
  • 76
  • 10
    Commits have two timestamps, the "author date" and the "commit date". This answer sets the timezone of the author date, but not the commit date (check using `git log --pretty=fuller`). Setting the TZ environment variable [like jthill suggested](https://stackoverflow.com/questions/23874208/how-can-i-ignore-committing-timezone-information-in-my-commit#comment36750060_23874208) sets the timezone of both. – tom Nov 23 '17 at 03:11
  • For mac OS use `git config --global alias.commitutc '!git commit --date="$(date -u +%Y-%m-%dT%H:%M:%S%z)"'` – Ali Adnan Aug 20 '19 at 18:57
7

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.

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • 2
    This deserves several bug requests with Git. – Michael-O Jun 16 '14 at 06:54
  • 2
    @Michael-O - Perhaps one to add `--date=utc` as an option, but what else did you have in mind? – Matt Johnson-Pint Jun 16 '14 at 06:58
  • 2
    several things can done done. First of all, the ISO format of Git is not ISO conformat, that needs to be fixed. `--date=iso` can remain as-is and retain original time zones. `--date=iso-local` should normalize all timestamps to the local time zone and omit the offset and `--date=iso-utc` should normalize to UTC and add a `Z` to denote the UTC time zone. That would be perfect. – Michael-O Jun 16 '14 at 07:43
  • 1
    @Michael-O date==iso-local is (soon) a reality: http://stackoverflow.com/a/32990722/6309 – VonC Oct 07 '15 at 11:16
1

Combining an answer to a totally different question and this question, I made the following and added it to my ~/.bashrc (or ~/.zshrc depending on the system).

This will result in all git commit operations being run as UTC:

git()
{
    if [[ $# -ge 1 && "$1" == "commit" ]]
    then
        TZ=UTC command git "$@"
    else 
        command git "$@"
    fi
}
derpedy-doo
  • 1,097
  • 1
  • 18
  • 22
1

Ommiting the timezone in your commit is also a important from the point of information security. The timezone tells everybody in which timzone do you live and in case security is important for you, you should omit these information.

Other answers to this question focus on the author date, but for each commit Git stores a author date and a commit date. So you have to omit the timezone for both dates.

I solved this for my self with the help of the following Git alias:

[alias]
    co = "!f() { \
            export GIT_AUTHOR_DATE=\"$(date -u +%Y-%m-%dT%H:%M:%S%z)\"; \
            export GIT_COMMITTER_DATE=\"$(date -u +%Y-%m-%dT%H:%M:%S%z)\"; \
            git commit $@; \
            git log -n 1 --pretty=\"Autor: %an <%ae> (%ai)\"; \
            git log -n 1 --pretty=\"Committer: %cn <%ce> (%ci)\"; \
          }; f"

This alias outputs also after the commit the important properties of this commit, so that I can check them easily.

Oliver
  • 3,815
  • 8
  • 35
  • 63
0

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.
aleb
  • 2,490
  • 1
  • 28
  • 46