Amending the author of a Git commit

Ellie Huxtable

修改 Git 提交的作者

原文由 Ellie Huxtable 发布,订阅该博客

我经常在提交时不小心用错邮箱。我有好几个邮箱,分别用于不同的用途,所以把邮箱写对很重要 :)

修改最新一次提交的作者

这个方法非常简单!

git commit --amend --author="Example Name <[email protected]>"

使用交互式变基

  1. 在你需要的任意基点上执行 git rebase -i
  2. 将需要修改的提交标记为 edit,而不是 pick
  3. 执行 git commit --amend --author="Example Name <[email protected]>",然后执行 git rebase --continue

使用 filter-branch

直接过滤整个历史!不过要小心,如果使用不当,filter-branch 可能会弄坏一些东西。

git filter-branch -f --commit-filter '
      if [ "$GIT_AUTHOR_EMAIL" = "[email protected]" ];
      then
              GIT_AUTHOR_NAME="New Name";
              GIT_AUTHOR_EMAIL="[email protected]";
              git commit-tree "$@";
      else
              git commit-tree "$@";
      fi' HEAD

使用 filter-repo

我之前在拆分 Git 仓库一文中提到过 git-filter-repo,它在这里也同样有用。相比 filter-branch,它是更优的选择,但默认并未包含在 Git 安装中。

安装方法如下

curl "https://raw.githubusercontent.com/newren/git-filter-repo/main/git-filter-repo" -o ~/.local/bin/git-filter-path

然后执行以下任意一种方式即可

使用 mailmap

如果你已经配置好了将旧邮箱映射到新邮箱的 mailmap,就可以运行

git filter-repo --use-mailmap

不使用 mailmap

否则,以下方式也很有效

git filter-repo --email-callback '
    return email if email != b"[email protected]" else b"[email protected]"
    '

本文章由 muse-spark-1.2-contributor 进行翻译

评论