36
14

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】実務でよくあるGitエラーと対処法まとめ

36
Posted at

Gitを使っていると、ほぼ必ずエラーに遭遇します。

特に実務では、

  • pullしたらコンフリクトした
  • pushできない
  • ブランチを間違えた
  • コミットをやり直したい

といったトラブルが日常的に発生します。

今回は、実務でよくあるGitエラーとその対処法をまとめて紹介します。


1. failed to push some refs

push時によく見る代表的なエラーです。

! [rejected]        feature/sample -> feature/sample (non-fast-forward)
error: failed to push some refs

原因

リモート側に、自分のローカルにない最新コミットが存在しています。

他メンバーが先にpushしたケースでよく発生します。

対処法

まず最新を取得してから再度pushします。

git pull origin feature/sample
git push origin feature/sample

実務ポイント

いきなり push --force は避けましょう。
他メンバーの変更を上書きする可能性があります。


2. merge conflict

pullやmerge時によく発生します。

CONFLICT (content): Merge conflict in sample.js
Automatic merge failed

原因

同じファイルの同じ箇所を複数人で変更した場合に発生します。

対処法

対象ファイルを開くと以下のように表示されます。

<<<<<<< HEAD
自分の変更
=======
相手の変更
>>>>>>> main

不要な記号を削除し、内容を手動で整理します。

修正後は再度コミットします。

git add .
git commit -m "コンフリクト解消"

実務ポイント

慌てずに、どちらの変更を残すべきか確認しましょう。


3. fatal: not a git repository

Git管理されていない場所でコマンドを実行した時に出ます。

fatal: not a git repository

原因

リポジトリ配下ではないディレクトリで操作しています。

対処法

現在地を確認します。

pwd

正しいプロジェクト配下へ移動します。

cd プロジェクト名

4. nothing to commit, working tree clean

nothing to commit, working tree clean

原因

変更内容が存在しない状態です。

対処法

まず変更が反映されているか確認します。

git status
git diff

保存忘れや別ファイル編集もよくある原因です。


5. error: pathspec did not match any file(s) known to git

error: pathspec 'mainn' did not match any file(s)

原因

ブランチ名やファイル名の入力ミスです。

対処法

ブランチ一覧を確認します。

git branch

スペルミスを修正して再実行します。


6. 間違えてコミットした

これは実務でかなり多いです。

直前コミットを修正したい場合

git commit --amend

コミットだけ取り消す場合

git reset --soft HEAD~1

実務ポイント

push済みコミットは慎重に扱いましょう。


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

原因

mainやdevelopで直接作業してしまったケースです。

対処法

新しいブランチを作成して退避します。

git checkout -b feature/fix-work

その後、元ブランチの変更を戻します。

git checkout main
git restore .

まとめ

実務で特によくあるエラーは以下です。

  • failed to push some refs
  • merge conflict
  • not a git repository
  • nothing to commit

最初は焦りやすいですが、エラー文をしっかり読むことが一番大切です。

Gitエラーは慣れるほど対処が早くなるので、実務で何度も経験しながら覚えていきましょう。

36
14
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
36
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?