0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Gitで別アカウントでコミットしてしまった場合の対処法

0
Last updated at Posted at 2026-05-26

経緯

Gitで作業中、誤って別のアカウントでコミット・プッシュしてしまいました。
コミット履歴に別アカウントの名前・メールアドレスが残ってしまったため、
コミット情報の修正とアカウントの切り替えを行いました。

現在の状況確認

まず、直近のコミット履歴とAuthor情報を確認します。

git log --oneline --format="%h %an <%ae> %s" -10

出力例:

abc1234 誤ったアカウント名 <wrong@example.com> コミットメッセージ1
def5678 誤ったアカウント名 <wrong@example.com> コミットメッセージ2

誤ったアカウントが何件あるか確認しておきましょう。

コミット情報を修正する

直前の1件だけ修正する場合

git commit --amend --author="正しい名前 <correct@example.com>" --no-edit
git push --force origin ブランチ名

複数コミットをまとめて修正する場合

① rebaseを開始する

git rebase -i HEAD~N  # Nは修正したいコミット件数

⚠️ コミット数がリポジトリの総コミット数と同じ場合は HEAD~N が使えません。
その場合は --root オプションを使います。

git rebase -i --root

② エディタが開いたら pick → edit に変更して保存

edit abc1234 コミットメッセージ1
edit def5678 コミットメッセージ2

③ 各コミットを修正する

editにしたコミットごとに以下を実行します。

git commit --amend --author="正しい名前 <correct@example.com>" --no-edit
git rebase --continue

④ 全件完了したらforce pushする

git push --force origin ブランチ名

⚠️ --force push は共有ブランチの場合、チームメンバーに影響します。
個人ブランチであれば問題ありません。事前にチームへ共有しましょう。

修正後の確認

git log --format="%h %an <%ae> %s" -10

全てのコミットが正しいアカウント名・メールアドレスになっていればOKです。

次回からのアカウントを切り替える

再発防止のため、Gitのユーザー情報を正しいアカウントに設定します。

リポジトリ単位で設定する(推奨)

特定のリポジトリだけ別アカウントを使いたい場合はこちら。

git config user.name "正しい名前"
git config user.email "correct@example.com"

グローバルで設定する

全リポジトリのデフォルトを変更したい場合はこちら。

git config --global user.name "正しい名前"
git config --global user.email "correct@example.com"

設定の確認

# リポジトリ単位の設定確認(globalより優先される)
git config --local --list

# グローバル設定の確認
git config --global --list
優先度 設定範囲 コマンド
高 リポジトリ単位 git config(--local)
低 全リポジトリ git config --global

Tips

全コミットのAuthorを一覧確認する

修正漏れがないか確認したいときに便利です。

git log --format="%an <%ae>" | sort | uniq

正しいアカウントのみ表示されればOKです。

GitHubのContributorsに以前のアカウントが残る場合

force pushでコミット履歴を書き換えても、GitHubのContributorsページはキャッシュされているため、すぐには反映されないことがあります。
数時間〜数日待てば自動的に更新されます。特別な対処は不要です。

GitHubに登録されていないメールアドレスを使った場合

コミットはpushできますが、GitHubアカウントと関連付けられず、プロフィールのコントリビューショングラフが増えません。
GitHubの Settings → Emails で登録済みのメールアドレスを使用しましょう。

プライバシーを守りたい場合は、GitHubが提供するnoreplyアドレスを使う方法もあります。

{ID}+{username}@users.noreply.github.com

Settings → Emails → Keep my email address private を有効にすると確認できます。

0
0
2

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?