Suggest an editImprove this articleRefine the answer for “What is the main difference between merge and rebase?”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)The main difference between **`git merge`** and **`git rebase`** is how they work with the commit history. **Key point:** `merge` keeps the history, while `rebase` rewrites the history.Shown above the full answer for quick recall.Answer (EN)ImageThe main difference between `git merge` and `git rebase` is **how they work with the commit history**. `merge` **preserves the history**, `rebase` **rewrites the history**. --- ## Key idea (very important for an interview) - `git merge` - *"I combine the branches as they are"* - `git rebase` - *"I rewrite my commits as if they had been made later"* --- ## Visual comparison ### `git merge` ``` A---B---C---F---M (main) \ / D---E--- ``` - a **merge commit** is created - it is visible that there was a separate branch - the history branches out --- ### `git rebase` ``` A---B---C---D'---E' (feature) ``` - **there is no merge commit** - the history is linear - commits `D` and `E` became **new ones** --- ## The main difference in one paragraph | Parameter | git merge | git rebase | |---|---|---| | History | Preserved | Rewritten | | Merge commit | Yes (usually) | No | | Safety | Safe for shared branches | Dangerous for shared branches | | History | Branching | Linear | | Conflicts | Once | May happen several times | | Usage | Team work | Local work | --- ## When to use which ### Use `merge` if: - the branch is **shared** - the code is already **pushed** - the full history matters - you work in a team `merge` is the safe default option --- ### Use `rebase` if: - the branch is **only yours** - you want a **clean history** - you are preparing code to be merged - you are updating a feature branch from `main` `rebase` is for tidying things up --- ## A popular phrasing for an interview > `git merge` **combines branches, preserving the history and creating a merge commit,** > **while** `git rebase` **moves the commits of one branch on top of another, rewriting the history and making it linear.** --- ## The golden rule (often asked) > **Never rebase public branches** > **Merge can always be done**For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.