How do I resolve git saying "Commit your changes or stash them before you can merge"?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
That Git message is a safety warning, not an arbitrary failure. Git is telling you that your working tree contains local changes that could be overwritten by the merge, so you need to decide what should happen to those edits before the merge continues.
Inspect the Working Tree First
Before choosing a fix, check exactly what is modified:
The correct next step depends on whether the changes are:
- meaningful work you want to keep
- temporary work you are not ready to commit
- disposable edits you are happy to discard
Once that decision is clear, the Git workflow is usually straightforward.
Option 1: Commit the Changes
If the local work matters and belongs in history, commit it before merging:
This is often the cleanest solution because it turns the working-tree state into a normal commit that Git can merge properly.
If the commit is only temporary, you can later squash or clean it up with rebase.
Option 2: Stash the Changes
If you need to preserve the work but are not ready to commit it, stash it:
The -u flag includes untracked files. Without it, untracked files stay in the working tree and may still interfere later.
If you want a safer restore step, use:
instead of pop, then drop the stash manually only after you are sure everything is correct.
Option 3: Discard the Changes Intentionally
If the local changes are definitely disposable, remove them and then retry the merge:
This is destructive. Use it only when you are certain those changes are not needed.
If there is any doubt, stash first.
Expect Real Merge Conflicts Afterward
Resolving this warning only gets you past the working-tree protection step. The merge itself may still create actual branch-history conflicts.
Normal conflict-resolution flow:
If the conflict appears after git stash pop, the principle is the same: resolve the files, stage the result, and finish the integration.
Use a Safety Branch if the Situation Is Messy
If the working tree is complicated and you are not fully sure what should be preserved, create a backup branch first:
That gives you a recovery point if you later realize you made the wrong choice about stashing or discarding changes.
Build Better Habits So This Happens Less Often
This warning is normal, but seeing it constantly often points to workflow issues. Good habits include:
- making smaller commits more often
- syncing with the target branch regularly
- keeping experimental edits separate from merge-ready work
- avoiding long-lived branches full of unrelated local changes
Git is not being fussy here. It is protecting you from a very real risk of losing work.
Common Pitfalls
The biggest mistake is treating the warning as something to bypass rather than something to understand.
Another common issue is stashing without -u when untracked files are part of the work.
People also discard changes before checking git diff, which is one of the fastest ways to lose edits they actually meant to keep.
Finally, solving the warning does not guarantee a clean merge. It only clears the working-tree safety block so the real merge can happen.
Summary
- Git blocks the merge to protect local changes from being overwritten.
- Choose deliberately whether to commit, stash, or discard the current edits.
- Inspect
git statusand the diffs before making that choice. - Include untracked files in the stash when they matter.
- Treat the warning as a data-loss guardrail, not as something to ignore.
Related reading
- How do I resolve git saying Commit your changes or stash them before you can merge?
- How do I resolve merge conflicts in a Git repository?
- How do I revert a Git repository to a previous commit?
- How do I revert a merge commit that has already been pushed to remote?
- How do I resolve Please make sure that the file is accessible and that it is a valid assembly or COM component?
- How do I resolve the java.net.BindException Address already in use JVM_Bind error?
- How do I revert all local changes in Git managed project to previous state?
- How do I revert my changes to a git submodule?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseInterview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.