Git Merge vs Git Rebase
Git Merge vs. Rebase vs. Squash Commit! What are the differences?
When we ๐ฆ๐๐ซ๐ ๐ ๐๐ก๐๐ง๐ ๐๐ฌ from one Git branch to another, we can use โgit mergeโ or โgit rebaseโ. The diagram above shows how the two commands work.
๐๐ข๐ญ ๐๐๐ซ๐ ๐
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 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.
๐๐ก๐ ๐๐จ๐ฅ๐๐๐ง ๐๐ฎ๐ฅ๐ ๐จ๐ ๐๐ข๐ญ ๐๐๐๐๐ฌ๐ Never use it on public branches!