MeWrite Docs

CONFLICT: Your local changes would be overwritten by stash apply

git stash applyでコンフリクトが発生した場合の対処法

概要

git stash apply または git stash pop を実行した際に、作業ディレクトリの変更とstashの内容がコンフリクトする場合に発生するエラーです。

エラーメッセージ

error: Your local changes to the following files would be overwritten by merge:
        path/to/file.js
Please commit your changes or stash them before you merge.
Aborting
CONFLICT (content): Merge conflict in path/to/file.js

原因

  1. 作業ディレクトリに変更がある - stashを適用しようとするファイルに未コミットの変更がある
  2. stash作成後にファイルが変更された - stash作成時と現在のファイル内容が異なる

解決策

1. 現在の変更を先にコミット

1
2
3
4
5
6
7
8
# 現在の変更をコミット
git add .
git commit -m "WIP: 作業中の変更"

# stashを適用
git stash pop

# コンフリクトがあれば解決

2. 現在の変更を別のstashに保存

1
2
3
4
5
6
7
# 現在の変更を新しいstashに保存
git stash push -m "current work"

# 古いstashを適用
git stash apply stash@{1}

# コンフリクトを解決後、両方の変更をマージ

3. コンフリクトを手動で解決

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
# stashを適用(コンフリクトが発生)
git stash apply

# コンフリクトマーカーを確認
git status

# ファイルを編集してコンフリクトを解決
# <<<<<<< Updated upstream
# 現在のブランチの内容
# =======
# stashの内容
# >>>>>>> Stashed changes

# 解決後、ステージング
git add path/to/file.js

# stashを削除(popの場合は自動削除されない)
git stash drop

4. –indexオプションなしで適用

1
2
3
4
5
# ステージング状態を無視して適用
git stash apply --index

# エラーになる場合、indexなしで試す
git stash apply

5. 特定のファイルのみ復元

1
2
3
4
5
# stashから特定のファイルだけ取り出す
git checkout stash@{0} -- path/to/file.js

# または
git show stash@{0}:path/to/file.js > path/to/file.js

stashの確認と管理

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# stash一覧を表示
git stash list

# stashの内容を確認
git stash show stash@{0}

# stashの詳細な差分を確認
git stash show -p stash@{0}

# 特定のファイルの差分を確認
git stash show -p stash@{0} -- path/to/file.js

よくあるパターン

ブランチ切り替え時のコンフリクト

1
2
3
4
5
6
# 作業中の変更がある状態でブランチを切り替えたい
git stash
git checkout other-branch
# 作業後、元のブランチに戻る
git checkout original-branch
git stash pop  # ここでコンフリクトの可能性

安全なstash適用

1
2
3
4
5
6
7
8
9
# --no-commitで適用(マージコミットを作らない)
git stash apply

# コンフリクトの確認
git diff --check

# 問題なければコミット
git add .
git commit -m "Apply stash"

stashをブランチに変換

1
2
3
4
5
6
7
# コンフリクトを避けるため、stashを新しいブランチとして作成
git stash branch new-branch stash@{0}

# これにより:
# 1. stash作成時点からブランチが作成される
# 2. stashが適用される
# 3. コンフリクトなく変更が復元される

stash popとstash applyの違い

1
2
3
4
5
6
7
8
# stash pop: 適用後にstashを削除(コンフリクト時は削除されない)
git stash pop

# stash apply: 適用後もstashを保持
git stash apply

# コンフリクト時は、popでもstashは削除されない
# 手動でgit stash dropが必要

予防策

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# stash前に変更を確認
git status

# 明確なメッセージ付きでstash
git stash push -m "feature: 機能Aの作業中"

# 変更のあるファイルを確認
git stash show

# 部分的にstash(対話モード)
git stash push -p

関連エラー

関連エラー

Git の他のエラー

最終更新: 2025-12-24