4

I would like to remove the time and especially time zone information of all commits that have been created in a repository. Is that possible? And if not would it be possible to simply set the the time to 00:00 and time zone to +0000?
(I want to keep the date btw. It's just the time and timezone that I don't want.)

I have only found questions talking about changing the author of all commits, but nothing about just altering a property in all commits keeping the remaining information.

And yes I know there are other ways people could find out where I live etc, but this would be good enough for me.

Forivin
  • 14,780
  • 27
  • 106
  • 199

3 Answers3

6

You can't "not store a time", but you can always set the time to 00:00 and the TZ to +0000.

For new commits, the most direct way would be to set the GIT_COMMITTER_DATE and GIT_AUTHOR_DATE environment variables. For example at a bash command line you could say

export GIT_COMMITTER_DATE="$(date +%Y-%m-%d) 00:00:00+0000"
export GIT_AUTHOR_DATE="$(date +%Y-%m-%d) 00:00:00+0000"

You'd want to make sure you do this before the first commit of each day; perhaps by adding it to your login scripts or something.

Your phrasing suggests you might also have a history of commits. Those can be changed as well, e.g. by using git filter-branch with the env-filter option. See the filter-branch docs at https://git-scm.com/docs/git-filter-branch for details of how this works.

However, it's important to understand that this is a history rewrite - i.e. you would be replacing all existing commits with new commits, and if anyone else shares this repo, it would put them in a broken state requiring some recovery. (See "recovering from upstream rebase" in the git rebase documentation for an understanding of what would be involved.) There's no way around this really - the commit and author dates are an integral part of each commit.

Mark Adelsberger
  • 42,148
  • 4
  • 35
  • 52
  • 1
    A bit cleaner and without race condition: `UTC_DAY_BEGIN=$(TZ=0 date +%F)T00:00:00+0000; export GIT_COMMITTER_DATE=$UTC_DAY_BEGIN; export GIT_AUTHOR_DATE=$UTC_DAY_BEGIN` ``` – ominug Jan 22 '21 at 18:36
  • @ominug Why do you bleieve this makes a difference with respect to any "race condition"? – Mark Adelsberger Jan 22 '21 at 22:27
  • Extremely rare, but my code fetches the date only once, yours twice. The day could change between the fetches. – ominug Feb 05 '21 at 11:57
  • Even if I took that seriusly, it's not a race condition\. – Mark Adelsberger Feb 05 '21 at 14:46
  • I mean, if the day changes between the fetches, the both variables would differ. From that, anybody could deduce that the (shell rc) script has run at exactly 00:00. I think this is not in line with the desire described in the question of "removing time and especially time zone information". So while it is very rare, it is a bug (independent of whether you call it [race condition](https://en.wikipedia.org/wiki/Race_condition) or not). – ominug Feb 06 '21 at 08:36
5

Warning: Editing any information of a commit will change its hash and, consequently, the hash of all descendant commits, which may lead to "rewriting" history if anyone else has already fetched the commit in question.


As mentioned, git filter-branch can be used to rewrite many commits at once.

Editing only the timestamps can be accomplished with its environment filter:

This filter may be used if you only need to modify the environment in which the commit will be performed. Specifically, you might want to rewrite the author/committer name/email/time environment variables (see git-commit-tree for details).

Specifically, one can set the GIT_AUTHOR_DATE and GIT_COMMITTER_DATE environment variables with values in the git internal date 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 negativeoffset from UTC. For example CET (which is 1 hour ahead of UTC) is +0100.

Warning: The following code examples rewrite the entire tree at once.

They can be simply changed directly in the env-filter.

I would like to remove the time and especially time zone information of all commits that have been created in a repository.

To remove only the timezone information, one may just set the date variables to $timestamp +0000:

git filter-branch --env-filter '
  GIT_AUTHOR_DATE="$(git show -q --format="%at" "$GIT_COMMIT") +0000"
  GIT_COMMITTER_DATE="$(git show -q --format="%ct" "$GIT_COMMIT") +0000"
  ' -- --all

(I want to keep the date btw. It's just the time and timezone that I don't want.)

To remove both the time and timezone, it is a little more tricky (using the ISO 8601 format):

git filter-branch --env-filter '
  author_ts="$(git show -q --format="%at" "$GIT_COMMIT")"
  committer_ts="$(git show -q --format="%ct" "$GIT_COMMIT")"
  GIT_AUTHOR_DATE="$(date -d "@$author_ts" +"%Y-%m-%dT00:00:00 +0000")"
  GIT_COMMITTER_DATE="$(date -d "@$committer_ts" +"%Y-%m-%dT00:00:00 +0000")"
  ' -- --all

Note: The timezone information does not appear in the format example, so this might break in the future. So one could also use the TZ environment variable to set the timezone, but I'm not sure how portable it is:

TZ=UTC git filter-branch --env-filter '
  author_ts="$(git show -q --format="%at" "$GIT_COMMIT")"
  committer_ts="$(git show -q --format="%ct" "$GIT_COMMIT")"
  GIT_AUTHOR_DATE="$(date -d "@$author_ts" +"%Y-%m-%dT00:00:00")"
  GIT_COMMITTER_DATE="$(date -d "@$committer_ts" +"%Y-%m-%dT00:00:00")"
  ' -- --all
kelvin
  • 1,421
  • 13
  • 28
  • Thanks for the information! Unfortunately it only changed the time zones. The times themselves are still there. edit: Sorry I just realized that you literally mention that above the code. – Forivin Jul 17 '18 at 19:35
  • 1
    You're welcome :). I managed to change the time aswell and added it to the answer. – kelvin Jul 17 '18 at 20:05
0

I never have to use that, but someone here creates a plugin to manage this situation. You can access the code here: https://github.com/bitriddler/git-change-date

Pankwood
  • 1,799
  • 5
  • 24
  • 43
  • Thanks, I tried the tool. It's buggy (it will apply incorrect time zones and times) and besides that it would take me hours to go through all the commits. :/ – Forivin Jul 17 '18 at 19:43