How to Resolve Git Merge Conflicts, Step by Step
Two branches edited the same lines. Git paused the merge and printed scary markers in your files. Here is the exact, calm sequence of commands to get back to a clean working tree without losing work.
What a merge conflict actually is
A Git merge conflict happens when Git cannot safely combine two commits. Usually that means two branches changed the same lines of the same file, or one branch deleted a file the other edited. Git refuses to guess. It stops the merge, marks the file as unmerged, and asks you to pick the correct result by hand.
The conflict is not a bug. It is Git doing its job. You keep your work, Git keeps theirs, and you get to decide which version — or combination — ships.
Step 1: See which files are conflicted
When a merge pauses, run this first. The files you need to fix are in the “Unmerged paths” section.
git statusgit diff --name-only --diff-filter=UThe second command prints just the conflicted file paths, one per line. Handy in big merges.
Step 2: Read the conflict markers
Open a conflicted file in your editor. You will see blocks that look like this:
<<<<<<< HEAD total = price * qty ======= total = (price * qty) - discount >>>>>>> feature/apply-discount
The top section is the version on your current branch (ours). The bottom section is the version from the branch being merged in (theirs). Your job is to delete the markers and keep the final code you actually want to ship.
You can keep one side, the other side, a mix of both, or write something new. Git does not care which — it just needs the file to compile into a clean state.
Step 3: Pick a side without opening the file (optional)
If the correct answer is simply “keep my version” or “keep their version” for the whole file, Git has a shortcut.
git checkout --ours path/to/filegit checkout --theirs path/to/fileDuring a regular git merge, ours is the branch you were on when you ran the merge. During a git rebase, the meanings swap: ours becomes the branch being replayed onto and theirs is your own commits. It confuses everyone once, and then you remember forever.
Step 4: Mark the file resolved and commit
Once the file looks right in the editor, tell Git it is done.
git add path/to/filegit statusgit commitRepeat git add for every conflicted file. When git status shows “All conflicts fixed but you are still merging”, run git commit. Git prefills a merge commit message for you; just save and close.
Step 5: Run your tests before pushing
The build can be green and the code can still be wrong. The easiest bug to ship in a conflict is accidentally deleting a line from both sides. Always boot the app, run your test suite, and at minimum open the feature you were merging.
npm testphp artisan testpytestWhat to do if you panic
You can always back out. A merge conflict is not destructive until you commit.
git merge --abortgit rebase --abortgit refloggit reflog is the real superpower. It prints every HEAD your repo has pointed at for the last ~90 days. You can always git reset --hard <reflog-hash> to jump back to a known good state, even after a bad commit.
Tips to get fewer conflicts next time
Frequently Asked Questions
What causes a Git merge conflict?
Two branches changed the same lines of the same file, or one branch deleted a file the other edited. Git cannot decide which version to keep, so it pauses and asks you.
How do I abort a merge?
Run git merge --abort before you commit. Your branch returns to the state from before the merge started, with no history written.
What is the difference between ours and theirs?
During a merge, ours is the branch you are on, theirs is the branch being merged in. During a rebase, it is reversed: ours is the branch you are replaying onto, theirs is your own commits.
Can I resolve conflicts with a GUI tool?
Yes. VS Code, IntelliJ, GitKraken, Fork, and Sourcetree all show conflicts in a three-pane merge view. Under the hood they do exactly what this guide shows: keep lines, drop lines, then git add the file.