🌿 Branching

How to rebase a branch in Git

Reapply commits on top of another branch for a linear history.

Rebase current branch onto main
git rebase main

Replays all commits from the current branch on top of main. Creates a linear history.

When to use: Before merging a feature branch to bring it up to date with main.

Interactive rebase (last N commits)
git rebase -i HEAD~3

Opens an editor to rewrite the last 3 commits. You can squash, reword, drop or reorder.

When to use: Cleaning up commit history before a pull request.

Abort a rebase in progress
git rebase --abort

Cancels the rebase and returns to the original state.

Continue after resolving conflicts
git rebase --continue

After resolving conflicts and staging the fixes, continue the rebase.

← Back to Git Reference