git pull が「Your local changes would be overwritten」でできない
未コミットのローカル変更が pull で取り込む差分と同じファイルにあると、git は上書きを避けて中断する。
変更を commit するか git stash で退避してから pull するのが安全。
公開:
要約
error: Your local changes to the following files would be overwritten by merge は、未コミットのローカル変更と、pull で取り込む変更が同じファイルを触っている ために git が安全装置として merge を止めた状態。
ローカルの変更を commit するか git stash で退避してから git pull すれば進む。
捨ててよい変更なら git restore で消す。
よくある原因
- 編集中ファイルの衝突: 自分が直しているファイルを、リモート側も更新していた
- 追跡対象の誤り: ビルド生成物や
.envをコミット対象にしてしまい、毎回差分が出る - 改行・権限差分: CRLF/LF や実行ビットの違いで、実質変更していないのに変更扱いになる
- 履歴の書き換え: 共有ブランチへの force push で差分の範囲が想定より広がっている
解決策
1. 変更を commit してから pull
git add -A
git commit -m "wip"
git pull残したい変更はこれが最も素直。
pull 後にコンフリクトが出たら通常の merge 解消に移る。
2. stash で退避する
git stash push -m "before pull"
git pull
git stash popコミットするほど固まっていない作業は stash で逃がす。git pull の挙動は git-pull 公式ドキュメント に、退避の詳細は git-stash ドキュメント に記載がある。pop で衝突したら解消後に git stash drop する。
3. 変更を破棄する
git restore path/to/file # 特定ファイルだけ捨てる
git restore . # 全部捨てる(取り返せないので注意)
git pull不要な変更だと確信できる場合のみ。restore は元に戻せないため、対象を限定して打つ。
4. そもそも追跡から外す
echo "dist/" >> .gitignore
git rm -r --cached dist
git commit -m "stop tracking build output"生成物や個人設定が原因なら、追跡をやめれば再発しない。
これが根本対策になることが多い。
実行例
alpine/git 環境(git 2.54.0)で上記手順を実際に動かすと、f.txt の先頭行をローカルで未コミット編集した状態で git pull を実行した時点で error: Your local changes to the following files would be overwritten by merge が発生し終了コード 1 で中断、その後 git stash で退避してから pull・stash pop を順に実行したところ、末尾行の上流変更と先頭行のローカル変更は別箇所のため衝突なく適用され、終了コード 0 で完了した。
== versions ==
git version 2.54.0
== run ==
--- セットアップ: bare リモート + 初期コミット ---
warning: You appear to have cloned an empty repository.
--- ローカルを clone(v1 を取得)---
--- 上流が f.txt の末尾行を更新して push ---
--- ローカルは同じ f.txt の先頭行を未コミットで編集 → git pull(overwritten を想定)---
From /tmp/tmp.cFCoem/remote
* branch main -> FETCH_HEAD
542de30..e6df4ba main -> origin/main
Updating 542de30..e6df4ba
error: Your local changes to the following files would be overwritten by merge:
f.txt
Please commit your changes or stash them before you merge.
Aborting
pull の終了コード: 1
--- 解決: git stash で退避 → pull → stash pop ---
Saved working directory and index state On main: before pull
stash の終了コード: 0
From /tmp/tmp.cFCoem/remote
* branch main -> FETCH_HEAD
Updating 542de30..e6df4ba
Fast-forward
f.txt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
pull の終了コード: 0
Auto-merging f.txt
On branch main
Your branch is up to date with 'origin/main'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: f.txt
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (ffaa0de1ddb0f6da2772a48f4a7432d9a8beb13c)
stash pop の終了コード: 0(別の行を編集しているため衝突なく適用される)
--- 結果 ---
M f.txt
--- f.txt ---
HEADER-LOCAL
a
b
c
d
FOOTER-UPSTREAM— 2026-06-18 時点の出力