Error Codes Wiki

Git Merge Conflict — Resolving File Conflicts During Merge, Rebase, and Cherry-Pick

Warningcommand

About Git Merge Conflict

Fix Git merge conflicts when two branches modify the same lines of code, preventing automatic merge and requiring manual conflict resolution. This guide covers everything you need to know about this topic, including common causes, step-by-step solutions, and answers to frequently asked questions.

Here are the key things to understand: Merge conflicts occur when Git cannot automatically combine changes from different branches. Conflicts are marked in files with <<<<<<< HEAD, =======, and >>>>>>> branch-name markers. Conflicts can happen during merge, rebase, cherry-pick, and stash apply operations. Git marks the file as 'both modified' and waits for manual resolution before completing the merge. After resolving all conflicts, you must stage the files and complete the merge/rebase. Understanding these fundamentals will help you diagnose and resolve this issue more effectively.

The most common reasons this occurs include: Two branches modified the same lines in the same file independently. File deleted in one branch but modified in another. Rebasing a long-lived branch onto main with many changes in overlapping areas. Cherry-picking a commit that depends on context that differs between branches. Identifying the root cause is the first step toward finding the right solution.

To resolve this, follow these recommended steps: Identify conflicted files: 'git status' shows files with 'both modified' status. Open each conflicted file and look for <<<<<<< markers — choose or combine the changes. Remove all conflict markers (<<<, ===, >>>) and save the file with the desired final content. Stage resolved files: 'git add resolved-file.txt' for each file. Complete the operation: 'git merge --continue' or 'git rebase --continue' depending on the operation. If these steps do not resolve the issue, consider consulting additional resources or a qualified professional.

This article is part of our Linux Error Codes collection on Error Codes Wiki. We provide comprehensive, up-to-date information to help you find solutions quickly.

Quick Answer

Can I abort the merge and go back?

Yes. Run 'git merge --abort' to cancel the merge and return to the state before the merge started. For rebase: 'git rebase --abort'. For cherry-pick: 'git cherry-pick --abort'. No data is lost.

Overview

Fix Git merge conflicts when two branches modify the same lines of code, preventing automatic merge and requiring manual conflict resolution.

Key Details

  • Merge conflicts occur when Git cannot automatically combine changes from different branches
  • Conflicts are marked in files with <<<<<<< HEAD, =======, and >>>>>>> branch-name markers
  • Conflicts can happen during merge, rebase, cherry-pick, and stash apply operations
  • Git marks the file as 'both modified' and waits for manual resolution before completing the merge
  • After resolving all conflicts, you must stage the files and complete the merge/rebase

Common Causes

  • Two branches modified the same lines in the same file independently
  • File deleted in one branch but modified in another
  • Rebasing a long-lived branch onto main with many changes in overlapping areas
  • Cherry-picking a commit that depends on context that differs between branches

Steps

  1. 1Identify conflicted files: 'git status' shows files with 'both modified' status
  2. 2Open each conflicted file and look for <<<<<<< markers — choose or combine the changes
  3. 3Remove all conflict markers (<<<, ===, >>>) and save the file with the desired final content
  4. 4Stage resolved files: 'git add resolved-file.txt' for each file
  5. 5Complete the operation: 'git merge --continue' or 'git rebase --continue' depending on the operation

Tags

gitmerge-conflictrebaseresolutionversion-control

More in Command

Frequently Asked Questions

Yes. Run 'git merge --abort' to cancel the merge and return to the state before the merge started. For rebase: 'git rebase --abort'. For cherry-pick: 'git cherry-pick --abort'. No data is lost.