How to Resolve Merge Conflicts in Git

A Git merge conflict means Git cannot automatically combine changes to the same part of a file. You can resolve it by reviewing the conflicting versions, editing the file to the intended result, and completing the merge or rebase.

Practice Git workflows →

Why Git merge conflicts happen

When two branches change overlapping lines, Git may not know which result is correct. Rather than silently choosing one side, Git marks the affected files and asks you to resolve them. Conflicts can happen during a merge, rebase, cherry-pick, or other operation that integrates changes.

Step 1: Identify the conflicted files

Start with git status. Git lists files that need resolution and indicates whether you are in a merge or another operation. Open each conflicted file in an editor and find the conflict markers.

<<<<<<< HEAD
The version from the current branch
=======
The version being merged in
>>>>>>> feature/update-copy

In this example, the section above ======= is the current branch’s version, and the section below it is the incoming version. The labels depend on the operation and branch; inspect the context instead of assuming one side is always the correct choice.

Step 2: Edit the file to the intended result

Decide whether to keep one side, combine both changes, or write a different version that satisfies the feature and existing behavior. Edit the file directly and remove all conflict markers. Do not choose a side mechanically if it would discard needed work.

For example, if both branches add distinct information, the final file may contain both lines in the correct order. Read the surrounding code and consider how the combined result behaves; a conflict can affect more than the marked lines.

Step 3: Stage and complete the operation

After resolving a file, stage it to tell Git that it is resolved. Review the staged result, then complete the operation that started the conflict:

git add path/to/file
git diff --cached
# For a merge:
git merge --continue
# For a rebase:
git rebase --continue

Use the continuation command that matches the operation shown by git status. A merge may also complete with git commit if Git requests it. A rebase can stop for additional conflicts; resolve and continue each one until it finishes.

Step 4: Verify the resolved changes

How to abort a merge or rebase

If you need to stop and return to the state before the operation, first check git status to identify what is in progress. Then use the matching command:

Aborting returns the operation to its earlier state, so save any important manual edits elsewhere first. If you have not started a merge or rebase, these commands do not apply.

Merge conflict FAQ

Can I keep changes from both branches?

Yes. Edit the conflicted file so it contains the intended combined result, remove the markers, stage the file, and complete the operation. Keeping both sides is appropriate only when the changes work together.

Do I need to resolve conflicts one file at a time?

You can resolve several files before staging them, but review each conflict carefully. Git considers a file resolved once you stage your edited version.

Learn Git Faster by Practicing

Reading is just the first step. Real learning happens when you practice.

Start Learning Git →