Git Merge vs. Rebase vs. Squash Commit! What are the differences?...

@bytebytego
Bytebytego@bytebytego
79 views Dec 18, 2023 ~1 min read
Advertisement
1
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 below 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!

--
Subscribe to our weekly newsletter to get a Free System Design PDF (158 pages): bit.ly/3KCnWXq
Media image
Actions
What You Can Do
  • Export as PDF or Markdown
  • Batch Export to Notion
  • Bookmark & Highlight
  • Screenshot Tweet
Create Free Account

Includes 7-day Premium trial

Advertisement