LoginSignup
0
0

More than 3 years have passed since last update.

[Git] 過去のコミットの Author と Committer 情報を書き換える

Last updated at Posted at 2021-04-13

過去のコミット履歴から、特定のコミットの Author と Committer を一気に書き換える方法です。

経緯

自分は仕事とプライベートで GitHub アカウントを分けています。

最近、間違った方のアカウント名・メールアドレスでコミットしてしまっていたことに後から気付いて、修正したいことがありました。

まだプロジェクトとしては序盤でしたが、他の人のコミットも少し入っていて、その中から自分のコミットだけを修正する方法を調べたので、残しておきます。

やり方

便宜上

  • プライベートの GitHub アカウントを Neo <neo@example.com>
  • 仕事の GitHub アカウント名を Anderson <anderson@example.com>

として、 Neo <neo@example.com> => Anderson <anderson@example.com> に変更したいとします。

一応、変更前のコミットを見てみると、以下のようになっているはずです。

修正前
% git log --pretty=full -1
commit xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx (HEAD -> branch/hoge, origin/branch/hoge)
Author: Neo <neo@example.com>
Commit: Neo <neo@example.com>

これを上書きします。

※ コミット情報を上書きするので、念の為バックアップ用のブランチを切っておいた方が良いです

# 確認
git log --pretty=full

# 修正
git filter-branch --env-filter '
if [ $GIT_AUTHOR_NAME = "Neo" ]
then
    export GIT_AUTHOR_NAME="Anderson"
    export GIT_AUTHOR_EMAIL="anderson@example.com"
    export GIT_COMMITTER_NAME="Anderson"
    export GIT_COMMITTER_EMAIL="anderson@example.com"
fi'

実行後に git log --pretty=full してみると、該当するコミット(例では Author が Neo のコミット)だけが上書きされているはずです。

修正後
% git log --pretty=full -1
commit xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx (HEAD -> branch/hoge, origin/branch/hoge)
Author: Anderson <anderson@example.com>
Commit: Anderson <anderson@example.com>

あとは、 --force-with-lease を付けて git push すれば OK。

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0