Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

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.

~5 min readGit
#git#checkout#stash#conflicts
Bold white headline reading CHECKOUT BLOCKED with two Git branch crates colliding at an amber edit gate.

Introduction & Quick Summary

Git refuses to switch branches because the switch would destroy uncommitted work:

code
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. Aborting

The 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 main while 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.

bash
# 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 # fails
code
error: 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. Aborting

Inspect the state before choosing a fix:

bash
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 around

The messages that look similar and mean different things:

MessageMeaningWhere the conflict is
local changes ... overwritten by checkouttracked files modifiedworking tree versus target tree
local changes ... overwritten by mergetracked files modifiedworking tree versus merge result
untracked working tree files would be overwrittennew file exists at a path the target addsuntracked versus target
Your branch is behind ... and can be fast-forwardednot an error, just statusnone
cannot switch branch while mergingmerge in progressrepository state

Key Comparison Table

ApproachCommandKeeps workUse when
Stashgit stash push -u -m "wip"yes, as a stashquick switch, come back soon
Commit on a branchgit switch -c wip/x && git commit -am "wip"yes, as a commitwork will continue later
Merge the switchgit checkout -m <branch>yes, as conflict markersboth sides must be combined now
Separate worktreegit worktree add ../hotfix mainyes, untouchedwork on two branches at once
Discardgit checkout -f <branch>noedits are known garbage

Choosing between stash and commit:

SituationBetter choiceReason
Switch for a two-minute reviewstashno history noise
Hotfix that must be pushed nowworktree or commit on a branchstash is local and easy to forget
Half-finished refactor, return tomorrowcommit on a WIP branchsurvives a machine reset
Accidental edits from a build stepdiscard with restorethe 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.

bash
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.

bash
git switch -c wip/config-port git commit -am "wip: config port change" git switch main git switch feature

When 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.

bash
git checkout -m feature git status # both modified: config.ts git diff config.ts

This 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.

bash
git worktree add ../repo-hotfix main cd ../repo-hotfix # fix, commit, push cd ../repo git worktree remove ../repo-hotfix

Both 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.

bash
git fsck --lost-found --unreachable | head git stash list git reflog --date=iso | head -20
bash
# if the file was staged before being discarded git fsck --unreachable | grep blob git show <blob-sha> > recovered.ts

Prevention 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 documentation

The Git documentation that defines the behaviour this guide explains.

Try matching browser tools

All tools

Small utilities that fit Git work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Git guides

More in Git

All Git guides

The guides filed next to this one in Git, in library order.