問題
プライベートリポジトリへのコミットで、誤って関係ない情報のGit設定で作業してしまい、著者情報が間違ってしまいましたので、修正の過程をまとめてた内容です。
commit abc123def456ghi789jkl101112mnop131415qr (HEAD -> main, origin/main)
Author: incorrect_user <incorrect_email@example.com>
Date: Mon Sep 22 11:03:45 2024 +0900
push 3
commit stu161718vwx192021yz222324abc252627de
Author: incorrect_user <incorrect_email@example.com>
Date: Mon Sep 22 10:56:36 2024 +0900
push 2
commit fgh282930ijk313233lmn343536opq373839rs
Author: user_id <private_email@example.com>
Date: Mon Sep 22 09:57:09 2024 +0900
Initial commit
注意:commitやユーザー情報はマスク済みです。
解決方法
1. 最新のコミットの修正
-
対話的なrebaseを開始:
git rebase -i fgh282930ijk313233lmn343536opq373839rs -
エディタで最新のコミットを
editに変更:edit abc123d push 3 pick stu1617 push 2 -
著者情報を修正:
git commit --amend --author="user_id <hogehoge_email@example.com>" --no-edit -
rebaseを続行:
git rebase --continue
2. 2番目のコミットの修正
-
再度対話的なrebaseを開始:
git rebase -i fgh282930ijk313233lmn343536opq373839rs -
エディタで2番目のコミットを
editに変更:pick tuv4041 push 3 edit stu1617 push 2 -
著者情報を修正:
git commit --amend --author="user_id <hogehoge_email@example.com>" --no-edit -
rebaseを続行:
git rebase --continue
3. 変更の確認
git rebase --continue
4. リモートリポジトリへの反映
git push --force origin main
結果
全てのコミットの著者情報が正しく修正されました。
commit tuv404142wxy434445zab464748cde495051fg (HEAD -> main)
Author: user_id <hogehoge_email@example.com>
Date: Mon Sep 22 11:03:45 2024 +0900
push 3
commit hij525354klm555657nop585960qrs616263tu
Author: user_id <hogehoge_email@example.com>
Date: Mon Sep 22 10:56:36 2024 +0900
push 2
commit fgh282930ijk313233lmn343536opq373839rs
Author: user_id <hogehoge_email@example.com>
Date: Mon Sep 22 09:57:09 2024 +0900
Initial commit
注意点
- 強制プッシュ(
git push --force)は他の人と共有しているリポジトリではやめましょう。 - 重要なデータのバックアップを取ることをお勧めします。(一度ミスってコード消えたので焦りますした)
- もし何か問題が発生した場合は、
git reflogを使用して以前の状態に戻ることができます。
まとめ
今後同様の問題を防ぐために、.gitconfig.Aと.gitconfig.Bの設定を再確認し、必要に応じて調整しましょう。また、プロジェクトの.git/configファイルにuser.nameとuser.emailが設定されていないことを確認しましょう。(今回はグローバルにuser情報を残したままにしていたのが原因でした)