# GitHub Merge Strategies: A Visual Explanation

[![Badge](https://androidweekly.net/issues/issue-664/badge align="center")](https://androidweekly.net/issues/issue-664)

Discussion started up at work this week about the different merge strategies available in GitHub. I looked around online to see if I could find a good article to send to share a quick snapshot of the differences represented visually and didn’t find any I liked, so here’s my take!

## Merging your Pull Request

You wrote all your code and are ready to merge! Let’s go! At the bottom of your Pull Request (PR) you’ll find a button with a drop down arrow that shows three options with descriptions, but what do they actually do? Sometimes it’s helpful to actually see how these things play out.

![GitHub merge dialog showing three options.  Create a merge commit: All commits from this branch will be added to the base branch via a merge commit.  Squash and merge: The 10 commits from this branch will be combined into one commit in the base branch.  Rebase and merge: The 10 commits from this branch will be rebased and added to the base branch.](https://cdn.hashnode.com/res/hashnode/image/upload/v1740762906175/1b89b3c1-4c48-421a-9dee-1bf515ebf43d.png align="center")

---

## **Before the Merge**

(aka you wrote code on `feature-branch` and are ready to merge back to `main`)

![A diagram showing the main branch with commits D to E and a feature branch with commits A to B to C](https://cdn.hashnode.com/res/hashnode/image/upload/v1740765759866/2188e3a0-a3d5-46cb-93d0-089d8e55686f.png align="center")

* `main` branch: `D → E`
    
* `feature-branch` branched from `D` and adds commits `A → B → C`
    

---

## **1\. Merge Commit**

* **Preserves full commit history**
    
* **Shows when branches were merged**
    
* **Creates an extra merge commit (**`M`)
    

**After merge:**

![A diagram showing the main branch with commits D to E to M and a feature branch with commits A to B to C to M showing that M is a new commit on the main branch merging the changes.](https://cdn.hashnode.com/res/hashnode/image/upload/v1740765766109/c112165e-71ff-4a04-b152-4466d41bb7bf.png align="center")

* The feature branch is merged with all its history intact.
    
* A new merge commit (`M`) is created.
    
    * These usually read like `Merge pull request #---- from feature-branch` in your git history.
        

---

## **2\. Squash Merge**

* **Clean history with a single commit**
    
* **Loses individual commits from the feature branch**
    

**After squash merge:**

![A diagram showing the main branch with commits D to E to S and a feature branch with commits A to B to C showing that S is a new commit on the main branch merging the changes that were combined from A, B, and C.](https://cdn.hashnode.com/res/hashnode/image/upload/v1740765780748/881fb9ee-e3be-4477-a5b7-fcec53e77479.png align="center")

* The entire `A → B → C` branch is squashed into a single commit `S`, removing individual commits.
    

---

## **3\. Rebase Merge**

* **Retains individual commits**
    
* **Creates a linear history**
    
* **Rewrites commit history (be careful in shared branches)**
    

**After rebase:**

![A diagram showing the main branch with commits D to E to A prime to B prime to C prime. This represents that A, B, and C are rebased on top of the Head of main, but are fundamentally new commits since they have new parent commits.](https://cdn.hashnode.com/res/hashnode/image/upload/v1740765793172/415a053f-7c52-4140-84e6-5f8e362f5e58.png align="center")

* The commits are replayed on top of `main`, making history linear.
    
* New commit hashes (`A'`, `B'`, `C'`) are created because their parent commits changed.
    

---

## **Final Comparison**

| Merge Type | Keeps Individual Commits? | Keeps Merge Info? | Linear History? | Creates Extra Commit? |
| --- | --- | --- | --- | --- |
| **Merge Commit** | ✅ Yes | ✅ Yes | ❌ No | ✅ Yes (`M`) |
| **Squash Merge** | ❌ No (One Commit) | ❌ No | ✅ Yes | ✅ Yes (`S`) |
| **Rebase Merge** | ✅ Yes | ❌ No | ✅ Yes | ❌ No |

---

Which one do you prefer? Let me know and thanks for reading!
