Nothing stalls a coding session faster than a merge conflict. You pull, Git refuses, and your terminal lists files you cannot merge and changes you are afraid to touch. The good news is that a conflict is not data loss. It is Git asking you to decide which version of a few lines wins. Learning how to handle merge conflicts calmly is one of the most useful skills a developer picks up, because it turns a scary red error into a routine editing job. This guide walks through the exact steps: read the markers, decide what stays, test the result, and recover safely if you pick the wrong side.
What a Merge Conflict Actually Is
A merge conflict happens when Git cannot combine two versions of the same lines on its own. You edited a function in auth.js, a teammate edited the same function on another branch, and Git has no way to know whose change is correct. The same thing happens when one branch deletes a file that another branch modified, or when two branches rename the same file in different ways. Git stops the merge, marks the files, and waits for a human decision. Your work is still there. Both versions sit in the file, wrapped in conflict markers, and nothing is overwritten until you commit the resolution.
Conflicts feel worse than they are because the markers look like broken code. They are not. They are a plain-text record of both sides, and once you know how to read them, the rest is ordinary editing. Most conflicts involve a handful of lines, and most of them take a few minutes to resolve.
Before You Touch Anything: Make a Safety Copy
Before you resolve anything, protect the state you are in. The cheapest safety net is a backup branch pointing at your current HEAD. Run git branch backup-before-merge and you can return to exactly this point no matter what goes wrong later. It costs nothing, and it removes the fear behind most bad decisions during a conflict.
Next, run git status to list the conflicted files, and git log --oneline --graph -10 to see which branches are being merged and where each change came from. Knowing which side is yours and which side is theirs makes every decision after this easier. If the history looks tangled and you need to untangle it later, our guide on how to fix broken Git history with rebase, reset, and reflog explains the recovery tools in detail.
How to Handle Merge Conflicts Step by Step
To resolve git merge conflict markers, work through the conflicted files one at a time. Do not try to fix everything at once.
- Run
git statusand note every file listed under “Unmerged paths”. - Open the first conflicted file and search for
<<<<<<<. - Read both versions and decide what the merged result should look like.
- Edit the file so it contains the correct code, then delete all the marker lines.
- Run your tests or build to check the merged code still works.
- Stage the file with
git addand repeat for the next file. When every file is staged, commit the merge.
Commit with a message that says what you resolved, not just “fix merge”. A message like “Merge main: keep new auth flow, keep updated rate limits” tells the next person exactly what you chose. Good commit messages make code review faster, and they matter even more after a merge, when the history is the only record of a judgment call — which is why it pays to learn how to write commit messages that make code review faster before your next merge.
Reading Conflict Markers Without Panicking
Here is what a typical conflict looks like inside a file:
<<<<<<< HEAD
const timeout = 5000;
=======
const timeout = 10000;
>>>>>>> feature/faster-retries
Everything between <<<<<<< HEAD and ======= is your current branch. Everything between ======= and >>>>>>> is the incoming branch. Pick one side, combine them, or write something new, then delete the markers.
Most editors make this easier. VS Code’s merge editor shows both versions side by side with buttons to accept the current change, the incoming change, or both. If you prefer the terminal, git mergetool opens a visual diff tool of your choice. One warning about choosing “both”: when two people add similar code in the same spot, accepting both can duplicate logic or declare the same variable twice. Read the combined result before you stage it.
That read-through is the heart of git merge conflict resolution. The tooling can show you the two versions, but only you can decide what the correct merged code is.
When the Conflict Is Bigger Than One File
Some conflicts are not about lines of code. If one branch deleted a file that another branch modified, Git asks whether the file should stay or go. Check whether anything still imports it before you decide. If two branches renamed the same file differently, pick one name and make sure every import and reference uses it.
Lockfiles deserve special treatment. Do not hand-edit a conflicted package-lock.json or yarn.lock. Accept one version, then regenerate it with your package manager so the lockfile matches the merged package.json. The same applies to generated files in general: regenerate them rather than merging them by hand.
Binary files such as images cannot be merged at all. For those, pick a side with git checkout --ours <file> or git checkout --theirs <file> and move on. And if conflicts keep hitting you on long-lived branches, the branch strategy may be the problem. Short-lived feature branches conflict less than branches that sit untouched for weeks, which is one reason teams compare rebase and merge workflows before settling on one. Our comparison of git rebase vs merge and what each does to your history lays out the trade-offs.
If You Already Made It Worse: Undo Options
If the merge is still in progress and you want to start over, run git merge --abort. That returns your working tree to the state before the merge began, with your uncommitted work intact. It is the safest escape hatch Git offers.
If you already committed a bad resolution and have not pushed it, git reset --hard HEAD~1 takes you back one commit. Only use that on commits nobody else has pulled. If the bad merge is already shared, use git revert to create a new commit that undoes it instead. And if you are not sure where you are, git reflog lists every position HEAD has been in, so you can find the commit before the merge and check out from there.
The rule is simple: do not force-push to fix a merge on a shared branch. A force-push rewrites history for everyone who already pulled, and it trades one person’s confusion for a whole team’s.
Preventing the Next Conflict
The best merge conflict git workflow is the boring one: pull or rebase often, keep branches short-lived, and commit in small steps. A branch that lives for two days collects far fewer conflicts than one that lives for two weeks, because there is less time for someone else to edit the same lines.
Split large changes into smaller pull requests. When two people must touch the same file, a quick message about who is editing what avoids the conflict entirely. Teams that adopt a clear feature branch strategy, one branch per task merged as soon as it is reviewed, see fewer conflicts and smaller ones when they do appear.
None of this removes conflicts completely, and that is fine. Conflicts are a normal part of working with other people in the same codebase. The goal is not zero conflicts. It is conflicts that take five minutes instead of an afternoon.
Frequently Asked Questions
Can a merge conflict delete my work?
No. Git keeps both versions in the file until you commit the resolution. Work is lost when people panic and run commands they do not understand, not by the conflict itself.
Should I use rebase or merge to avoid conflicts?
Neither avoids conflicts. Rebase replays your commits on top of the latest main, so you resolve conflicts one commit at a time. Merge resolves them all at once. Pick the workflow your team agrees on and stick to it.
What if the same file conflicts on every merge?
That usually means two people keep editing the same lines, or a generated file is committed that should not be. Talk to the other person, split the file if it has grown too big, or add the generated file to .gitignore.
Handle your next conflict the same way: back up, read both sides, decide line by line, test, then commit. After two or three clean resolutions, the error message stops looking like a disaster and starts looking like a to-do list.