# Git Merge and Rebase: The Dance of Branches and the Rebirth of Commits

Merge and rebase are two different approaches to get changes from one branch to another.

Let's assume we are trying to get the latest changes from the `main` to a `feature branch` we are currently developing.

---

## Merge

Takes the latest changes from the `main` and creates a new commit in the `feature branch`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1692737353146/ffe5da0a-13d4-4bbb-aa6e-cdd216b0e746.png align="center")

**Commands**:

```plaintext
git checkout feature-branch
git merge main
```

<table><tbody><tr><td colspan="1" rowspan="1" colwidth="363"><p><strong>Pros:</strong></p></td><td colspan="1" rowspan="1"><p><strong>Cons:</strong></p></td></tr><tr><td colspan="1" rowspan="1" colwidth="363"><p>Preserves the whole commit history.</p></td><td colspan="1" rowspan="1"><p>Can get messy with more details.</p></td></tr></tbody></table>

---

## Rebase

Changes the base of a feature branch to the latest commit on the `main` and then places the `feature branch` changes from there.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1692738886747/9b19ce38-abc4-4e47-9404-c77d9479270a.png align="center")

**Commands:**

```plaintext
git checkout feature-branch
git rebase main
```

<table><tbody><tr><td colspan="1" rowspan="1" colwidth="384"><p><strong>Pros:</strong></p></td><td colspan="1" rowspan="1"><p><strong>Cons:</strong></p></td></tr><tr><td colspan="1" rowspan="1" colwidth="384"><p>Linear and cleaner commit history.</p></td><td colspan="1" rowspan="1"><p>Reduced traceability</p></td></tr></tbody></table>
