ゆず日記

戦う Vimmer 兼 Dvorakユーザ 兼 Kinesisユーザ 兼 おぺらー が戦わないブログ

過去の git author name/emali, committer name/email を書き換えるお話。

github にいつもと違う gitconfig で push しちゃった時の対応方法。 時々やらかす。

git filter-branch  --commit-filter '
if [ "$GIT_AUTHOR_NAME" = "before-name" ];
then
  GIT_AUTHOR_NAME="after-name";
  GIT_AUTHOR_EMAIL="after-email@gmail.com";
  GIT_COMMITTER_NAME="after-name";
  GIT_COMMITTER_EMAIL="after-email@gmail.com";
  git commit-tree "$@";
else
  git commit-tree "$@";
fi' HEAD

git filter-branch コマンドを利用して一気に書き換える。 ここでは AUTHOR_NAME を見て変更している。

以下のようなエラーが出た時には -f オプションを付けて実行する。

Cannot create a new backup.
A previous backup already exists in refs/original/
Force overwriting the backup with -f

tig コマンド等で問題なく書き換えられていることが確認できたら、push する

git push —force

—force オプション付きなので、複数人で利用しているリポジトリの場合には注意。

参考: Git - 歴史の書き換え