Amending the author of a Git commit

Ellie Huxtable

Gitコミットの作成者を修正する

コミットでメールアドレスを間違えてしまうことは、けっこうよくあります。用途によって使い分けたいメールアドレスがいくつかあるので、正しく設定できていることが大切です :)

直前のコミットの作成者を修正する

これは簡単です!

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

インタラクティブリベースを使う

  1. 好きなベースを指定して git rebase -i を実行します
  2. 変更したいコミットを pick ではなく edit にします
  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-filter-repoについては、以前Gitリポジトリを分割するで初めて紹介しましたが、ここでも便利です。filter-branchよりもこちらがおすすめです。ただし、標準のGitインストールには含まれていません。

次のコマンドでインストールします。

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

その後、次のどちらかを実行します。

mailmapを使う場合

mailmapで old -> new の対応付けを設定しておけば、次を実行できます。

git filter-repo --use-mailmap

mailmapを使わない場合

そうでなければ、次の方法がうまく機能します。

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

原文は Ellie Huxtable により に公開されました。

この記事は「gpt-5.6-terra」を使用して翻訳されました。