git · Git
The message
error: Your local changes to the following files would be overwritten by checkout:
What it means
You are trying to move to another branch, but files whose content differs there still hold edits you have not committed. It appears when you switch or checkout with work half done. git stash push -u, or a throwaway commit, gets you across and lets you come back to it. The git checkout --force and git switch --discard-changes you will find in search results do move you, but they delete those uncommitted edits permanently — nothing records them, not even the reflog, so there is no way back.
The fix
git stash push -u- Printed by
- git
- Git
- 29
Almost every git error is a refusal that means "doing that from this state would lose something"; fatal: means git stopped without changing anything, and the hint: lines under the first line usually carry the actual remedy.
Reading an error message
- Read from the first line down. The lower you go the more it is about the tool’s internals; the cause is usually at the top.
- If there is a file and a line number, start there — not the top stack frame, but the topmost line that names a file you wrote.
- Search the message verbatim, but strip your own paths and variable names first; those are what stop the search from matching.
- The same condition is worded differently across tool versions. If results look wrong, add the version number to the query.
- Before pasting a fix, check what it throws away. Some of these cannot be undone.
Common questions
Q. What does “error: Your local changes to the following files would be overwritten by checkout:” mean?
You are trying to move to another branch, but files whose content differs there still hold edits you have not committed. It appears when you switch or checkout with work half done. git stash push -u, or a throwaway commit, gets you across and lets you come back to it. The git checkout --force and git switch --discard-changes you will find in search results do move you, but they delete those uncommitted edits permanently — nothing records them, not even the reflog, so there is no way back.
Q. How do I fix it?
git stash push -u — before running it, check the explanation above for what this command discards.
Q. Which tool prints this?
git. It sits under Git, and the message runs to 13 words.