Skip to main content
1:1 mock interviews with Sumit, scored on a real hiring rubric.Book a slot →

Git Rebase vs Merge: When to Use Each (With a Safe Team Workflow)

Understand the real difference between git merge and git rebase, see what each does to your history, learn interactive rebase, and follow a safe workflow that never breaks a teammate's branch.

Sumit KumarUpdated Oct 8, 20263 min readGit
Git Rebase vs Merge: When to Use Each (With a Safe Team Workflow)

Sooner or later every developer hits this question: should I git merge or git rebase? Both bring changes from one branch into another. The difference is what your history looks like afterwards, and whether you rewrite commits that other people already have.

The starting point#

You branched feature off main. Since then, you made two commits and a teammate pushed two commits to main:

Starting point: main has commits A, B, E, F while the feature branch added C and D from B
Text
          C---D        feature
         /
A---B---E---F          main

Now you want your branch to include E and F.

Option 1: Merge#

Bash
git switch feature
git merge main
After git merge: history keeps both branches and a new merge commit M joins F and D
Text
          C---D---M    feature
         /       /
A---B---E---F----      main

Git creates a new merge commit M with two parents. Nothing that existed before is changed.

  • Pros: safe, never rewrites history, shows exactly when branches came together.
  • Cons: frequent merges from main fill the log with "Merge branch 'main'" commits and make it harder to read.

Option 2: Rebase#

Bash
git switch feature
git rebase main
After git rebase: the feature commits are replayed as new commits C' and D' on top of F, giving one straight line
Text
                  C'--D'   feature
                 /
A---B---E---F            main

Git takes your commits C and D, sets them aside, moves your branch to the tip of main, and replays them one by one. The result is new commits C' and D': same changes, new commit IDs.

  • Pros: a clean, straight-line history that reads like a story.
  • Cons: it rewrites commits. If someone else already pulled C and D, their copy and yours now disagree.

The golden rule of rebasing#

Warning

Never rebase commits that other people have based work on. Rebase your own local or personal branch freely. Never rebase main or a shared branch that teammates pull from.

After rebasing a branch you had already pushed, a normal push is rejected, because the history changed. Use:

Bash
git push --force-with-lease

--force-with-lease refuses to overwrite the remote if someone else pushed to it in the meantime. Prefer it over plain --force, which overwrites blindly.

Resolving conflicts#

MergeRebase
How oftenOnce, for all changesPossibly once per commit
Continuegit commitgit rebase --continue
Give upgit merge --abortgit rebase --abort

During a rebase, fix the conflicted files, git add them, then run git rebase --continue. If it gets messy, git rebase --abort puts everything back exactly as it was.

Interactive rebase: tidy up before review#

Interactive rebase lets you rewrite your own recent commits before anyone sees them:

Bash
git rebase -i HEAD~4
Text
pick   a1b2c3 Add login form
squash d4e5f6 fix typo
reword 7a8b9c Add validaton
drop   0c1d2e debug logs
  • pick: keep the commit
  • squash / fixup: fold it into the previous commit
  • reword: change the message
  • drop: delete it

Turning "WIP", "fix" and "fix again" into one clear commit makes your pull request much easier to review.

A safe team workflow#

  1. Create a short-lived branch from an up-to-date main.
  2. While working, update it with git pull --rebase origin main (or git fetch then git rebase origin/main).
  3. Before opening the pull request, tidy commits with git rebase -i.
  4. Push with --force-with-lease if you rewrote an already-pushed branch.
  5. Merge into main through the pull request, using your team's chosen strategy.

Merge strategies on GitHub and GitLab#

ButtonResult on main
Create a merge commitAll branch commits plus a merge commit
Squash and mergeOne single commit for the whole PR
Rebase and mergeBranch commits replayed in a straight line

Many teams pick squash and merge: one PR becomes one commit, which keeps main readable and easy to revert.

Tip

Lost commits after a bad rebase? git reflog lists every position your branch has been in. Find the commit before the rebase and git reset --hard <id> to go back.

Key takeaways#

  • Merge adds a merge commit and never rewrites history.
  • Rebase replays your commits on top of another branch, giving a straight history with new commit IDs.
  • Only rebase commits nobody else depends on, and push with --force-with-lease.
  • Use interactive rebase to clean up commits before review, and git reflog to recover from mistakes.