Please Commit Your Changes or Stash Them Before You Merge
When working with Git, few error messages are as commonly encountered — and as frustrating for beginners — as "please commit your changes or stash them before you merge." This message appears when you attempt to merge a branch into another while there are uncommitted modifications in your working directory. Understanding why this happens and how to resolve it is a fundamental skill for any developer working in a collaborative codebase. Whether you are a beginner just starting with version control or a seasoned developer looking to refine your workflow, this guide will walk you through everything you need to know about this Git merge conflict scenario.
No fluff here — just what actually works.
Understanding the Git Merge Process
Before diving into the error itself, it actually matters more than it seems. A git merge is an operation that combines the histories of two branches. When you run git merge feature-branch while on your main branch, Git attempts to take the independent lines of development from both branches and integrate them into a single unified branch.
For a merge to proceed smoothly, Git needs a clean and predictable starting point. Still, it compares the current state of your branch with the branch you are merging in, looking for a common ancestor commit. On the flip side, if your working directory is not in a clean state — meaning you have uncommitted changes — Git cannot safely determine which version of each file should serve as the baseline for the merge. From there, it calculates the differences and attempts to combine them automatically. This is where the error originates.
Why Git Refuses to Merge with Uncommitted Changes
The core reason behind the "please commit your changes or stash them before you merge" message is simple: Git wants to protect you from data loss. When you have modified files that have not been committed, those changes exist only in your working directory and have not been safely recorded in the repository's history Small thing, real impact. Turns out it matters..
You'll probably want to bookmark this section.
If Git were to proceed with a merge under these circumstances, several problematic scenarios could arise:
- Overwritten changes: The merge operation could silently overwrite your uncommitted modifications with the incoming changes from the other branch.
- Ambiguous merge results: Git would not know whether your uncommitted changes should be factored into the merge calculation, leading to unpredictable and potentially broken outcomes.
- Corrupted merge state: The repository could end up in a half-merged state that is difficult to recover from without manual intervention.
Git takes a cautious approach by halting the operation entirely and prompting you to resolve the situation first That's the part that actually makes a difference..
What Does "Commit Your Changes" Mean?
When Git suggests that you commit your changes, it is asking you to permanently record your current modifications in the repository's history before proceeding with the merge. A commit captures a snapshot of your entire working directory at a specific point in time, creating a stable reference point that Git can safely work with That alone is useful..
No fluff here — just what actually works.
To commit your changes, you follow a two-step process:
- Stage your changes using
git add. This tells Git which files you want to include in the next commit. - Create the commit using
git commitwith a descriptive message explaining what you changed.
Here is what the workflow looks like in practice:
git add .
git commit -m "Save current work before merging"
git merge feature-branch
This approach is ideal when your uncommitted changes are complete and ready to be saved. By committing first, you create a clean checkpoint that allows the merge to proceed without risk to your work.
What Does "Stash Your Changes" Mean?
If your changes are not yet ready to be committed — perhaps you are in the middle of an experimental feature or your code is still in a rough state — Git offers an elegant alternative: stashing. Stashing is the process of temporarily saving your uncommitted modifications to a special stack managed by Git, allowing you to return to a clean working directory without losing any of your work.
This is where a lot of people lose the thread.
Think of the stash as a clipboard for your entire project. You place your current work on the clipboard, perform the merge on a clean slate, and then retrieve your work afterward It's one of those things that adds up..
To use the stash workflow, follow these steps:
git stash
git merge feature-branch
git stash pop
The git stash command saves your changes and reverts your working directory to the last committed state. After the merge completes successfully, git stash pop restores your saved changes back into your working directory. If the merge introduces conflicts with your stashed changes, you will need to resolve those manually after popping the stash And that's really what it comes down to. Which is the point..
People argue about this. Here's where I land on it.
There are a few variations of the stash command worth knowing:
git stash save "message"— Creates a named stash entry for easier identification later.git stash list— Displays all stashed entries currently stored in your repository.git stash apply— Applies a stash without removing it from the stash stack, unlikepopwhich removes it.git stash drop— Removes a specific stash entry once you no longer need it.
Step-by-Step Solutions to Resolve the Error
Now that you understand the theory behind the error, let us look at practical solutions you can apply depending on your situation.
Solution 1: Commit and Merge
Use this approach when your changes are stable and ready to be saved permanently.
- Check the status of your working directory with
git statusto see which files are modified. - Review your changes carefully to ensure nothing accidental is included.
- Stage all relevant files with
git add. - Commit your work with a clear and descriptive commit message.
- Run the merge command again.
Solution 2: Stash and Merge
Use this approach when your changes are incomplete or experimental That's the part that actually makes a difference..
- Run
git stashto temporarily save your work. - Verify your working directory is clean by running
git status. - Perform the merge with
git merge feature-branch. - Once the merge is complete, restore your stashed changes with
git stash pop. - Resolve any conflicts that may arise between the merge and your stashed changes.
Solution 3: Discard Changes and Merge
In rare cases, your uncommitted changes may no longer be needed. If you are certain you want to discard them, you can reset your working directory before merging.
git checkout -- .
git merge feature-branch
Warning: This permanently deletes any uncommitted changes in your working directory. Use this option only when you are absolutely sure those changes are not needed.
Commit vs Stash: When to Use Which
Choosing between committing and stashing depends entirely on the state of your work and your intended next steps.
| Scenario | Recommended Action |
|---|---|
| Changes are complete and tested | Commit before merging |
| Changes are incomplete or experimental | Stash before merging |
| Changes are no longer needed | Discard and merge |
| You need a quick context switch | Stash, merge, then pop |
| You want to preserve a clean history | Commit before merging |
A common best practice in professional development workflows is to commit early and often. Small, focused commits
A common best practice in professional development workflows is to commit early and often. Small, focused commits not only make it easier to track progress and identify issues but also make sure your work is safely stored in the repository's history. When you encounter a merge situation, having these incremental commits means you're less likely to face the dilemma of uncommitted changes blocking your workflow. If you must work on something experimental, pair this habit with stashing—commit what's stable, then stash the rest. This hybrid approach keeps your main branch clean while allowing flexibility for exploratory work.
It sounds simple, but the gap is usually here.
Maintaining a clean working directory is equally crucial. That said, when a merge conflict does occur, a clean state allows you to focus solely on resolving the conflicting changes rather than managing a cluttered workspace. It minimizes the risk of accidental inclusion of incomplete code and simplifies debugging. What's more, in collaborative environments, a disciplined approach to committing and stashing ensures that your teammates aren't inadvertently affected by your local, uncommitted modifications.
Simply put, the error caused by uncommitted changes during a merge is not just a technical hurdle but an opportunity to refine your development habits. By strategically committing, stashing, or discarding changes based on their stability and relevance, you build a more efficient and reliable workflow. Remember, the goal is to integrate changes smoothly without losing progress or introducing instability. With these techniques, you can confidently figure out merge scenarios, keeping your project's history clear and your collaborations seamless Worth keeping that in mind..