Git: local changes would be overwritten by checkout
Git blocks a branch switch that would discard tracked edits. Stash, commit, or carry them over with the merge flag, and know when worktree is safer.

Introduction & Quick Summary
Git refuses to switch branches because the switch would destroy uncommitted work:
error: Your local changes to the following files would be overwritten by checkout:
src/config.ts
src/server.ts
Please commit your changes or stash them before you switch branches.
AbortingThe files listed have modifications in the working tree, and the branch you are switching to has different content for those same files. Git will not silently throw away edits, so it stops. The fix is to decide what should happen to those edits: keep them, commit them, or discard them deliberately.
Where it appears:
- Switching branches while a refactor is half done.
- Pulling or rebasing onto a branch that changed the same files.
- A script or CI step that checks out a commit with a dirty working tree.
- Switching between a release branch and
mainwhile a hotfix is in progress. - Line-ending or file-mode changes that make a clean tree look dirty.
Quick answer: git stash push -u, switch, then git stash pop. When the work is not finished but matters, commit it on a scratch branch instead. Never reach for checkout -f unless the edits are truly disposable.
Core Mechanics, Signatures & Reproducible Setup
git checkout <branch> (and git switch) updates the index and working tree to the target. Before writing, Git checks every path that differs between the current and target trees. If such a path also has local modifications, the write would lose them, and Git aborts with this message. Untracked files are not part of that check unless the target adds a file at the same path, which produces a different message.
# reproduce
git init repro && cd repro
git commit --allow-empty -m "root"
printf 'port=3000\n' > config.ts
git add config.ts && git commit -m "add config"
git switch -c feature
printf 'port=4000\n' > config.ts
git commit -am "feature port"
git switch main
printf 'port=3001\n' > config.ts # local edit on main
git switch feature # failserror: Your local changes to the following files would be overwritten by checkout:
config.ts
Please commit your changes or stash them before you switch branches.
AbortingInspect the state before choosing a fix:
git status --short # which paths are modified
git diff --stat # how large the changes are
git diff <target> -- <path> # how the target differs from the working copy
git stash list # existing stashes, in case one is still aroundThe messages that look similar and mean different things:
| Message | Meaning | Where the conflict is |
|---|---|---|
local changes ... overwritten by checkout | tracked files modified | working tree versus target tree |
local changes ... overwritten by merge | tracked files modified | working tree versus merge result |
untracked working tree files would be overwritten | new file exists at a path the target adds | untracked versus target |
Your branch is behind ... and can be fast-forwarded | not an error, just status | none |
cannot switch branch while merging | merge in progress | repository state |
Key Comparison Table
| Approach | Command | Keeps work | Use when |
|---|---|---|---|
| Stash | git stash push -u -m "wip" | yes, as a stash | quick switch, come back soon |
| Commit on a branch | git switch -c wip/x && git commit -am "wip" | yes, as a commit | work will continue later |
| Merge the switch | git checkout -m <branch> | yes, as conflict markers | both sides must be combined now |
| Separate worktree | git worktree add ../hotfix main | yes, untouched | work on two branches at once |
| Discard | git checkout -f <branch> | no | edits are known garbage |
Choosing between stash and commit:
| Situation | Better choice | Reason |
|---|---|---|
| Switch for a two-minute review | stash | no history noise |
| Hotfix that must be pushed now | worktree or commit on a branch | stash is local and easy to forget |
| Half-finished refactor, return tomorrow | commit on a WIP branch | survives a machine reset |
| Accidental edits from a build step | discard with restore | the files are generated |
Practical Edge Cases, Error Fixes & Deep Dive Code Examples
1. Stash without losing untracked files
A plain git stash keeps only tracked changes. New files stay in the working tree and can block the switch with the untracked variant of the error.
git stash push -u -m "wip: config port"
git switch feature
# ... work ...
git switch main
git stash pop-u includes untracked files; -a also includes ignored files, which is rarely what you want because it can stash node_modules. After pop, a conflict can occur if the branch changed those files; resolve it as a normal merge conflict, then git stash drop only after the tree is clean.
2. Commit the work on a scratch branch
If the change is worth keeping but not worth committing to the current branch, put it somewhere findable.
git switch -c wip/config-port
git commit -am "wip: config port change"
git switch main
git switch featureWhen you come back, git switch wip/config-port restores the exact state. Rebase or cherry-pick onto the real branch later, so the temporary commit disappears from history.
3. Combine both sides during the switch
git checkout -m (or git switch -m) performs a three-way merge between the current tree, the working changes, and the target, leaving conflict markers instead of aborting.
git checkout -m feature
git status
# both modified: config.ts
git diff config.tsThis is the right tool when the target's change and your local edit must coexist, such as a version bump on both branches. It is the wrong tool when you simply want to switch; it leaves the tree in a conflicted state that must be resolved before anything else.
4. Use a second worktree for urgent work
A worktree gives the branch its own directory, so the dirty tree never has to move.
git worktree add ../repo-hotfix main
cd ../repo-hotfix
# fix, commit, push
cd ../repo
git worktree remove ../repo-hotfixBoth directories share one object database and one set of refs, so commits made in the worktree are visible everywhere. This is the cleanest answer for a hotfix while a long refactor sits uncommitted.
5. Recover when the wrong command already ran
git checkout -f discards tracked changes in place, and they are not in the index. The content may still exist in the object database if it was staged at some point, or in the editor's local history.
git fsck --lost-found --unreachable | head
git stash list
git reflog --date=iso | head -20# if the file was staged before being discarded
git fsck --unreachable | grep blob
git show <blob-sha> > recovered.tsPrevention costs one command: stash before any destructive checkout, and compare the recovered file against the current one with Diff before overwriting anything.
Common Compilation/Runtime Mistakes & Version Gotchas
Using -f to make the error go away. Force checkout overwrites the files and the edits are gone. The error exists to prevent exactly that outcome; treat -f as a delete, not as a fix.
Assuming the listed files are the only problem. Git aborts on the first set it finds. After resolving those, the next switch attempt may name more files from the same category.
Confusing untracked files with local changes. The untracked variant has its own message and is fixed with git clean -n to review, then git clean -f or a move, not with stash unless you used -u.
Losing the stash. git stash pop removes the entry after applying. Use git stash apply when you want to keep the entry until the result is verified.
Line endings that create phantom changes. With core.autocrlf set differently on Windows and Linux, every text file can show as modified after a checkout, and the next switch fails for files you never edited. Check git diff --stat for a whole-repo pattern and normalize with a .gitattributes containing * text=auto.
Submodules and detached HEAD. In a detached HEAD, checkout <branch> can still fail on modified submodule pointers. git submodule update --init --recursive brings them to the recorded commit, and git status shows whether the pointer itself is modified.
Version differences. git switch and git restore (Git 2.23 and later) split the overloaded checkout command. The error text is the same, but the safer correspondence is: git restore <path> to discard one file, git switch <branch> to change branch, git checkout -m still for the merge behaviour.
A reliable order. git status, git stash push -u, git switch, git stash pop, and only then run tests. Log the stash message with the branch and date so a forgotten stash can be found with git stash list weeks later; a stash hash is easier to trace with Hash than a memory of which branch it came from.
Reference material: git-checkout and git-stash.
Official references
primary documentationThe Git documentation that defines the behaviour this guide explains.
Try matching browser tools
All toolsSmall utilities that fit Git work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Git guides
All Git guidesGit: fatal: not a git repository (or any parent): .git
The command ran outside a working tree or the .git metadata is gone. Confirm the root, restore a deleted .git, and repair worktree pointers.
Git: fatal: Need to specify how to reconcile divergent branches
Git 2.34 and later refuse to guess between merge, rebase and fast-forward. Set pull.rebase or pull.ff once, and know what each strategy does to history.
Git: fatal: unable to auto-detect email address
A commit needs an author identity. Containers and fresh installs have no user.email, so Git guesses from the OS user and refuses to continue.
Git: could not read Username, terminal prompts disabled
A headless git has no terminal to ask for credentials. Supply a token through a helper, a header or SSH instead of relying on a prompt.
More in Git
All Git guidesThe guides filed next to this one in Git, in library order.