Git Merge Conflicts: Branching, Merging & How to Resolve
Git merge conflicts feel like a failure the first time they happen, but they are Git declining to guess. Two branches changed the same lines in different ways, and a tool that silently picked one would eventually pick wrong. This part covers the whole path: how branches and merges actually behave, what the conflict markers in your PHP files mean, how to resolve a conflict calmly, and how to back…
Git merge conflicts can initially appear as a failure, but they are Git's way of not guessing. When two branches make changes to the same lines differently, Git requires human intervention to resolve the conflict. This section explains how branches and merges work, what the conflict markers in your PHP files mean, how to resolve conflicts calmly, and how to revert if you'd rather start fresh.
In Part 2, we covered staging and commits; here, we use those concepts to create and combine branches. Git 2.43.0 was used for all commands in this example. The examples involve a small Invoice.php class, which makes the conflicts relatable.
A branch is a pointer, so branching is cheap. To create a branch, run `git switch -c feature/tax`. This creates and moves to a new branch. You can then edit files, stage changes with `git add -p`, and commit them using `git commit`. To view all local branches, run `git branch`. When you make commits on a branch, the branch pointer moves forward, while the main branch remains unchanged. There are two types of merges: fast-forward and merge commit.
A fast-forward occurs when the main branch has not moved since you branched. Git simply moves the main pointer forward without creating a merge commit. Conversely, a true merge (merge commit) happens when both branches have new commits. Git creates a new commit with two parents. You can force a merge even if a fast-forward is possible by using `git merge --no-ff`. This keeps the branch visible in the history. If you only want fast-forward merges, use `git merge --ff-only`; it will abort if a merge commit is needed.
Conflicts occur when both branches have changed the same region differently. In the example, one branch changed the currency to EUR, while another changed it to LKR. After merging, Git stops halfway and waits for you to resolve the conflict. You'll see `UU Invoice.php` when running `git status -s`, indicating an unmerged file. Git also provides an escape hatch with `git merge --abort` if you need to revert.
To resolve a conflict, open the file and examine the conflict markers. The common ancestor, which you can view using `git config --global merge.conflictStyle zdiff3`, will help you understand the changes made on each side. Decide which version of the code you want, remove the conflict markers ( `HEAD `, `======= `, and ` `), and keep the valid code. Remove any leftover conflict markers using `grep -rn ^ \|^ `. Stage the resolved file with `git add Invoice.php`, and finish the merge by running `git merge --continue`.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.