Amending the author of a Git commit

Ellie Huxtable

修改 Git commit 的作者

我很常不小心用錯電子郵件來提交 commit。我有幾個電子郵件會依不同用途使用,所以確保正確是很重要的 :)

修改最新一筆 commit 的作者

這個方法簡單又輕鬆!

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

使用 interactive rebase(互動式變基)

  1. git rebase -i 在你想要的任何基底上執行
  2. 將你想修改的 commit 標記為 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 進行翻譯