【发布时间】:2020-07-06 21:07:30
【问题描述】:
当我需要更改各种提交的提交日期时,我使用交互式 rebase 并一一更改。
如何在一个命令中更改它们?换句话说,我需要将给定的命令应用于将在交互式 rebase 中列出的所有提交。
谢谢
【问题讨论】:
-
日期应该怎么改?全部设置为同一时间,还是一小时后全部移动,等等?
当我需要更改各种提交的提交日期时,我使用交互式 rebase 并一一更改。
如何在一个命令中更改它们?换句话说,我需要将给定的命令应用于将在交互式 rebase 中列出的所有提交。
谢谢
【问题讨论】:
git filter-branch 已弃用。相反,请使用git filter-repo。您需要安装它。
这里有一个excellent article,关于如何使用 git-filter-repo 来修改提交日期。 git-filter-repo documentation 很好地解释了--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)
# 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)"
【讨论】:
改编自https://stackoverflow.com/a/750182/7976758:
#!/bin/sh
git filter-branch --env-filter '
GIT_AUTHOR_DATE="2000-12-21 23:45:00"
GIT_COMMITTER_DATE="`date`" # now
export GIT_AUTHOR_DATE GIT_COMMITTER_DATE
' --tag-name-filter cat -- --branches --tags
【讨论】:
git filter-repo。
git rebase 支持the --exec option,它会做到这一点。
-x <cmd>
--exec <cmd>在最终历史记录中创建提交的每一行之后附加“exec
”。 将被解释为一个或多个 shell 命令。任何失败的命令都会中断 rebase,退出代码为 1。
【讨论】: