/errors/git/local-changes-would-be-overwritten-by-checkout
error: Your local changes to the following files would be overwritten by checkout
- tool
- Git
- exit code
- 1
- versions
- Git 2.x
- os
- linux, macos, windows
error: Your local changes to the following files would be overwritten by checkout: means you have uncommitted edits in files that also differ between your current branch and the target branch. Switching would replace those files and lose the edits, so Git aborts. Commit or stash the changes first (git stash, git switch BRANCH, git stash pop). Uncommitted edits in files that are the same on both branches do not block the switch; Git carries them over.
Reproduce it
git init -q -b main repo && cd repo
echo "v1" > config.txt && git add config.txt && git commit -qm v1
git switch -q -c other && echo "other" > config.txt && git commit -qam other
git switch -q main
echo "my local edit" > config.txt
git checkout otherOutput · exit 1
error: Your local changes to the following files would be overwritten by checkout: config.txt Please commit your changes or stash them before you switch branches. Aborting
git switch other prints the same message.
Fix: stash, switch, pop
git init -q -b main repo && cd repo
printf 'port=80\nhost=a\nlog=off\n' > config.txt && git add . && git commit -qm v1
git switch -q -c other && sed -i 's/port=80/port=8080/' config.txt && git commit -qam other
git switch -q main
sed -i 's/log=off/log=on/' config.txt # local edit, not committed
git switch -q other 2>/dev/null || echo "switch refused"
git stash push -q
git switch -q other
git stash pop -q
git status --short
cat config.txtOutput
switch refused M config.txt port=8080 host=a log=on
other changed line 1 and the local edit changed line 3, so git stash pop merged them cleanly. If the stashed changes touch the same lines with the new branch, git stash pop reports a conflict and keeps the stash, so nothing is lost.
Choose an option
| Goal | Command |
|---|---|
| Keep the work on this branch | git commit -am "wip" |
| Park the work, switch, bring it back | git stash push; git switch B; git stash pop |
| Take the edits with you to B | git switch -m B |
| Include untracked files in the stash | git stash push -u |
| Discard the edits (cannot be undone) | git switch --discard-changes B |