Merge Conflicts: What They Are and How to Deal with Them
This article continues our "Advanced Git" series. Follow Tower on Twitter or subscribe to their newsletter for updates on future articles.
Merge conflicts: a common frustration for Git users, especially those collaborating. However, they're often less daunting than they seem. This installment explains their causes, nature, and resolution.
Advanced Git Series:
- Part 1: Mastering the Git Commit
- Part 2: Effective Git Branching Strategies
- Part 3: Streamlining Collaboration with Pull Requests
- Part 4: Tackling Merge Conflicts (You're here!)
- Part 5: Rebase vs. Merge: A Comparative Analysis
- Part 6: The Power of Interactive Rebase
- Part 7: Cherry-Picking Commits: A Selective Approach
- Part 8: Recovering Lost Commits with the Reflog
Understanding Merge Conflicts: Causes and Occurrence
Merge conflicts arise when integrating changes from different sources into your current branch. This isn't limited to branch merging; rebasing, cherry-picking, git pull
, or even stash reapplication can all trigger conflicts. While not every integration leads to a conflict, conflicts occur when contradictory changes exist.
Git's merging prowess is a key strength. It automatically handles most integrations. However, when changes conflict—for instance, the same code line modified differently in two commits or a file modified in one branch and deleted in another—Git requires human intervention to resolve the ambiguity.
Recognizing Merge Conflicts
Git clearly signals merge conflicts. A failed merge or rebase will be immediately reported in the terminal:
<code>$ git merge develop CONFLICT (content): Merge conflict in index.html Automatic merge failed; fix conflicts and then commit the result.</code>
Even if you miss this message, git status
will highlight the conflict. Git GUIs like Tower provide visual cues to ensure you don't overlook conflicts. Rest assured, Git makes it difficult to miss a merge conflict.
Undoing a Merge Conflict
Ignoring a merge conflict isn't an option; it must be addressed. You have two choices: resolve the conflict or undo the action that caused it.
Undoing is often straightforward using the --abort
parameter (e.g., git merge --abort
, git rebase --abort
). This reverses the merge/rebase, restoring the pre-conflict state. This works even if you've begun resolving files; you can always abort and restart.
The Anatomy of a Merge Conflict
Let's examine a conflicted index.html
file:
Git marks conflicting sections with . The content after this marker is from your current branch (HEAD). <code>=======
separates the conflicting changes, followed by the changes from the other branch (e.g., develop
), marked by .
Your task is to edit the file, using a text editor, IDE, Git GUI, or merge tool, to resolve the conflict.
Resolving Merge Conflicts
The resolution method—text editor, IDE, GUI, or merge tool—doesn't matter; the final file must reflect your desired state. Simple conflicts might involve discarding a change. More complex conflicts may require collaboration to decide which change to keep or how to combine them.
While manual editing is possible, dedicated tools often streamline the process. Git GUIs offer visual conflict resolution aids. Merge tools provide advanced diff viewing and comparison features (side-by-side, combined views, etc.). Configure your preferred tool using git config
and invoke it with git mergetool
.
After resolving the conflict and staging the changes (git add <filename></filename>
), commit the changes as usual.
Don't Panic!
Merge conflicts are manageable. Understanding the cause allows you to either undo or resolve the conflict. Even mistakes are reversible; simply revert to the pre-conflict commit and start again.
For a deeper dive into advanced Git, explore the free "Advanced Git Kit" with videos on branching, interactive rebase, reflog, submodules, and more.
Advanced Git Series:
- Part 1: Mastering the Git Commit
- Part 2: Effective Git Branching Strategies
- Part 3: Streamlining Collaboration with Pull Requests
- Part 4: Tackling Merge Conflicts (You're here!)
- Part 5: Rebase vs. Merge: A Comparative Analysis
- Part 6: The Power of Interactive Rebase
- Part 7: Cherry-Picking Commits: A Selective Approach
- Part 8: Recovering Lost Commits with the Reflog
The above is the detailed content of Merge Conflicts: What They Are and How to Deal with Them. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

It's out! Congrats to the Vue team for getting it done, I know it was a massive effort and a long time coming. All new docs, as well.

I had someone write in with this very legit question. Lea just blogged about how you can get valid CSS properties themselves from the browser. That's like this.

I'd say "website" fits better than "mobile app" but I like this framing from Max Lynch:

The other day, I spotted this particularly lovely bit from Corey Ginnivan’s website where a collection of cards stack on top of one another as you scroll.

If we need to show documentation to the user directly in the WordPress editor, what is the best way to do it?

There are a number of these desktop apps where the goal is showing your site at different dimensions all at the same time. So you can, for example, be writing

Questions about purple slash areas in Flex layouts When using Flex layouts, you may encounter some confusing phenomena, such as in the developer tools (d...

CSS Grid is a collection of properties designed to make layout easier than it’s ever been. Like anything, there's a bit of a learning curve, but Grid is
