AI Hints
Home›Git & GitHub Tutorial›Common Errors
Git & GitHub · Lesson 102 of 144

Would Be Overwritten

Commit or stash first.

⏱ 2 min read▶ Video at 1:52:21🎓 Free
Watch this lesson in the Git & GitHub courseWatch on YouTube ↗Full Git & GitHub course

Here's an error everyone meets sooner or later. You try to switch branches, and Git refuses, saying your local changes would be overwritten. Git is protecting your unsaved work.

Would Be Overwritten
From the video: Would Be Overwritten
  • Git refuses to switch
  • Local changes would be overwritten
  • Git protects your work

The error

We have an uncommitted change to the home page. The dark branch has a different version of that same file. So git switch dark refuses. Your local changes to the following files would be overwritten. Please commit your changes or stash them before you switch branches.

git
git status --short
 M index.html
git switch dark
error: Your local changes to the following files would be overwritten by checkout:
        index.html
Please commit your changes or stash them before you switch branches.
Aborting

The fix

Git even tells us the fix. Commit, or stash. Our change isn't ready, so we stash it. Now the switch works. Later, back on main, git stash pop restores our change.

git
git stash
Saved working directory and index state WIP on main: f63a17a Link to about page
git switch dark
Switched to branch 'dark'
git switch main
Switched to branch 'main'
git stash pop
On branch main
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   index.html

no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (f06e36b203271bb69649b0d8e40f7f6f143dda0c)

Summary

If Git says your changes would be overwritten, commit or stash them first, then switch. Next, keeping big files out of your repository.

Key takeaways
  • 'would be overwritten'
  • Commit, or stash
  • Then switch
Next up: Big Files — and Git LFS