TechEarl

Git: 'Your Local Changes Would Be Overwritten by Merge' - How to Fix It

Git stops a pull when uncommitted edits would be clobbered. Fix it by committing your work, or by stashing it, pulling, then popping the stash back. Includes the full git stash workflow: pop vs apply, untracked files, partial stashes.

Ishan KarunaratnePublished ⏱️ 11 min readUpdated
Share thisCopied
How to fix the Git error that your local changes would be overwritten by merge when you run git pull

"Your local changes would be overwritten by merge" means you have uncommitted edits to a file, and the git pull you just ran would have to overwrite them to bring in the remote version. Git refuses rather than silently destroy your work. The fix is to get those edits out of the way first: either commit them, or stash them, pull, then restore them. Nothing is lost; Git stopped exactly so nothing would be. Further down I also cover the full git stash workflow, since it is the tool behind the second fix.

Here is the message in full:

text
error: Your local changes to the following files would be overwritten by merge:
        src/app.js
Please commit your changes or stash them before you merge.
Aborting

What the error actually means

When you run git pull, Git fetches the new commits from the remote and then merges them into your current branch (git pull does a merge under the hood, and you can choose whether that step merges or rebases). To merge, it needs to update the files in your working directory to match the incoming changes.

The problem: you have edited src/app.js but have not committed that edit. The remote also changed src/app.js. If Git went ahead with the merge, it would have to write the remote version over the top of your uncommitted edit, and your edit would be gone with no commit to recover it from. Git will not do that. It aborts the pull and leaves everything exactly as it was, so you can decide what to do.

The key word is uncommitted. Committed work is safe; Git can merge committed changes and tell you about any conflicts. It is the work sitting in your working directory (or staged in the staging area but not yet committed) that Git cannot reconcile automatically, so it stops.

Fix 1: commit your changes, then pull

If your edits are finished, or finished enough to record, the cleanest fix is to commit them. Once they are a real commit, Git can merge the remote work alongside yours.

bash
git add src/app.js
git commit -m "Adjust app startup logging"
git pull

If your first pull then asks you to reconcile divergent branches, that is a separate prompt about how to combine your new commit with the remote ones; I cover it in if your first pull then asks you to reconcile divergent branches.

After the pull, one of two things happens:

  • The changes do not overlap, and Git merges them cleanly.
  • Both sides changed the same lines, and you get a merge conflict. That is normal and recoverable. Open the file, pick the right lines, and finish the merge. I cover the whole process in how to resolve merge conflicts in Git.

Use this fix when the work is in a committable state. Do not commit half-broken code just to clear the error; reach for the stash instead. If you already did commit prematurely, you can undo that commit before pulling and keep the edits in your working tree.

Fix 2: stash your changes, pull, then pop them back

If your edits are mid-flight and you are not ready to commit, git stash is the right tool. It tucks your uncommitted changes away on a stack, leaving you a clean working directory, lets the pull go through, and then puts your changes back on top.

bash
git stash
git pull
git stash pop

git stash saves both staged and unstaged changes and reverts your working tree to match the last commit. The pull now has nothing to overwrite, so it succeeds. git stash pop reapplies your saved changes on top of the freshly pulled code and drops them from the stash.

If the pulled code touched the same lines you had stashed, git stash pop can raise its own conflict. Same drill as a merge conflict: edit the file, resolve, then drop the stash by hand (a conflicted pop keeps the entry). The rest of the stash toolkit (pop vs apply, untracked files, partial stashes) is in how to use git stash below.

Which fix should I use?

SituationUse
Edits are finished and worth recordingCommit, then pull
Edits are mid-flight, not ready to commitStash, pull, pop
Edits are throwaway and you do not want themDiscard them (see below), then pull
You want the work on a separate line of historyCommit to a new branch, then pull

Commit when the work is a real unit of history. Stash when you just need a clean tree for a moment and want the same edits back afterward. They are not competing tools; they solve slightly different versions of the same "clear the working tree" problem.

Fix 3: you actually want to throw the edits away

Sometimes the uncommitted changes are junk: a debug console.log, a config tweak you no longer want, an accidental edit. If you are certain you do not need them, discard them and the error disappears because there is nothing left to overwrite.

bash
git checkout -- src/app.js
git pull

On Git 2.23 and newer you can use the clearer git restore:

bash
git restore src/app.js
git pull

This is destructive: the uncommitted edit is gone for good, with no stash to recover it from. Be sure before you run it. If you want the full set of discard options, including resetting the whole working tree, see how to discard local changes in Git.

Why does Git stop instead of merging anyway?

This is Git protecting you. Committed history is recoverable; you can almost always get a committed state back, even after a bad reset, using the reflog. Uncommitted working-directory edits have no such safety net. They exist only as the current bytes on disk. If Git overwrote them during a merge, there would be no record of them anywhere, and no command could bring them back.

So Git draws a hard line: it will never clobber uncommitted changes to satisfy a merge. The abort is the feature, not the failure. Once you understand that, the error stops being alarming and becomes a simple prompt: deal with the working tree first, then pull.

How to use git stash

git stash takes the changes in your working tree, tucks them away on a stack, and gives you back a clean working directory matching the last commit. git stash pop brings them back. It is the right tool whenever you need a clean tree for a moment without committing, which is exactly what this error asks for.

Shelving and restoring work with git stash and git stash pop
git stash shelves your changes; git stash pop brings them back.

By default git stash saves changes to tracked files (staged or not) and your staged changes, then resets the working tree to HEAD. It does not save untracked files or files matched by your .gitignore rules, so a brand-new file still sitting there after a stash is expected.

git stash pop vs apply

CommandReapplies changesKeeps the stash entryUse when
git stash popYesNo (removes it from the stack)The normal case: you want your work back and you are done with the stash
git stash applyYesYes (entry stays on the stack)You want to apply the same stash to more than one branch, or keep a safety copy until you confirm it applied cleanly
bash
git stash apply     # reapply the latest stash, but leave it on the stack
git stash drop       # then remove it manually when you are happy

If a pop causes a merge conflict, Git does not drop the stash entry, so you do not lose the work while resolving it. Resolve the conflict, then drop the stash by hand.

Name, list, inspect, and drop stashes

A bare git stash records a generic message like WIP on main. Give it a label instead. git stash save "msg" is the older form; Git 2.13 (May 2017) added git stash push, which deprecates save, so on anything modern use:

bash
git stash push -m "half-finished login form"

Stashes live on a stack, newest first:

bash
git stash list
text
stash@{0}: On main: half-finished login form
stash@{1}: WIP on feature/api: 9c3f1a2 add request logging

Inspect, apply, or remove a specific entry by name:

bash
git stash show stash@{1}          # summary of changed files
git stash show -p stash@{1}        # the full diff
git stash pop stash@{1}
git stash drop stash@{1}     # remove a single entry
git stash clear               # remove every stash (no undo command, be careful)

git stash clear does not warn you and there is no undo command for it, so list and inspect first. Recovery after a clear is possible only until garbage collection prunes the orphaned commits (see recover a dropped stash below), so plan as if it were permanent.

Stash untracked files, or only some changes

To include untracked files, add -u (--include-untracked). To also include ignored files, use -a (--all), which I rarely want because ignored files are usually build output or local config:

bash
git stash -u        # stash tracked changes AND untracked files
git stash -a        # stash everything, including ignored files

To stash only part of your work, -p (--patch) walks you through each hunk and asks whether to stash it, using the same interactive interface as git add -p:

bash
git stash -p
text
y - stash this hunk
n - do not stash this hunk
s - split this hunk into smaller pieces
q - quit, stash nothing more
? - show the full list of options

Stash to switch branches

The same overwrite protection applies to switching branches: Git will not check out another branch if that would overwrite your uncommitted edits. When an urgent fix comes in mid-change, stash, switch, fix, switch back, pop:

bash
git stash                       # set your half-done work aside
git checkout main               # move to the branch with the bug
# ... fix the urgent bug, commit it, push ...
git checkout feature/login      # back to where you were
git stash pop                   # your half-done work returns

Git 2.23 (August 2019) added git switch as a dedicated command for changing branches (git switch main); it does the same thing as git checkout <branch> here.

Two honest caveats. Stash is for short detours, not long-term storage: if work is worth keeping for any length of time, make a throwaway branch and a real commit instead. And a stash is local only; it never gets pushed, so it is not a backup.

Recover a dropped stash

If you git stash drop or git stash clear and then realize you needed that work, it is usually not gone yet. Stashes are commits under the hood, and unreachable commits hang around until garbage collection. Find the dangling stash commit and apply it:

bash
git fsck --no-reflogs | grep commit     # find dangling commits
git stash apply <commit-hash>            # reapply the one you recognise

This is a fallback, not a habit. Stash only ever touches the working tree and the index, never commit history, so it is the wrong tool for undoing a commit; for that, see discarding and undoing changes in Git.

Avoiding the error in the first place

A few habits keep this from showing up:

  • Commit or stash before you pull. Make a clean working tree your default state before syncing with the remote. The cleaner your tree, the less friction every pull has.
  • Understand what a pull does. git pull is a fetch plus a merge in one step. If you fetch first and inspect the incoming changes, you keep more control over when the merge happens. I lay out the distinction in git pull vs git fetch.
  • Keep generated and local-only files out of the repo. Build artifacts, editor settings, and local config that you keep editing are a constant source of "would be overwritten" friction. A good .gitignore stops them from being tracked at all.
  • Commit in small, frequent units. The longer uncommitted work sits in your tree, the more likely a remote change collides with it. Small commits keep the window narrow.

New to Git and want the foundations behind all of this (the working tree, the staging area, commits, and remotes)? Start with Git for beginners, and read the staging area explained for what "committed" versus "uncommitted" really means.

FAQ

No. The error means Git aborted the pull and left your working directory exactly as it was. Nothing has been merged and nothing has been overwritten. Your uncommitted edits are still on disk, untouched. You only risk losing them if you choose to discard them yourself with a command like git checkout or git restore on that file.

Commit if your edits are finished and worth recording as a unit of history. Stash if your work is mid-flight and you are not ready to commit, but you still want the same edits back after the pull completes. Both clear the working tree so the pull can proceed; they differ only in whether the edits become a permanent commit or sit temporarily on the stash stack.

It reapplies the changes you stashed earlier on top of the newly pulled code, then removes them from the stash stack. If the pulled code changed the same lines you had stashed, the pop can raise a conflict, which you resolve the same way you would resolve any merge conflict: edit the file, keep the lines you want, and save.

Both reapply your stashed changes to the working tree. Pop removes the stash entry from the stack afterward, while apply leaves it there. Use pop for the normal case, and apply when you want to keep the stash, for example to apply it to more than one branch, then drop it manually.

By default git stash only saves changes to files Git already tracks, plus staged changes. Brand-new untracked files stay in your working tree. Add the -u flag to include them.

No. Stashes are entirely local and never travel to a remote, so they are not a backup. If you would be upset to lose the work, commit it to a branch instead.

Yes, but only when you are certain you do not want the local edits. Discard them first with git restore on the affected file, or reset the whole working tree, then run git pull. There is no flag that safely merges over uncommitted work, because Git has no way to recover edits it overwrites. Discarding deliberately is the supported path.

Sources

Authoritative references this article was fact-checked against.

Tagsyour local changes would be overwritten by mergeGitVersion Controlgit pullgit stashgit stash popgit stash apply

Found this useful? Pass it on.

Copied

Ishan Karunaratne

Systems and Network Architect · Chief Technology Officer

Systems and network architect and Chief Technology Officer with more than two decades designing, building, and running production software, cloud and network architecture, Linux systems, and the bare metal underneath them, and lately working AI into the stack. A US Army veteran who served in Operation Iraqi Freedom. What I write here is drawn from the full arc of that work, across architecture, engineering, and operations, not any single job.

Keep reading

Related posts