2.

Git エラー一覧|push / pull / merge / rebase の頻出エラーと対処入口

編集
この記事の要点
  • Git のエラー一覧入口ページ
  • カテゴリ: リモート / プッシュ系 / 作業ツリー・インデックス / 参照名・ファイル指定 / ロック・プロセス
  • 切り分け順: エラーメッセージ最初の 1 行を読む → git status 確認 → git log --oneline -20
  • 頻出: non-fast-forward / merge conflict / Permission denied / detached HEAD / index.lock
  • 子ページから個別のエラーを選択

Git エラーの 5 大カテゴリ

カテゴリ典型エラー原因の方向性
リモート/プッシュ系! [rejected] non-fast-forward / Permission deniedリモートが進んでいる / 認証問題
作業ツリー/インデックスYour local changes would be overwritten / Unmerged paths未コミットの変更とぶつかる
参照名・ファイル指定pathspec did not match any files / ambiguous argumenttypo / ファイル存在しない
マージ/リベースCONFLICT (content): Merge conflict / could not apply同じ箇所を別 commit で変更
ロック・プロセスUnable to create '.git/index.lock' / Another git process既存 Git プロセス / 異常終了の残骸

エラー出たときの調査フロー

  1. エラーメッセージの最初の 1 行を読む(fatal: / error: / hint: の見出しが種類を表す)
  2. git status で作業ツリーの状態を確認
  3. git log --oneline -20 で最近のコミット履歴を確認
  4. git remote -v でリモートの設定を確認
  5. git branch -avv でブランチとリモート追従を確認
  6. 慌てて git reset --hardgit push --force取り返しがつかないので慎重に

頻出エラー Top 10

エラー原因対処
! [rejected] non-fast-forwardリモートに別 commit があるgit pull --rebase → 解決 → git push
Permission denied (publickey)SSH 鍵が設定されていないssh-add ~/.ssh/id_ed25519 / GitHub に公開鍵登録
fatal: refusing to merge unrelated histories履歴が無関係なリポジトリ同士をマージ--allow-unrelated-histories
Your local changes would be overwritten by merge未コミット変更とマージが衝突git stashgit pullgit stash pop
CONFLICT (content): Merge conflict in xxx同じ箇所を別 commit で変更ファイル編集 → git addgit commit
fatal: pathspec 'xxx' did not match any filesファイルパスの typo正しいパスに直す
fatal: Unable to create '.git/index.lock'別 Git プロセスが動いている / 異常終了残骸rm -f .git/index.lock
detached HEAD stateブランチでない位置をチェックアウトgit switch -c new-branch で新規ブランチ作成
fatal: not a git repositoryカレントディレクトリが Git 管理下にないgit init / 正しいディレクトリに移動
HTTP 403 / 401 push 時HTTPS 認証情報が古い / Personal Access Token 失効git credential 削除 / PAT 再発行

「やらかし」リカバリ集

間違って commit した

# 直前の commit を取り消し(変更は残す)
git reset --soft HEAD~1

# 直前の commit メッセージを書き換え(既に push 済みでも可だが force push 必要)
git commit --amend

# 既に push 済みなら revert(新規 commit で打ち消す)
git revert HEAD
git push

間違ったブランチで作業した

# 現在の変更を別ブランチに移す
git stash
git checkout 正しいブランチ
git stash pop

# commit 済みなら cherry-pick
git checkout 正しいブランチ
git cherry-pick <間違ったブランチの commit ハッシュ>

# 間違ったブランチから commit を取り消し
git checkout 間違ったブランチ
git reset --hard HEAD~1

大事なファイルを消した / 上書きした

# 直前の commit 状態に戻す
git checkout HEAD -- path/to/file

# 数 commit 前の状態を取得
git checkout HEAD~3 -- path/to/file

# reflog で「失われた」commit を探す
git reflog
# 該当 commit を確認
git show 
# その状態に戻す
git reset --hard 

conflict が出たけど解決方法がわからない

# 衝突しているファイル一覧
git status

# 衝突箇所には以下が入っている
# <<<<<<< HEAD
# 自分の変更
# =======
# 相手の変更
# >>>>>>> branch_name

# 手動で編集して <<<<< >>>>>>> を消す
# またはツールを使う
git mergetool

# 解決したら
git add <ファイル>
git commit
# rebase 中なら
git rebase --continue

# 全部取り消したい
git merge --abort
git rebase --abort

push 拒否されたとき

# non-fast-forward の典型
git pull --rebase    # リモートの変更を取り込む(rebase 方式)
git pull             # マージ方式

# 自分の変更を確実に残したい場合
git pull --rebase
# conflict が出たら個別解決 → git rebase --continue

# ★ NG: 安易に強制 push しない(チームメンバーの commit を消す可能性)
# git push --force  ← 危険

# ★ 個人ブランチで完結する場合のみ許容
git push --force-with-lease  # 他人の変更があれば拒否(より安全)

子ページ(個別エラー詳細)

  • ! [rejected] non-fast-forward の対処
  • fatal: refusing to merge unrelated histories の対処
  • Permission denied (publickey) の対処
  • fatal: Unable to create '.git/index.lock' の対処
  • CONFLICT (content): Merge conflict の解決手順
  • detached HEAD 状態からの復帰
  • 各種 git reset / revert / restore の使い分け

各エラーの詳細解説は子ページを参照してください。

編集
Post Share
子ページ
  1. fatal: remote origin already exists.
  2. fatal: '~' does not appear to be a git repository
  3. Cannot rebase: You have unstaged changes. Please commit or stash them.
  4. remote: error: denying non-fast-forward refs/heads/master (you should pull first)
  5. error: pathspec ... did not match any file(s) known to git.
  6. The following untracked working tree files would be overwritten by checkout
  7. fatal: Not a valid object name: 'master'.
  8. Unlink of file 'ファイル名' failed. Should I try again? (y/n)
  9. Another git process seems to be running in this repository, ~
  10. error: Your local changes to the following files would be overwritten by checkout:
同階層のページ
  1. 用語一覧
  2. エラー一覧
  3. git本体のインストール(Linux)
  4. Linuxサーバーへのgit導入とクライアントのセットアップ
  5. リモートリポジトリをローカルリポジトリとしてクローンする方法
  6. リモートとローカルのリポジトリを同期(pull)する方法
  7. 設定の確認
  8. gitユーザー名とemailの設定
  9. リモートリポジトリの作成
  10. ローカルリポジトリの作成
  11. 新規ファイル/ディレクトリをインデックスに登録
  12. インデックスの登録状態を確認
  13. ローカルリポジトリの変更をコミット
  14. コミット履歴の確認
  15. クライアントからリモートリポジトリの接続設定、確認、削除
  16. リポジトリへのプッシュ
  17. リモートリポジトリからクライアントへのSSHクローン
  18. リモートとローカルの差分表示
  19. バージョンの確認
  20. プロキシの設定
  21. ローカルをリモートリポジトリの状態に戻す
  22. ブランチの作成, 一覧表示, 切り替え
  23. ブランチのマージと削除
  24. リベース
  25. .gitignoreの書き方
  26. .gitignoreの設定が反映されない場合
  27. 特定のファイルをgitの管理から外す方法
  28. 参照(ORIG_HEAD, HEAD, FETCH_HEAD)
  29. git rm [-r --cached] の取り消し
  30. 一部のディレクトリ/ファイルのみをリポジトリから復元する方法
  31. ローカルとリモートリポジトリの有無を同期
  32. pushの取消し方法
  33. マージツールの起動方法
  34. Gitで「MERGING」の状態a