ByteByteGo Logo
Git Version Control

Git Merge vs. Git Rebase

Understand the difference between Git merge and Git rebase commands.

What are the differences?

When we ๐ฆ๐ž๐ซ๐ ๐ž ๐œ๐ก๐š๐ง๐ ๐ž๐ฌ from one Git branch to another, we can use โ€˜git mergeโ€™ or โ€˜git rebaseโ€™. The diagram below shows how the two commands work.

Git Merge

This creates a new commit Gโ€™ in the main branch. Gโ€™ ties the histories of both main and feature branches.

Git merge is ๐ง๐จ๐ง-๐๐ž๐ฌ๐ญ๐ซ๐ฎ๐œ๐ญ๐ข๐ฏ๐ž. Neither the main nor the feature branch is changed.

Git Rebase

Git rebase moves the feature branch histories to the head of the main branch. It creates new commits Eโ€™, Fโ€™, and Gโ€™ for each commit in the feature branch.

The benefit of rebase is that it has ๐ฅ๐ข๐ง๐ž๐š๐ซ ๐œ๐จ๐ฆ๐ฆ๐ข๐ญ ๐ก๐ข๐ฌ๐ญ๐จ๐ซ๐ฒ.

Rebase can be dangerous if โ€œthe golden rule of git rebaseโ€ is not followed.

The Golden Rule of Git Rebase

Never use it on public branches!