You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将所有Git提交日期回溯2天?尝试git-filter-repo未成功

调整Git提交日期至“2天前”的实现方法

首先需要明确:要让GitHub显示提交为“2天前”,你需要将提交日期向前调整2天(而非向后)——比如今日是10号,提交日期改为8号,GitHub才会显示“2天前”。以下是用git-filter-repo实现的正确步骤:

1. 备份仓库(必做)

先克隆一份镜像仓库作为备份,避免操作失误丢失数据:

git clone --mirror <你的GitHub仓库地址> backup-repo

2. 调整所有提交的日期

在原本地仓库中,确保没有未提交的修改,然后运行以下git-filter-repo命令,将所有提交的作者日期和提交者日期统一向前推2天:

git filter-repo --commit-callback '
import datetime
# 向前调整2天,若要向后则改为 + offset
offset = datetime.timedelta(days=2)
# 处理作者日期
author_dt = datetime.datetime.fromisoformat(commit.author_date.replace("Z", "+00:00"))
commit.author_date = (author_dt - offset).isoformat().replace("+00:00", "Z")
# 处理提交者日期
committer_dt = datetime.datetime.fromisoformat(commit.committer_date.replace("Z", "+00:00"))
commit.committer_date = (committer_dt - offset).isoformat().replace("+00:00", "Z")
'

注:如果你的系统中未安装git-filter-repo,可通过pip install git-filter-repo完成安装。

3. 强制推送到GitHub

由于修改了Git历史,需要强制覆盖远程仓库的分支:

git push --force origin <你的分支名称>

备选方案:交互式Rebase(适用于少量提交)

如果只有最近几个提交需要修改,可使用交互式Rebase:

  1. 执行命令进入交互式Rebase模式(假设修改最近3个提交):
    git rebase -i HEAD~3
    
  2. 将每个提交前的pick改为edit,保存退出。
  3. 对每个提交,运行以下命令修改日期:
    GIT_COMMITTER_DATE="$(date -d '2 days ago')" git commit --amend --no-edit --date "$(date -d '2 days ago')"
    
  4. 完成所有提交修改后,执行git rebase --continue,最后强制推送。

内容的提问来源于stack exchange,提问作者Samuel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 19:06:12