error: Your local changes to the following files would be overwritten by checkout means you have uncommitted edits to a file that is different on the branch you are switching to. Git refuses rather than throw your edits away. Commit them, stash them, or discard them, and the switch goes through.

If you want to keep the edits but not commit them yet, git stash puts them aside, the checkout works, and git stash pop brings them back when you return. If the edits are worthless, git checkout -- notes.txt (or git restore notes.txt) discards them.

The error

> git checkout feature
error: Your local changes to the following files would be overwritten by checkout:
	notes.txt
Please commit your changes or stash them before you switch branches.
Aborting

Why it happens

Switching branches rewrites the files in your working folder so they match the commit the other branch points to. For a file that is identical on both branches Git has nothing to rewrite, so your uncommitted edits simply come along with you. For a file that differs between the branches, Git would have to replace its contents, and your edits exist only in the working folder, not in any commit. Overwriting them would lose work with no way back, so Git aborts the whole checkout and lists the files in the way.

That is why the error appears for some edits and not for others: it depends on whether the target branch changed the same file, not on how large your change is. Nothing has been modified when you see it; the branch, the index and your edits are exactly as they were.

The fix

> git stash
Saved working directory and index state WIP on main: 56dfc1f first
> git checkout feature
Switched to branch 'feature'
> git checkout 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:   notes.txt

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

git stash saved the edit and reset the file to the last commit, so the checkout had nothing to overwrite. After looking at the other branch, switching back and running git stash pop put the edit back as an unstaged change and dropped the stash entry. If the edit belongs on the other branch instead, pop it there; when that branch changed the same lines, expect a merge conflict to resolve, and the stash entry is kept until you do. If the edit is finished work, a plain git commit before switching is the simplest route of all.

How it was reproduced

Git for Windows 2.45.2 on Windows 11, run from Windows PowerShell. A test repository with one file, notes.txt, committed on main; a feature branch that added a second line to it; then, back on main, an uncommitted edit to the same file and git checkout feature, which printed the error. The stash sequence above then ran without errors.

Frequently asked

How do I switch branches without committing my changes?
Run git stash to save the uncommitted changes, switch with git checkout or git switch, and run git stash pop when you want the changes back.
Why can I switch branches with some uncommitted changes but not others?
Git only refuses when a changed file is also different on the target branch. Edits to files that are identical on both branches are carried over to the other branch unchanged.
How do I discard local changes before git checkout?
Run git checkout -- followed by the file name, or git restore with the file name. Both reset the file to the last commit and the edits cannot be recovered afterwards.

More decoded errors in the Fixes category. For more on stashing, see rebase, cherry-pick and stash in the Git series.