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