How to rebase in Git

· Category: Git

Short answer

Use git rebase <base-branch> to replay your current branch's commits on top of another branch.

Steps

  1. Rebase your feature branch onto main:
git checkout feature
git rebase main
  1. Interactive rebase to squash or edit commits:
git rebase -i HEAD~3
  1. Abort if things go wrong:
git rebase --abort

Tips

  • Rebase before merging to create a linear history.
  • Interactive rebase (-i) is powerful for cleaning up commits before sharing.
  • Never rebase commits that have already been pushed to a shared branch.

Common issues

  • Conflicts during rebase must be resolved one commit at a time.
  • Accidental rebase on shared history can cause duplicate commits for collaborators.