当我需要改变多个提交的提交日期时,我使用交互式变基并逐个更改它们。
我该如何在单个命令中更改它们所有提交的提交日期?换句话说,我需要将特定命令应用于在交互式变基中列出的所有提交。
谢谢
当我需要改变多个提交的提交日期时,我使用交互式变基并逐个更改它们。
我该如何在单个命令中更改它们所有提交的提交日期?换句话说,我需要将特定命令应用于在交互式变基中列出的所有提交。
谢谢
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)
# 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)"
git rebase
支持 --exec
选项, 可以完美实现此功能。
-x <cmd>
--exec <cmd>
在每个创建提交的行后附加 "exec <cmd>",以便在最终历史记录中执行。<cmd> 将被解释为一个或多个 shell 命令。任何失败的命令都将中断 rebase,并返回退出代码 1。
参考自 https://dev59.com/73RB5IYBdhLWcg3wAjNH#750182:
#!/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
。 - Unapiedragit rebase main --exec 'git commit --amend --date="now" --no-edit --allow-empty'
在其他答案中,建议将代码保存到文件.py中,并使用以下命令运行它:git filter-repo --commit-callback "$(cat ../<your file name>.py)"
但是在我的情况下,它忽略了所有空格,这在Python中会破坏语法。对我来说,直接将其粘贴到命令中可以解决问题。我正在使用Windows,在PowerShell上进行测试,对这些事情是初学者。
根据包含给定字符串的提交设置新日期。但是请注意,除非您有非常好的理由,否则不应更改日期!
if(b'[structural-programming]' in commit.message):
base_date = datetime(2021,1,1,0,0,0)
print(commit.message,' new date: ',base_date)
commit.committer_date = f'{int(datetime.timestamp(base_date))} +0100'.encode('utf-8')
commit.author_date = f'{int(datetime.timestamp(base_date))} +0100'.encode('utf-8')
elif(b'[intro-into-programming]' in commit.message):
base_date = datetime(2021,1,2,0,0,0)
print(commit.message,' new date: ',base_date)
commit.committer_date = f'{int(datetime.timestamp(base_date))} +0100'.encode('utf-8')
commit.author_date = f'{int(datetime.timestamp(base_date))} +0100'.encode('utf-8')
else:
base_date = datetime(2021,1,3,0,0,0)
print(commit.message,' new date: ',base_date)
commit.committer_date = f'{int(datetime.timestamp(base_date))} +0100'.encode('utf-8')
commit.author_date = f'{int(datetime.timestamp(base_date))} +0100'.encode('utf-8')
并且它如何在控制台中使用:
git filter-repo --commit-callback "
[seconds, timezone] = commit.committer_date.decode('utf-8').split()
if(b'[structural-programming]' in commit.message):
base_date = datetime(2021,1,1,0,0,0)
print(commit.message,' new date: ',base_date)
commit.committer_date = f'{int(datetime.timestamp(base_date))} +0100'.encode('utf-8')
commit.author_date = f'{int(datetime.timestamp(base_date))} +0100'.encode('utf-8')
elif(b'[intro-into-programming]' in commit.message):
base_date = datetime(2021,1,2,0,0,0)
print(commit.message,' new date: ',base_date)
commit.committer_date = f'{int(datetime.timestamp(base_date))} +0100'.encode('utf-8')
commit.author_date = f'{int(datetime.timestamp(base_date))} +0100'.encode('utf-8')
else:
base_date = datetime(2021,1,3,0,0,0)
print(commit.message,' new date: ',base_date)
commit.committer_date = f'{int(datetime.timestamp(base_date))} +0100'.encode('utf-8')
commit.author_date = f'{int(datetime.timestamp(base_date))} +0100'.encode('utf-8')
"