Git · Beginner Friendly

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.

Check state
git status
git diff --name-only --diff-filter=U

The 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:

Conflict markers
<<<<<<< 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.

Accept a side wholesale
git checkout --ours path/to/file
git checkout --theirs path/to/file

During 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.

Finish the merge
git add path/to/file
git status
git commit

Repeat 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.

Common checks
npm test
php artisan test
pytest

What to do if you panic

You can always back out. A merge conflict is not destructive until you commit.

Safe escape hatches
git merge --abort
git rebase --abort
git reflog

git 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

Pull main into your feature branch daily, so big divergence never builds up.
Keep pull requests small and short-lived (days, not weeks). Long branches always conflict.
Agree with your team on code formatting (Prettier, PHP-CS-Fixer, Black) so whitespace does not cause fake conflicts.

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.