error: The following untracked working tree files would be overwritten by
checkout: means a file you never committed sits at a path that the branch you are
switching to tracks. Git will not replace your copy silently. Move it, stash it with
git stash push --include-untracked, or delete it if you do not need it, then
switch again.
The same message ends in by merge: when the other branch brings the file in
through git merge or git pull, and the fix is the same: get the
untracked copy out of the way first.
The error
PS> git status --short
?? config.json
PS> git checkout feature
error: The following untracked working tree files would be overwritten by checkout:
config.json
Please move or remove them before you switch branches.
Aborting
git switch feature printed exactly the same lines. The merge variant:
PS> git merge feature
error: The following untracked working tree files would be overwritten by merge:
config.json
Please move or remove them before you merge.
Updating e1ee528..29dcb5c
Aborting
Why it happens
Switching branches rewrites the working folder to match the target branch. Files that
the target tracks are written out; untracked files are normally left alone. The
conflict arises when both are true for one path: feature committed a
config.json, and on main you have your own
config.json that Git has never stored. Writing the branch's version would
destroy the only copy of yours, because Git has nothing to restore it from, so it aborts
before touching anything.
Typical sources: a config or .env file you created by hand that a teammate
later committed, or a build output that one branch accidentally added while the other
never tracked it.
The fix
PS> git stash push --include-untracked -m local-config
Saved working directory and index state On main: local-config
PS> git checkout feature
Switched to branch 'feature'
PS> git stash list
stash@{0}: On main: local-config
--include-untracked is what makes the stash take the file; a plain
git stash leaves untracked files where they are and the checkout fails
again. Your copy is now safe in the stash, and the branch's config.json is
in the folder. Bring yours back later, on the branch where it belongs, with a stash pop.
If the local file is something you do not need, deleting or renaming it is just as good;
what matters is that the path is free before Git writes the branch's version there.
How it was reproduced
Git for Windows 2.45.2 on Windows 11, Windows PowerShell 5.1, in a throwaway repository.
Branch feature committed config.json; on main a
different, untracked config.json was created. git checkout,
git switch and git merge each refused with exit code 1. After
the stash with --include-untracked, the same checkout succeeded.
Frequently asked
- How do I fix untracked working tree files would be overwritten by checkout?
- Move, rename or delete the listed files, or stash them with git stash push --include-untracked, then run the checkout again. Git refuses only to protect your uncommitted copy.
- Why does git stash not fix untracked files would be overwritten?
- A plain git stash saves only tracked changes. Add --include-untracked (or -u) so the untracked file goes into the stash too.
- What does untracked working tree files would be overwritten by merge mean?
- The branch you are merging or pulling adds a file at a path where you have an untracked file. Move or stash your copy, then merge or pull again.
More decoded errors in the Fixes category. When the blocking file is tracked and modified rather than untracked, see Your local changes would be overwritten by checkout.