Table of Contents
Untracked files when switching branches?
So, you’re trying to switch branches with Git, and you run into this error:
error: The following untracked working tree files would be overwritten by checkout:
.example/example.json
Aborting
This situation typically arises in a few scenarios:
- New Files: You’ve created new files in your working directory that haven’t been added to the staging area with
git add
. - Branch Changes: You’re switching to a branch that has a file with the same name and path as your untracked file.
- Incomplete Merges: You might have untracked files lingering from a previous merge or checkout operation.
So, what untracked files is Git talking about?
Before going we’ll fix this, let’s check what Git means.
You can open Git Bash, PowerShell, or whatever IDE you’re using, most of the time you can run Git commands.
Run this:
git status
Untracked files:
(use "git add <file>..." to include in what will be committed)
.example/example.json
Let’s fix it
If you do not care about the files, you can manually remove them (in a UI) or remove them via Git.
git clean -f
And if it’s one file you need to remove:
git clean -f .example/example.json
Try to change branches again and see if that resolved it.
git checkout origin/example
If you don’t care about the untracked files and the above didn’t work, we can also force check out.
git checkout -f origin/example
If you want to keep the files you can stash them:
git add .example/example.json
git stash
Then run a checkout:
git checkout origin/example
If Git is blocking your checkout because of untracked files, it’s usually one of three things:
And bring back the files:
git stash pop
What to choose when?
- You don’t need the files → Delete them and try again.
- You just want to switch branches → Force checkout with
-f
. - You want to keep the files → Stash them before switching.