Amending the author of a Git commit

Ellie Huxtable

修改 Git commit 的作者

原文由 Ellie Huxtable 發布,訂閱此部落格

還滿常不小心用錯 email 來 commit 的。我有幾個 email 會依不同用途分開使用,所以確保用對是很重要的 :)

修改最新一筆 commit 的作者

這個最簡單!

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

使用互動式 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

我第一次是在 split git repo 裡提到 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 進行翻譯

留言