Git
Commit Changes
Stash Changes
Git Merge
Git Error Resolution

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.

Browse interview questions

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:

bash
git status
git diff
git diff --staged

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:

bash
git add .
git commit -m "WIP: save local changes before merge"
git merge main

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:

bash
git stash push -u -m "temp before merge"
git merge main
git stash pop

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:

bash
git stash apply

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:

bash
1git restore .
2git restore --staged .
3git clean -fd
4git merge main

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:

bash
# edit conflicted files
git add path/to/resolved-file
git commit

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:

bash
git switch -c backup/pre-merge-safety
git switch feature/payments

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 status and 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.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No 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 course
Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions