🐷

GitにPushしちゃったあとでAuthorを変更したい

2024/12/05に公開

概要

  • GitでPushしたあとに気づいて直したいことがある
  • ローカルのGit Config更新忘れでCommitのAuthorが意図しないアカウントになっちゃった系
  • あとからGit履歴を修正してforceで書き換える

手順

まずは自分のローカル環境を見てみる

git config --list --show-origin | grep user

user.nameuser.email が古くなっていないか確認。今回は古いメールアドレスを使っていたのでここを更新忘れたままremote pushをしてしまった。

忘れないうちに名前を更新しておく

git config user.name "new_name"
git config user.email "new-email@example.com"

続いてgit履歴も書き換える

git filter-repo --commit-callback '
if commit.author_name == "old_name":
    commit.author_name = "new_name"
    commit.author_email = "new_email@example.com"
if commit.committer_name == "old_name":
    commit.committer_name = "new_name"
    commit.committer_email = "new_email@example.com"

git log を実行して履歴が修正されていることを確認する。その後RemoteにPushしなおす

git push -f origin master

Pushする際には残念ながらforce pushしかない。履歴を書き換えるのはそれだけ重いことなのだ……

Discussion