8. Merge Strategies

Once a Pull Request has been fully reviewed, all tests have passed, and approval has been granted, it is time to merge the changes into your main branch. GitHub offers **three different merge strategies** to help you maintain your project's commit history structure. In this chapter, we will study these strategies and when to use them.

The 3 Merge Options on GitHub

When you click the drop-down arrow on the green "Merge pull request" button on a PR page, you can choose from three options:

1. Create a Merge Commit (Default)

This performs a standard git merge. All commits from the feature branch are added to the target branch history, and a dedicated **Merge Commit** is created to join the branches.

  • Pros: Preserves the full, unaltered history and chronological context of all developer commits.
  • Cons: If your team works on many small features, your commit logs can quickly become cluttered and difficult to navigate.

2. Squash and Merge (Highly Recommended for Clean History)

This strategy takes **all commits** from the feature branch, combines them into **one single commit**, and applies it to the target branch.

  • Pros: Keeps your main branch history clean, linear, and readable. Wipes out messy WIP commits (like "Fix typo", "Debugging", "Add missing bracket").
  • Cons: Erases the individual granular commit history of the feature branch.
When to use: Highly recommended for standard feature branches where you only care about the final polished result going into your production code.

3. Rebase and Merge

This strategy takes each individual commit from the feature branch and applies them **individually** onto the target branch's latest commit, without creating a merge commit. It creates a perfectly straight, linear timeline.

  • Pros: Retains granular commit details while avoiding muddying the logs with merge commits.
  • Cons: Rewrites commit hashes. Requires careful handling if branches diverge.

Comparing the Strategies

Merge StrategyNew Commit Created?Granular History Kept?Timeline Structure
Create a Merge CommitYes (1 merge commit)Yes (all commits)Divergent (branching history lines visible)
Squash and MergeYes (1 combined commit)No (condensed)Linear (straight line)
Rebase and MergeNoYes (all commits)Linear (straight line)

Automatic Branch Deletion

Once a pull request is merged, the feature branch is no longer needed. To keep your repository tidy, you can configure GitHub to automatically delete branches upon successful merges:

  1. Go to your repository and click Settings.
  2. On the **General** settings tab, scroll down to the "Pull Requests" section.
  3. Check the box for "Automatically delete head branches".