stackademic

The leading education platform for anyone with an interest in software development.

Git Rebase

Replay commits on top of another branch for a linear history

Overview

Rebase moves your branch commits to start from a new base—typically the latest main. Unlike merge, rebase rewrites commit history. Use it to update feature branches before merging; avoid rebasing shared public branches.

Syntax / Usage

# Update feature branch onto latest main
git switch feature/api
git fetch origin
git rebase origin/main

# Interactive rebase — squash, reword, reorder
git rebase -i HEAD~3

# Continue after resolving conflicts
git add resolved-file.ts
git rebase --continue

# Abort
git rebase --abort

# After rebase, force push feature branch (your branch only!)
git push --force-with-lease

Examples

Squash WIP commits before PR:

git rebase -i origin/main
# Mark commits as 'squash' or 'fixup' in the editor

Keep a clean log:

git switch feature/search
git fetch origin
git rebase origin/main
# resolve conflicts if any
git push --force-with-lease

Common Mistakes

  • Rebasing main or branches others have pulled—rewrites shared history
  • Using --force instead of safer --force-with-lease
  • Forgetting that rebase creates new commit SHAs—open PRs may need force push
  • Rebasing merge commits without understanding --rebase-merges

See Also

merge branch commit remote