Skip to content

Git Conflicts Tutorial

Overview

Git conflicts occur when changes in different branches or commits cannot be automatically merged by Git. This typically happens when there are competing changes to the same lines in a file. Resolving conflicts is a critical part of collaborative development and ensures that all changes are integrated correctly.

What is a Git Conflict?

A Git conflict arises when Git encounters changes in different branches or commits that cannot be automatically reconciled. Conflicts occur during merge, rebase, or cherry-pick operations when changes to the same lines or files are made in different commits.

How Conflicts Occur

Conflicts typically occur in the following scenarios:

  • Merge Conflicts: When merging branches with competing changes to the same lines or files.
  • Rebase Conflicts: When rebasing a branch onto another branch with conflicting changes.
  • Cherry-Pick Conflicts: When cherry-picking commits that have conflicts with the current branch.

Identifying Conflicts

When a conflict occurs, Git will:

  1. Mark Conflicted Files: Git marks the conflicted files with conflict markers.
  2. Notify the User: Git will display messages in the terminal indicating the files with conflicts.

Example: Conflict Markers in a File

A file with conflicts will contain markers like:

<<<<<<< HEAD
Your changes here
=======
Changes from the branch being merged
>>>>>>> branch-to-merge
  • <<<<<<< HEAD: The changes in your current branch.
  • =======: The dividing line between conflicting changes.
  • >>>>>>> branch-to-merge: The changes from the branch being merged.

Resolving Conflicts

To resolve conflicts:

  1. Open Conflicted Files: Edit the files to resolve the conflicts by removing the conflict markers and merging the changes manually.
  2. Add the Resolved Files: Use git add to mark the conflicts as resolved.
  3. Complete the Merge/Rebase: Commit the resolved changes.

Steps to Resolve Conflicts

  1. Edit the Conflicted Files:

    Open the file(s) with conflicts in your text editor and manually resolve the differences. For example:

    // Before resolving:
    <<<<<<< HEAD
    Your changes here
    =======
    Changes from the branch being merged
    >>>>>>> branch-to-merge
    
    // After resolving:
    Combined changes that should be kept
    
  2. Mark as Resolved:

    After editing the files, use:

    git add <file>
    
  3. Commit the Changes:

    Complete the merge or rebase by committing the resolved changes:

    git commit
    

    For rebases, continue the rebase process with:

    git rebase --continue
    

Examples of Conflict Resolution

Example 1: Resolving a Merge Conflict

  1. Merge Branches:

    git merge <branch-to-merge>
    
  2. Resolve Conflicts in Files:

    Edit conflicted files and remove conflict markers.

  3. Add and Commit:

    git add <resolved-file>
    git commit -m "Resolved merge conflict"
    

Example 2: Resolving a Rebase Conflict

  1. Rebase Branch:

    git rebase <base-branch>
    
  2. Resolve Conflicts in Files:

    Edit conflicted files and remove conflict markers.

  3. Add and Continue Rebase:

    git add <resolved-file>
    git rebase --continue
    

Example 3: Resolving a Cherry-Pick Conflict

  1. Cherry-Pick Commit:

    git cherry-pick <commit-hash>
    
  2. Resolve Conflicts in Files:

    Edit conflicted files and remove conflict markers.

  3. Add and Commit:

    git add <resolved-file>
    git cherry-pick --continue
    

Common Use Cases

  • Merging Feature Branches: Integrating feature branches into a main branch where conflicts may arise.
  • Rebasing Updates: Updating a feature branch with the latest changes from the main branch.
  • Applying Commits: Cherry-picking specific commits from one branch to another with potential conflicts.

Summary

Git conflicts occur when changes cannot be automatically merged due to competing modifications. Resolving conflicts involves manually editing files to reconcile differences, marking the conflicts as resolved, and completing the merge, rebase, or cherry-pick process. Understanding and effectively resolving conflicts is crucial for maintaining a smooth and collaborative development workflow.