Rabi Siddique
424 words
2 minutes
Merge vs Rebase vs Squash
2024-08-05

In the world of software development, managing changes and versions efficiently is critical, especially when multiple branches of development are involved.

Bringing Changes from Main Branch to Feature Branch#

Image Credits: ByteByteGo

Let’s say we have created a new feature branch from main:

Image Credits: ByteByteGo

We add commits A, B, and C on the feature branch:

Image Credits: ByteByteGo

At the same time, the main branch has commits D and E:

Image Credits: ByteByteGo

We can picture this as two branches of a tree growing in different directions: Image Credits: ByteByteGo

Keeping the feature branch updated with the main branch is a crucial part of the Git workflow. We can do this with either Git merge or Git rebase.

Image Credits: ByteByteGo

Git Merge#

Git merge pulls in the latest changes from main into the feature branch, creating a new merge commit in the process. Image Credits: ByteByteGo

It’s like tying the two branches with a knot.

Git Rebase#

Git rebase changes the base of the feature branch to the latest commit on the main branch, and then we place our changes from there. Image Credits: ByteByteGo

It gives us a clean, straightforward history.

Bringing Changes from Feature Branch to Main Branch#

Once we finish developing the feature, we need to bring it to the main branch.

Image Credits: ByteByteGo

We have a few options.

Git Merge#

Git will create a new commit that ties together the history of the feature and main branches. Image Credits: ByteByteGo

It’s like creating a knot on the rope, showing where the branches joined. But if we do this a lot, it ends up creating a messy history: Image Credits: ByteByteGo

Git Rebase and Fast Forward Merge#

We can use Git rebase to move the changes of our feature branch to the tip of the main branch, then perform a fast-forward merge.

Image Credits: ByteByteGo

Image Credits: ByteByteGo

It is like straightening a rope, as if it’s all in one line.

Squash Commit#

The last option is squash commit. With squashing, all the feature branch commits are squeezed into a single commit and merged into the main branch.

Image Credits: ByteByteGo

This keeps the main branch’s history linear, like rebasing, while still creating a single merge commit. But remember that we lose fine details of individual feature commits in the main branch commit history.

Image Credits: ByteByteGo

With that said, squashing commits is a popular strategy on GitHub because it allows us to tidy up history in the main branch while still preserving the detailed commit history in the feature branch. It’s more like a hybrid approach. Image Credits: ByteByteGo

Further Reading#

Merge vs Rebase vs Squash
https://rabisiddique.com/posts/merge-vs-rebase-squash/
Author
Rabi Siddique
Published at
2024-08-05