Uncommitted Changes
Error message
error: Your local changes to the following files would be overwritten
Quick summary
Git stopped you because switching branches or pulling would overwrite changes you haven't committed yet.
Why this happens
You have modified files that haven't been committed
You tried to switch branches with a dirty working tree
A pull or checkout would overwrite your local changes
Minimal example
git checkout main # error: local changes would be overwrittengit stash
git checkout main # succeeds
git stash pop # restore changes latergit stash saves your uncommitted changes temporarily, allowing the branch switch. Restore them with git stash pop after switching.
How to diagnose
Run
git statusto see which files are modifiedDecide whether the changes should be kept or discarded
Check if the changes are staged or unstaged
How to fix
Commit the changes with
git addandgit commitStash them temporarily with
git stashand restore later withgit stash popDiscard the changes with
git checkout -- <file>if you don't need them
How to prevent
Commit or stash before switching branches or pulling
Run
git statusregularly to stay aware of uncommitted changes
Related Resources
Related Lessons
- git commit
Learn how to commit changes
- git status
Related Practice
- Git Status Command Quiz
Test your knowledge of git status for uncommitted changes.
- Spot the Git Commit Mistake
Practice spotting Git commit mistakes.