删除所有 git 提交的时间和时区,但保留日期

Remove time and time zone of all git commits, but keep date

我想删除已在存储库中创建的所有提交的时间,尤其是时区信息。那可能吗?如果不能,是否可以简单地将时间设置为 00:00,将时区设置为 +0000?
(顺便说一句,我想保留日期。这只是我不想要的时间和时区。)

我只发现了关于 changing the author of all commits 的问题,但没有关于仅在所有提交中更改 属性 保留剩余信息的问题。

是的,我知道人们可以通过其他方式找到我住的地方等,但这对我来说已经足够了。

我从来不用它,但这里有人创建了一个插件来管理这种情况。您可以在此处访问代码:https://github.com/bitriddler/git-change-date

您不能"not store a time",但您始终可以将时间设置为00:00,将TZ 设置为+0000。

对于新提交,最直接的方法是设置 GIT_COMMITTER_DATEGIT_AUTHOR_DATE 环境变量。例如在 bash 命令行你可以说

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

您需要确保在每天第一次提交之前执行此操作;也许通过将它添加到您的登录脚本或其他东西。

您的措辞表明您可能也有提交历史。这些也可以更改,例如通过使用 git filter-branchenv-filter 选项。有关其工作原理的详细信息,请参阅 https://git-scm.com/docs/git-filter-branch 上的 filter-branch 文档。

但是,重要的是要了解这是一次历史重写——即您将用新的提交替换所有现有的提交,如果其他人共享此 repo,这会使它们处于需要恢复的损坏状态。 (请参阅 git rebase 文档中的 "recovering from upstream rebase" 以了解将涉及的内容。)这真的没有办法解决 - 提交和作者日期是每个提交的组成部分。

警告:编辑提交的任何信息都将更改其哈希值,并且, 因此,所有后代提交的哈希值,这可能会导致 "rewriting" history 如果其他人已经获取了提交 问题。


如前所述,git filter-branch 可用于重写许多提交 一次。

只编辑时间戳可以用它来完成 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).

具体可以设置GIT_AUTHOR_DATEGIT_COMMITTER_DATE 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.

警告:以下代码示例会立即重写整个树。

它们可以在 env-filter.

中简单地 changed directly

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

删除只有timezone信息,可以只设置日期 变量 $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.)

删除 时间时区,这有点棘手 (使用 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

注意format example中没有出现时区信息, 所以这可能会在未来中断。所以也可以 use the TZ 设置时区的环境变量,但我不确定它的便携性如何:

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