Git Merge Conflicts: Resolving Them Step by Step
A merge conflict happens when Git cannot automatically combine changes from two branches because both branches modified the same lines in the same file. Git marks the conflicting sections with <<<<<<<, =======, and >>>>>>> markers. You resolve it by editing the file to keep the code you want, removing the markers, then staging and committing.
Reading conflict markers
When a merge conflict occurs, Git writes both versions into the file separated by markers:
<<<<<<< HEAD
const price = amount * 1.16; // your branch
=======
const price = amount * 1.14; // the other branch
>>>>>>> feature/update-taxEverything between <<<<<<< HEAD and ======= is your current branch's version. Everything between ======= and >>>>>>> is the incoming branch's version.
Your job is to decide which version to keep, or combine them into something new, then delete all three marker lines.
Resolving in the terminal
Step by step:
- Run
git statusto see which files have conflicts. They appear under "Unmerged paths". - Open each conflicting file in your editor.
- Find the conflict markers (
<<<<<<<). Edit the file to keep the correct code and remove all marker lines. - Stage the resolved file:
git add path/to/file.ts - Once all conflicts are resolved, complete the merge:
Git pre-fills the commit message for merge commits.git commit
If you want to abort the merge entirely and go back to where you were before:
git merge --abortResolving in VS Code
VS Code highlights conflicts and offers clickable options above each one:
- Accept Current Change keeps your branch's version
- Accept Incoming Change keeps the other branch's version
- Accept Both Changes keeps both, stacked on top of each other
- Compare Changes opens a side-by-side diff
These buttons are convenient for simple conflicts. For complex ones where you need to combine logic from both sides, edit the file manually. The buttons cannot merge code intelligently; they only pick one side or concatenate both.
After resolving, save the file, then use the Source Control panel or terminal to stage and commit.
Conflicts during rebase
Rebase replays your commits on top of another branch. If a commit conflicts, Git pauses the rebase and asks you to resolve. The process is similar:
# Resolve the conflict in your editor, then:
git add path/to/file.ts
git rebase --continueIf multiple commits conflict, you resolve each one as Git replays it. To abort a rebase in progress:
git rebase --abortRebase conflicts can feel repetitive because the same lines may conflict across multiple commits. If this happens often, consider squashing your commits before rebasing.
Preventing merge conflicts
You cannot eliminate conflicts entirely, but you can reduce them:
- Pull frequently. The longer your branch lives without pulling from main, the more likely it diverges from what others pushed.
- Keep branches short-lived. Merge feature branches within a few days, not weeks.
- Avoid reformatting entire files. A whitespace-only commit that touches every line will conflict with every other branch. Run formatters as a separate, coordinated step.
- Communicate with your team. If two people are editing the same file, talk about it before both branches grow large.
Frequently Asked Questions
- What if I accidentally commit with conflict markers still in the file?
- The code will be broken because the marker lines are not valid syntax. Most CI pipelines catch this. If it reaches production, fix the file in a new commit. You can search your codebase for leftover markers with: grep -rn "<<<<<<" .
- Is git merge or git rebase better for avoiding conflicts?
- Neither avoids conflicts. Rebase rewrites history to be linear, which can make the log cleaner, but you still resolve the same conflicts. Merge preserves the branch structure. Pick the workflow your team agrees on and stay consistent.
- What does "both modified" mean in git status?
- It means both your branch and the branch you are merging changed the same file and Git could not auto-merge. Open the file, resolve the conflict markers, stage it, and commit.
Ready to build real-world apps?
Join the McTaba Labs full-stack marathon. Ship 8 production apps with M-Pesa, USSD, and WhatsApp integrations, and get career support until placement.
See Programs