如何修改提交的作者而不改变日期?

8

我已经知道如何更改提交的作者(作者和提交者字段)。

git rebase --root --exec "git commit --amend --reset-author --no-edit"

但是当作者更改时,日期(作者日期和提交日期)会改变为当前日期。我该如何同时保存旧日期并更改作者?


请参考 https://git-scm.com/docs/git-filter-branch,特别是 --env-filter - user3159253
@user3159253 我找到了一些问题,建议采用这种方法。但它并没有帮助我。 - JonyStack
请提供您在尝试使用“filter-branch”方法时遇到的确切问题描述。我相信它会有所帮助,因为我之前解决过类似的问题。如果我知道您任务的全部情况,特别是需要重写多少个提交、有多少个分支等,我可以提供一个确切的解决方案... - user3159253
@user3159253 有2个分支(主分支,开发分支),每个提交都来自根目录。 - JonyStack
@user3159253 好的,我在 Git Pro 中找到了关于 filter-branch 的内容,然后看了这个教程:https://help.github.com/en/articles/changing-author-info。所以我解决了我的主要问题。谢谢你。另外,你能告诉我更多关于 --msg-filter 的信息吗?我该如何指定正确的提交进行重写? - JonyStack
这个回答解决了你的问题吗?如何在修改 Git 提交时更新作者但保留原始日期? - DharmaTurtle
4个回答

8

我在这里回答了。简而言之:

git -c rebase.instructionFormat='%s%nexec GIT_COMMITTER_DATE="%cD" GIT_AUTHOR_DATE="%aD" git commit --amend --no-edit --reset-author' rebase -f <commit/branch before wrong author and email, or --root to rebase all>

以下命令(注意包含名称和电子邮件地址)修改了作者和提交字段,并更新了CommitDate字段:git rebase --root --exec 'git commit --amend --author="My Name <myemail@domain.com>" --no-edit'但可以通过使用 git rebase --committer-date-is-author-date --root 命令进行修复。相比之下,这个命令完全保留了所有日期的所需效果:git -c rebase.instructionFormat='%s%nexec GIT_COMMITTER_DATE="%cD" GIT_AUTHOR_DATE="%aD" git commit --amend --no-edit --reset-author' rebase --root - user14391097

4
这并不是您问题的完整解决方案,因为提交日期仍在更新(毕竟这确实改变了提交),但对于只想保持作者日期不变的人可能会有用。
与使用--reset-author同时更新作者日期不同,您可以直接显式设置作者。
git rebase --root --exec "git commit --amend --author=John --no-edit"

你可以明确指定作者,也可以使用搜索模式(以上示例所做的就是这样)。

--author=

覆盖提交者。使用标准的 A U Thor 格式指定显式作者。否则,它被认为是一个模式,并用于搜索由该作者创建的现有提交(即 rev-list --all -i --author=)。然后将从找到的第一个提交中复制提交作者。

来源


3

以上的答案都没有完全适合我, 我使用 git filter-repo 工具,并带有 --mailmap 选项。 安装 git 扩展后,请按照以下步骤进行操作:

  1. Create a my_mailmap file like this:

    New Name <new-mail@address.com> Old Name <old-mail@address.com>
    
  2. Run the following command in your repos root directory:

    git filter-repo --mailmap my_mailmap
    

    (I additionally also needed the --force option)

但请注意,这将替换整个历史记录中的邮件地址和姓名,请确保您知道自己在做什么。您可以使用git show -s --format=fuller单独确认提交者和提交时间:

Author:     Author Name <mail@address.com>
AuthorDate: Wed Dec 30 10:27:44 2020 +0100
Commit:     Commiter Name <mail@address.com>
CommitDate: Wed Dec 30 10:27:44 2020 +0100

1
这对我有用。我还需要在git filter-repo中使用--force选项。运行过滤器后,它删除了我的远程源,所以我必须在git remote add origin ....中添加它回来。要推送到我的现有存储库,我必须使用git push --force,或者您可以创建一个新的空存储库并将其推送到其中。所有提交都已正确更新作者,并保留了提交日期。感谢@tidreyer! - VoidZA

1

使用--ignore-date标志或--committer-date-is-author-date

git rebase --ignore-date

我尝试将您建议的命令与我的命令结合起来:$ git rebase --root --ignore-date --exec "git commit --amend --reset-author --no-edit",但出现了fatal: cannot combine am options with either interactive or merge options - JonyStack

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接