Skip to content
ohsudo, Home
CI/CD

Git "Your local changes would be overwritten by merge": keep them, or throw them away

Fix git "Your local changes to the following files would be overwritten by merge" or checkout: commit, stash, or discard them, plus untracked files and servers.

Beginner3 min readApplies to Git 2.30+ · GitHub · GitLab · Bitbucket

by OhSudo ·

Part of a learning path:Git for everyday work

error: Your local changes to the following files would be overwritten by merge:
config.yml
Please commit your changes or stash them before you merge.
Aborting

Git is protecting your work. You’ve changed config.yml without committing, and the commits you’re pulling change it too. Git won’t mix the two silently, so it stopped before changing anything. Your files are exactly as they were.

You have three choices: commit your changes, stash them for a moment, or discard them. Which one depends only on whether you want to keep them.

TL;DR

Keep your changes, and pull anyway:

Terminal window
git stash
git pull
git stash pop

Throw them away, which can’t be undone:

Terminal window
git restore config.yml
git pull

1. See what you changed

You can follow along in the terminal, where a pull has just been refused:

Terminal window
git status
git diff config.yml

Look before choosing. It may be a change you forgot, a debug line, or an edit a tool made on its own.

2. Keep them: commit

If the change is real work, commit it, then bring in the remote commits on top of yours:

Terminal window
git add config.yml
git commit -m "Update config"
git pull --rebase

Now both sides have commits, so a plain git pull would ask whether to merge or rebase: rejected (fetch first) explains the choice. If both sides changed the same lines, you get a conflict to resolve, which is normal. Then git push. And if the push is refused, see rejected (fetch first).

3. Keep them: stash

If it’s not ready to commit, set it aside, pull, and put it back:

Terminal window
git stash
git pull
git stash pop

git stash list shows what’s stashed. If pop reports a conflict, your changes are still in the stash: fix the conflict, then git stash drop.

Git can do this for you, in one step:

Terminal window
git pull --rebase --autostash

Or always, with git config --global rebase.autoStash true together with pull.rebase true.

4. Discard them

If you don’t need the changes:

Terminal window
git restore config.yml

Or every change in the working tree, which really can’t be undone:

Terminal window
git restore .

On Git older than 2.23, the same is git checkout -- config.yml. Unlike a stash, nothing is kept.

5. The same error with checkout

error: Your local changes to the following files would be overwritten by checkout:
config.yml
Please commit your changes or stash them before you switch branches.

Same choices. Or carry the changes over to the other branch, if they don’t conflict there, with git switch -m other-branch.

6. Untracked files

A slightly different message means the files aren’t in Git at all, and the incoming commits add files with the same names:

error: The following untracked working tree files would be overwritten by merge:
build/app.js
Please move or remove them before you merge.

Move them aside, or delete them if they’re generated. To see what git clean would remove, first run it with -n:

Terminal window
git clean -n
git clean -f build/

Generated files that keep causing this belong in .gitignore, and out of the repository. The .gitignore Generator has templates, and .gitignore not working explains removing files Git already tracks.

7. On a server

A deploy that runs git pull on a server, where someone edited a file by hand, fails like this on every deploy. Find out what changed with git diff, then move that change into the repository or into a config file Git doesn’t track. Don’t edit tracked files on servers.

For a deploy that should always match the remote exactly, discarding anything local:

Terminal window
git fetch origin
git reset --hard origin/main

Use that only where nobody works by hand. It throws away local commits too.

Warning

git reset --hard, git restore . and git clean -f delete work that Git can’t bring back, as it never stored it. When in doubt, git stash first: it costs nothing, and you can drop the stash later. To find a stash you dropped by mistake, git fsck --unreachable | grep commit can sometimes still recover it.

Shell, we have a problem!

Similar, but different: Updates were rejected because the remote contains work happens when pushing. refusing to merge unrelated histories is about two separate histories. detected dubious ownership is about who owns the folder. For everyday commands, see the Git Cheat Sheet.

$ ./practice.sh

Practice this in the terminal

Open the Terminal Playground with this article’s scenario loaded. It is safe and runs in your browser.

Open the terminal →

Found this useful? Share it.

Found a mistake? Let us know at contact@ohsudo.com.

Tools

Try it yourself

Articles

Related articles