🐧 How to Fix “Local Changes Would Be Overwritten by Merge” Error in Git

🐧 How to Fix “Local Changes Would Be Overwritten by Merge” Error in Git

Have you ever tried to run git pull origin main and been met with this dreaded message?

error: Your local changes to the following files would be overwritten by merge:
resources/views/frontend/master.blade.php
Please commit your changes or stash them before you merge.
Aborting

This error often happens when you're working on a file locally while the same file was also updated in the remote repository. Git doesn't know how to automatically merge those changes unless you clean things up first.

In this article, I’ll show you how to fix this issue without losing your work, using Git’s powerful stash command.


🔍 The Problem: Merge Conflict with Local Changes

Here’s the situation:

  • You made local changes to your project files.

  • Someone else also made changes and pushed them to the remote repository.

  • You try to pull the latest version with git pull origin main, but Git blocks you.

Why? Because if Git merges automatically, it could overwrite your local edits. To avoid that, it forces you to decide what to do first.


🧰 The Solution: Git Stash to the Rescue

Git stash is like a clipboard that temporarily holds your changes so you can safely pull and then reapply them.

🪄 Step-by-Step Fix

✅ 1. Stash Your Current Changes

git stash

This will temporarily save (stash) all your local uncommitted changes.

Sample output:

Saved working directory and index state WIP on main: 334cda6 Merge pull request #13

✅ 2. Pull the Latest Changes from Remote

git pull origin main

Now that your changes are out of the way, Git can update your working directory without conflict.

Sample output:

Fast-forward
resources/views/frontend/master.blade.php | 13 ++++++++-----

✅ 3. Reapply Your Stashed Changes

git stash pop

This will bring back your local changes and apply them on top of the updated project. Git will attempt to merge them automatically. If changes overlap, it may show merge conflicts for you to resolve manually.

Sample output:

Auto-merging resources/views/frontend/master.blade.php

🎯 Optional: Committing or Cleaning Up

After this, your Git status might look like:

modified:   resources/views/frontend/master.blade.php
modified: resources/views/frontend/post/index.blade.php

Now choose one of the following:

➕ Option A: Commit Your Local Changes

If you’re happy with the result:

git add .
git commit -m "Updated views after pulling from main"

Done. You’re clean again!


🧹 Option B: Discard Local Changes

If you don’t want those changes anymore:

git restore .

To discard untracked files as well (be careful):

git clean -fd

Use git clean -fdn to preview what will be deleted.


📝 Summary

StepCommandPurpose
1️⃣git stashSave your local changes
2️⃣git pull origin mainPull latest updates from remote
3️⃣git stash popReapply your changes after pulling
git add . && git commit(Optional) Commit changes
git restore .(Optional) Discard changes

🧠 Pro Tip: Use Branches

To avoid these headaches in the future, create a separate branch for your feature development:

git checkout -b feature/my-update

This way you can freely work and merge changes back when you're ready.


🦆 Final Thoughts

If you're managing projects where multiple files change often example in master.blade.php or blog post templates getting comfortable with Git stash and pull is essential.

You’re now ready to handle Git merge conflicts like a pro—without losing any work. 💪

Isaac Talb

Startup Co-Founder | Software Engineer | Football Player

Leave a Reply

Your email adress will not be published, Requied fileds are marked*.