EMZETT.
Login

Git rebase

In short: git rebase replays the commits of a branch onto a new base. The result is a linear history without extra merge commits.

In more detail: Instead of joining two branches with a merge commit, rebase sets your commits aside, moves the base to the latest state, and replays your commits one by one. The commits get new hashes because their parent changes.

In Depth

  • Typical use: git rebase main on a feature branch so that it contains the latest changes from main.
  • Interactive: git rebase -i HEAD~5 lets you squash, reword, reorder or drop commits.
  • Golden rule: Never rewrite commits that others already have (public branches). Otherwise the histories diverge.
  • Conflicts: They appear commit by commit. After resolving, run git rebase --continue; to abort, git rebase --abort.
  • Merge or rebase: Merge preserves the real history, rebase makes it readable. Many teams rebase locally and merge when integrating.

See also: Merge conflicts