Gitのwould be overwritten by checkoutエラーの直し方
git switch や git checkout でブランチを切り替えようとして error: The following untracked working tree files would be overwritten by checkout: と出たら、切り替え先のブランチにあるファイルと同じ名前の、Git が管理していないファイルが手元にあります。Git は手元のファイルを上書きしないように、何も変えずに止まっています。手元のファイルの名前を変えるか、要らなければ消してから切り替え直すと、エラーは消えます。以下では、エラーを実際に起こしてから直すまでを、検証環境の出力そのままで載せます。
検証環境は Ubuntu 24.04、Git 2.43.0 です。main と feature の2つのブランチがあり、feature にだけ config.json と docs/guide.md がコミットされているリポジトリで試しました。Windows・macOS での動作、git pull で出る場合は試していません。
エラーの全文と、Git が止まる理由
main にいるときに、Git が管理していない config.json を作りました。git status --short で ?? と表示されるのが、管理していないファイルです。この状態で feature に切り替えると、次のエラーになりました。
$ echo "{\"port\": 3000}" > config.json; git status --short
?? config.json
$ git switch feature; echo "exit=$?"
error: The following untracked working tree files would be overwritten by checkout:
config.json
Please move or remove them before you switch branches.
Aborting
exit=1
エラーの意味は「以下の、Git が管理していないファイルは、チェックアウトすると上書きされてしまいます。ブランチを切り替える前に、移動するか削除してください。処理は中止しました」です。2行目から、タブで始まる行に、重なっているファイルの名前が並びます。

feature に切り替えるには、feature にコミットされている config.json を作業フォルダーに取り出す必要があります。そのまま取り出すと手元の config.json が消えてしまうので、Git は切り替えを中止します。エラーのあとに確かめると、ブランチは main のままで、手元の config.json の中身も変わっていませんでした。
$ git branch --show-current; git status --short; cat config.json
main
?? config.json
{"port": 3000}
git switch で切り替えても、メッセージは by checkout です。git checkout feature でも、一字一句同じメッセージでした。Git 2.43.0 のソース unpack-trees.c では、ブランチの切り替えでは checkout、マージでは merge の文面を出すようになっています。
重なっているファイルが複数あるときは、すべて並びます。フォルダーの中のファイルは、docs/guide.md のようにパスで表示されました。
$ git switch feature; echo "exit=$?"
error: The following untracked working tree files would be overwritten by checkout:
config.json
docs/guide.md
Please move or remove them before you switch branches.
Aborting
exit=1
手元の config.json を、feature にあるものと一字一句同じ中身にしても、同じエラーで止まりました。中身が同じかどうかではなく、同じ名前の管理していないファイルがあるかどうかで止まります。
手元のファイルが要るなら、名前を変えてから切り替える
手元のファイルを残したいときは、メッセージの「移動する(move)」のとおり、名前を変えてから切り替えます。
$ mv config.json config.mine.json; git switch feature; echo "exit=$?"
Switched to branch 'feature'
exit=0
$ git branch --show-current; git status --short; cat config.json; cat config.mine.json
feature
?? config.mine.json
{"port": 8080}
{"port": 3000}
エラーは出ずに feature に切り替わりました。config.json は feature のもの(8080)になり、手元の分は config.mine.json として、管理していないファイルのまま残っています。
要らないファイルなら、一覧を確かめてから消す
手元のファイルが要らないなら、消してから切り替えます。消す前に、Git が管理していないファイルが何かを git clean -n -d で一覧にしておくと、消してよいかを確かめられます。git clean の公式リファレンスにあるとおり、-n は実際には何も消さずに、消すものを表示するだけの指定です。-d は、管理していないフォルダーの中も対象にする指定です。
$ git clean -n -d
Would remove config.json
Would remove docs/
$ rm config.json docs/guide.md; git switch feature; echo "exit=$?"
Switched to branch 'feature'
exit=0
docs/ は、フォルダーごと管理していないので、docs/ とだけ表示されます。エラーに出たファイルを消すと、切り替えられました。
git stash -u で退避すると、戻すときにも止まる
git stash に -u を付けると、管理していないファイルも退避できます。git stash の公式リファレンスの -u の説明のとおり、退避したファイルは作業フォルダーから消えるので、切り替えは通ります。
$ git stash push -u -m "手元の config.json"; echo "exit=$?"
Saved working directory and index state On main: 手元の config.json
exit=0
$ git switch feature; echo "exit=$?"
Switched to branch 'feature'
exit=0
ただし、切り替えた先で git stash pop を実行すると、今度は feature の config.json とぶつかって、戻せませんでした。
$ git stash pop; echo "exit=$?"
Already up to date.
config.json already exists, no checkout
error: could not restore untracked files from stash
On branch feature
nothing to commit, working tree clean
The stash entry is kept in case you need it again.
exit=1
退避した分は消えずに残っていて(The stash entry is kept)、作業フォルダーの config.json は feature のままです。
$ git stash list; cat config.json
stash@{0}: On main: 手元の config.json
{"port": 8080}
main に戻ってから git stash pop すると、手元の config.json が戻りました。
$ git switch main; echo "exit=$?"
Switched to branch 'main'
exit=0
$ git stash pop; echo "exit=$?"
Already up to date.
On branch main
Untracked files:
(use "git add <file>..." to include in what will be committed)
config.json
nothing added to commit but untracked files present (use "git add" to track)
Dropped refs/stash@{0} (d502574635e5457b83f24c94635229b5785b8c86)
exit=0
$ git stash list; git status --short; cat config.json
?? config.json
{"port": 3000}
git stash -u は、少しのあいだ feature を見て、main に戻ってくるときに向いています。feature でも手元のファイルを使い続けたいなら、名前を変えるほうが手間がかかりません。
–force で切り替えると、手元のファイルは消える
git switch --force(-f)や git checkout -f でも、エラーは出ずに切り替わります。ただし、手元の config.json は feature のもので上書きされました。
$ git switch --force feature; echo "exit=$?"
Switched to branch 'feature'
exit=0
$ git branch --show-current; git status --short; cat config.json
feature
{"port": 8080}
$ git switch -q main; git status --short; ls -A
.git
README.md
main に戻ると、config.json は作業フォルダーからなくなりました。git checkout の公式リファレンスの -f の説明にも、邪魔になっている管理していないファイルを捨てるための指定だと書かれています。
--force や -f は、エラーに出たファイルを確認なしで上書きします。Git が管理していないファイルなので、コミットの履歴にも残っていません。中身が要らないと確かめてから使ってください。
.gitignore で無視しているファイルは、エラーにならずに上書きされる
手元のファイルを .gitignore で無視していると、このエラーは出ません。config.json を .gitignore に書いてから同じ手順で切り替えると、何も表示されずに切り替わり、手元の config.json は feature のもので上書きされました。
$ echo "config.json" > .gitignore; echo "{\"port\": 3000}" > config.json; git status --short --ignored
?? .gitignore
!! config.json
$ git switch feature; echo "exit=$?"
Switched to branch 'feature'
exit=0
$ git branch --show-current; cat config.json
feature
{"port": 8080}
git checkout の公式リファレンスの --overwrite-ignore の説明に、無視しているファイルを何も言わずに上書きするのが既定の動きで、--no-overwrite-ignore を付けると中止すると書かれています。付けて試すと、無視しているファイルでも同じエラーで止まり、手元の config.json は残りました。
$ git checkout --no-overwrite-ignore feature; echo "exit=$?"
error: The following untracked working tree files would be overwritten by checkout:
config.json
Please move or remove them before you switch branches.
Aborting
exit=1
検証環境の Git 2.43.0 では、git switch --no-overwrite-ignore feature も受け付けられ、同じエラーで止まりました。ただし、git switch の公式リファレンスには、このオプションの説明はありません。
git merge で出る、by merge のエラー
main で git merge feature を実行したときも、同じ状況では次のエラーになりました。by checkout が by merge に、最後の文が before you merge に変わります。
$ git merge feature
Updating a3b28c7..97c67df
error: The following untracked working tree files would be overwritten by merge:
config.json
Please move or remove them before you merge.
Aborting
このときもマージはされず、手元の config.json は残っていました。ブランチの切り替えのときと同じように、手元のファイルの名前を変えてからマージし直すと、エラーは出ずにマージできました。
$ mv config.json config.mine.json; git merge feature; echo "exit=$?"
Updating a3b28c7..97c67df
Fast-forward
config.json | 1 +
docs/guide.md | 1 +
2 files changed, 2 insertions(+)
create mode 100644 config.json
create mode 100644 docs/guide.md
exit=0
$ git log --oneline -1; git status --short; cat config.json; cat config.mine.json
97c67df config.json と docs/guide.md を追加
?? config.mine.json
{"port": 8080}
{"port": 3000}




