Merging Branches

Master the art of combining branches and resolving conflicts in Git

Learning Objectives

  • Understand different types of merges in Git
  • Learn how to perform fast-forward and three-way merges
  • Master conflict resolution techniques
  • Discover best practices for clean and effective merging

Understanding Types of Merges

Git uses different merge strategies depending on the branch history. The two main types of merges are fast-forward and three-way merges.

Fast-Forward Merge

Occurs when there is a direct linear path from the source branch to the target branch. No additional commit is created.

# Fast-forward merge example
$ git checkout main
$ git merge feature
# Fast-forward

Three-Way Merge

Creates a new merge commit when the branches have diverged. Combines the histories of both branches.

# Three-way merge example
$ git checkout main
$ git merge feature
# Merge made by the 'recursive' strategy

Basic Merge Commands

Here are the essential commands you'll use when merging branches:

Basic Merge Commands
# Check branch status before merge
$ git status
# Update your current branch
$ git pull
# Merge a branch into current branch
$ git merge branch-name
# Merge with commit message
$ git merge branch-name -m "Merge feature branch"
# Abort a merge in case of conflicts
$ git merge --abort

Merge Strategies

Git offers several merge strategies to handle different scenarios:

Common Merge Strategies

  • recursive:Default strategy for branches with one merge base
  • ours:Resolves conflicts by keeping the current branch version
  • theirs:Resolves conflicts by keeping the merged branch version

Strategy Options

Merge Strategies
# Merge using specific strategy
$ git merge feature-branch -s recursive
# Merge with strategy options
$ git merge feature-branch -X ours
$ git merge feature-branch -X theirs
# Merge and squash commits
$ git merge --squash feature-branch

Handling Merge Conflicts

Merge conflicts occur when Git can't automatically resolve differences between branches. Here's how to handle them:

Conflict Resolution Steps

  1. 1.
    Identify conflicted files using git status
  2. 2.
    Open conflicted files and look for conflict markers (<<<<<<<, =======, >>>>>>>)
  3. 3.
    Edit files to resolve conflicts, removing conflict markers
  4. 4.
    Stage resolved files and complete the merge with a commit

Conflict Resolution Commands

Conflict Resolution
# Check status of conflicted files
$ git status
# After resolving conflicts
$ git add resolved-file.txt
# Continue merge after resolution
$ git merge --continue
# Abort merge if needed
$ git merge --abort

Merge Best Practices

  • Update before merging:

    Always pull the latest changes from the target branch before merging

  • Test before merging:

    Ensure all tests pass in your feature branch before merging

  • Review changes:

    Double-check the changes being merged using git diff

  • Clean history:

    Consider using --squash for feature branches to maintain a clean history

What's Next?

Now that you understand merging, in the next lesson you'll learn about:

  • Working with remote repositories
  • Setting up remote connections