Git:批量更改提交日期

Hag*_*idV 4 git git-rewrite-history

当我需要更改各种提交的提交日期时,我使用交互式变基并一一更改它们。

我怎样才能用一个命令来改变它们呢?换句话说,我需要将给定命令应用于交互式变基中列出的所有提交。

谢谢

Una*_*dra 5

过滤器仓库

git filter-branch已弃用。相反,使用git filter-repo. 您需要安装它。

这是一篇关于如何使用 git-filter-repo 修改提交日期的优秀文章。git -filter-repo 文档--commit-callback很好地解释了这个概念。

一个非常简单的例子

让我们将所有提交日期的时区重置为零。

# Save this as ../change_time.py
def handle(commit):
    "Reset the timezone of all commits."
    date_str = commit.author_date.decode('utf-8')
    [seconds, timezone] = date_str.split()
    new_date = f"{seconds} +0000"
    commit.author_date = new_date.encode('utf-8')

handle(commit)

Run Code Online (Sandbox Code Playgroud)
# You need to be in a freshly-cleaned repo. Or use --force.
git clone <...> your_repo
cd your_repo
# First just a dry run.
git filter-repo --dry-run --commit-callback "$(cat ../change_time.py)"
# And now do it for real
git filter-repo --commit-callback "$(cat ../change_time.py)"
Run Code Online (Sandbox Code Playgroud)