Rebase a branch onto latest main

Medium risk
What do risk levels mean?
Read-only
Inspects state without changing anything.
Low risk
Reversible with a routine follow-up command.
Medium risk
Changes state; undo path documented.
Destructive
Deletes or overwrites; confirmation required.

Replays the current branch's commits on top of the latest default branch, producing a linear history and surfacing conflicts before merging.

gitrebasebranch

Parameters

Branch to rebase onto.

Commands

Confirm the working tree is clean before rebasing

git status --short

Download the latest state of the base branch

git fetch origin

Replay this branch's commits on top of the updated base

git rebase origin/main

Verification

git log --oneline --graph -10

The branch's commits sit linearly on top of the base branch tip.

Undo

Restore the branch to its pre-rebase state

git reset --hard ORIG_HEAD

Pitfalls

  • Conflicts pause the rebase; fix files, git add them, then git rebase --continue, or bail out with git rebase --abort.
  • Rebasing rewrites commits; pushing afterwards needs a force push if the branch was already published.
  • Do not rebase a branch other people are building on.

Related