Merge Branches and Resolve Merge Conflicts

Summary:
Understand git merge and how to handle conflicts.


Merging branches is a fundamental operation in Git, frequently encountered during collaborative software development. This article covers how to use git merge to combine branches, explains why merge conflicts occur, and provides step-by-step guidance for resolving conflicts.


Table of Contents


What is a Branch in Git?

A branch in Git is a lightweight movable pointer to a commit. Developers use branches to work on features, fixes, or experiments in isolation from the main codebase (main or master branch).

# Create and switch to a new branch called 'feature'
git checkout -b feature

Introduction to git merge

The git merge command lets you integrate changes from one branch into another. For example, after finishing work on a feature branch, you merge it into the main branch to make your changes part of the main codebase.

Example:

# Switch to the main branch
git checkout main

# Merge the feature branch into main
git merge feature

This tries to automatically combine the histories and contents of two branches.


Merging Branches: Step-by-Step

  1. Ensure Your Branches Are Up-to-Date
    First, fetch the latest changes:

    git fetch origin
    
  2. Switch to the Target Branch
    Typically, you merge into main or develop:

    git checkout main
    
  3. Merge the Source Branch
    Use git merge followed by the branch name:

    git merge feature
    

    If there are no conflicts, Git creates a new "merge commit" combining the histories.


What Causes Merge Conflicts?

A merge conflict happens when Git is unable to automatically combine file changes from different branches. Common causes include:

  • Two branches modify the same line in a file differently.
  • A file is deleted in one branch but modified in another.
  • Changes overlap in such a way that Git cannot decide which content to keep.

How to Identify and Resolve Merge Conflicts

When a conflict happens:

  1. Git Halts the Merge Process
    You'll see messages like:
    CONFLICT (content): Merge conflict in <filename>

  2. Check Conflict Status

    git status
    

    This lists conflicted files.

  3. Open Conflicted Files
    In the conflicted files, you'll see conflict markers:

    <<<<<<< HEAD
    Code from the current branch
    =======
    Code from the branch being merged
    >>>>>>> feature
    
  4. Manually Edit the Files
    Decide which parts to keep, modify, or combine. Remove markers (<<<<<<<, =======, >>>>>>>) after resolving.

  5. Mark Conflict as Resolved

    git add <filename>
    
  6. Finish the Merge

    git commit
    

    If you used git merge (not with --no-commit), Git may automatically create a merge commit after all conflicts are resolved and staged.

Example

Suppose both branches changed the same line in app.py. You'll see:

<<<<<<< HEAD
print("Hello from main branch")
=======
print("Hello from feature branch")
>>>>>>> feature

You might resolve it like this:

print("Hello from main and feature branches")

Then save, add, and commit.


Best Practices for Avoiding Merge Conflicts

  • Pull Regularly: Frequently synchronize your branches with the remote repository.
  • Small, Frequent Merges: Merge changes incrementally rather than in large batches.
  • Clear Communication: Coordinate with team members to prevent overlapping work on the same files.
  • Rebase When Appropriate: Use git rebase to streamline your commit history before merging (advanced users).

Final Words

Merging branches and resolving conflicts are central to effective collaboration in Git. While merge conflicts can be frustrating, they're an opportunity to ensure the integrity and intent of everyone’s contributions. Practice makes perfect: with experience, resolving conflicts becomes a routine part of your development workflow.

Happy coding!