Home >Web Front-end >CSS Tutorial >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.
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.
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.
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.
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.
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.
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.
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!