Learn

Merge or rebase

Git

Two ways to combine two branches, two different histories. Choose based on context.

When you want to bring changes from one branch into another, Git offers two tools: merge and rebase. They produce different histories, with different trade-offs.

Merge: preserve the real history

git merge takes both branches and creates a merge commit that unites their histories. The graph stays faithful to reality: you can see exactly when the branch diverged and when it was integrated.

shell
git switch main
git merge my-feature

The result in git log --graph looks like two parallel lines converging into one point. It is readable, traceable, and non-destructive.

Rebase: linearize the history

git rebase replays your commits on top of another branch. The result: a perfectly linear history, as if you had always worked after the latest commits on main.

shell
git switch my-feature
git rebase main

Git moves each commit from my-feature one by one, replaying them after the last commit on main. The history becomes a straight line, without a merge commit.

Quick comparison

MergeRebase
HistoryPreserved, with merge commitLinearized, commits rewritten
Safe on shared branchYesNo
Graph readabilityShows real branchesClean and linear

When to use which

  • Merge: to integrate a feature into main, to keep a clear trace of where things came from. The default choice in a collaborative context.
  • Rebase: to clean up the history of a local branch before opening a pull request. You "rebase onto main" so your PR applies cleanly on top of the latest code.

A common workflow: you work on my-feature, run git rebase main to stay up to date without a spurious merge commit, then open a PR. GitHub then merges with an official merge commit.

git-scm.com - git merge git-scm.com - git rebase

Check off steps to unlock what comes next

Back to course