Skip to content

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

✗ Broken code
git checkout main  # error: local changes would be overwritten
✓ Fixed code
git stash
git checkout main  # succeeds
git stash pop  # restore changes later

git stash saves your uncommitted changes temporarily, allowing the branch switch. Restore them with git stash pop after switching.

How to diagnose

  • Run git status to see which files are modified

  • Decide whether the changes should be kept or discarded

  • Check if the changes are staged or unstaged

How to fix

  • Commit the changes with git add and git commit

  • Stash them temporarily with git stash and restore later with git stash pop

  • Discard 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 status regularly to stay aware of uncommitted changes

Related Resources

Related Glossary

Related Lessons

Related Practice

← Back to language