Effective Strategies to Resolve Merge Conflicts in Git for Developers

Effective Strategies to Resolve Merge Conflicts in Git for Developers
Effective Strategies to Resolve Merge Conflicts in Git for Developers

Learn how to resolve merge conflicts in Git with our comprehensive guide. Discover strategies using git merge, git rebase, and advanced tools to maintain a clean project history and improve your development workflow.

You pull the latest changes, run a merge, and Git stops cold: CONFLICT (content): Merge conflict in file.txt. Your first instinct might be to panic or force something through. Don’t. A merge conflict is just Git telling you it found two versions of the same lines and it won’t guess which one you meant. That decision is yours, and it’s a quick one once you know what you’re looking at.

Here we’ll walk through resolving conflicts with both git merge and git rebase, plus a few tools for the messier cases. Whether this is your first conflict or your five-hundredth, the goal is the same: read the markers, pick the right code, move on.

Table of Contents

Introduction to Merge Conflicts

A conflict happens when Git can’t automatically reconcile changes from two branches. The usual trigger is two people editing the same lines of the same file. It also shows up when one branch edits a file that another branch deleted, because now Git has no obvious winner.

Here’s a setup that leads to one:

Bash
# Create a new branch
git branch new-feature
# Switch to the new branch
git checkout new-feature
# Make some changes to a file and commit
echo "New changes" >> file.txt
git add file.txt
git commit -m "New feature changes"

If someone else edits that same file on another branch and you both merge into main, Git flags the overlap and hands the decision back to you.

How Merge Conflicts Occur

Most conflicts fall into one of two buckets.

1. Conflicting Changes in the Same File

Two people change the same line, and Git has no rule for choosing between them. That’s the classic conflict, and it needs a human to settle it.

2. Deleting a File That Has Been Modified

One branch deletes a file, another edits it. Git won’t assume you meant to throw the edits away, so it stops and asks. You decide whether the file stays or goes.

Basic Merge Conflict Resolution

When a conflict hits, Git pauses the merge and tells you. Start by seeing which files are affected:

Bash
# Check for merge conflicts
git status

Git lists the conflicted files under “Unmerged paths.” Each one has markers around the disputed lines.

Conflict Markers

Open a conflicted file and you’ll see something like this:

<<<<<<< HEAD
Changes from your current branch
=======
Changes from the branch being merged
>>>>>>> feature-branch

The block between <<<<<<< HEAD and ======= is your side. The block between ======= and >>>>>>> feature-branch is the incoming side. Your job is to end up with the code you actually want, then delete all three marker lines.

Resolving the Conflict

Edit the file by hand. Keep your side, keep their side, keep a blend of both, or scrap both and write something new. There’s no wrong tool here, just the wrong final code, so read both versions before you commit to one.

Once the markers are gone and the file reads the way you want, stage it and finish the merge:

Bash
# Stage the resolved file
git add file.txt
# Continue the merge process
git commit

That commit seals the merge. Newer Git versions also accept git merge --continue, which does the same thing but first checks that a merge is actually in progress.

Changed your mind partway through? git merge --abort unwinds the whole thing and puts you back where you started, before the merge. Commit or stash any loose work first, though, or Git may not be able to reconstruct it.

Using Git Rebase to Resolve Conflicts

Merging isn’t the only option. git rebase replays your commits on top of another branch instead of tying them together with a merge commit, which keeps history linear and easier to read.

How Rebasing Works

Rebase sets your commits aside, moves your branch to the tip of the other branch, then replays your commits one at a time on top. If a replay hits a conflict, Git stops on that commit and waits for you.

Resolving Conflicts During a Rebase

The rhythm is the same as a merge: fix the file, stage it, then tell Git to keep going.

Bash
# Start the rebase process
git rebase main
# If a conflict occurs, resolve the conflict in the file, then stage the changes
git add file.txt
# Continue the rebase process
git rebase --continue

Git applies the rest of your commits, pausing again if another one conflicts. If it gets out of hand, git rebase --abort drops you back to where you were before the rebase started.

Advanced Conflict Resolution Strategies

Some conflicts aren’t a simple this-side-or-that-side call. A few tools help with the harder ones.

1. Using Git’s Conflict Resolution Tools

Run git mergetool to open a visual three-way diff in an editor like VS Code, Meld, or vimdiff. Seeing your version, their version, and the common ancestor side by side often makes the right call obvious. If you’d rather stay in the terminal, git checkout --ours file.txt keeps your side wholesale and git checkout --theirs file.txt keeps theirs.

2. Cherry-Picking Specific Commits

When a full merge or rebase brings more conflict than it’s worth, git cherry-pick lets you grab just the commits you need.

Bash
# Cherry-pick a specific commit
git cherry-pick <commit-hash>

That pulls in only the changes from that one commit, so you sidestep the conflicts riding along with everything else.

Best Practices for Preventing Merge Conflicts

You can’t dodge conflicts entirely on a shared codebase, but you can make them rarer and smaller.

  • Talk to your team: Know who’s touching which files so two people aren’t rewriting the same function in parallel.
  • Use feature branches: Keep new work on its own branch so it doesn’t collide with main until you’re ready.
  • Pull frequently: Sync with the remote often. Small, regular updates conflict far less than one giant catch-up.
  • Resolve conflicts early: Handle them the moment they appear. A conflict you ignore only gets tangled up with the next one.
Conclusion

A merge conflict isn’t a failure, it’s Git refusing to guess on your behalf. Read the markers, decide what the code should be, and finish with a stage plus a commit or continue. Whether you reach for git merge or git rebase, the moves are the same, and they stop being intimidating fast.

Pull often, keep branches focused, and clear conflicts while they’re small. Do that and the ones that reach you stay quick to settle.

Next: Undo Local Git Commits Safely Using git reset and git revert

Leave a Comment

Your email address will not be published. Required fields are marked *


Scroll to Top