Resolving conflicts by hand
Merge conflicts aren't scary — they're git asking you to make a decision.
- Explain what causes a merge conflict
- Read the conflict markers in a file
- Resolve a conflict manually and complete the merge
A merge conflict happens when two branches changed the same lines of the same file in different ways. Git can combine non-overlapping changes on its own, but here it can't know which version you want — so it stops and asks you. This is git being honest, not broken, and resolving one by hand is a calm, mechanical process.
What a conflict looks like
When a merge or rebase hits a conflict, git pauses and marks the clashing
spot inside the file:
<<<<<<< HEAD
const greeting = "Hello";
=======
const greeting = "Hi there";
>>>>>>> add-loginRead the markers literally:
- Between
<<<<<<< HEADand=======is your current branch's version. - Between
=======and>>>>>>> add-loginis the incoming version.
Resolving it
You resolve a conflict by editing the file into the final state you want, then deleting all three marker lines. Keep one side, keep the other, or write a combination:
const greeting = "Hi there";Then tell git the conflict is settled and finish:
git add greeting.js # mark this file resolved
git status # check for any other conflicted files
git commit # complete the merge
# (for a rebase, use: git rebase --continue)git status during a conflict is your guide — it lists exactly which files still
need attention.
If it goes wrong, back out
You're never trapped. To abandon the whole thing and return to before you started:
git merge --abort # or: git rebase --abortDo it yourself: make two branches that edit the same line of a file differently, merge one into the other, and resolve the conflict by hand. Doing it once removes the fear entirely — it's just choosing which lines to keep.
Where to go next
Conflicts are one kind of "oops." Next: undoing anything — the commands for reverting, resetting, and recovering when something goes wrong.