Amending the author of a Git commit

Ellie Huxtable

修改 Git 提交的作者

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

修改最新提交的作者

这个方法既简单又方便!

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

使用 interactive rebase(交互式变基)

  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]"
    '

原文由 Ellie Huxtable 发布

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