git rebase
Reapply commits on top of another base tip
git rebase moves or combines a sequence of commits to a new base commit. It's commonly used to maintain a linear project history by replaying feature branch commits on top of the latest main branch. Unlike merge, rebase rewrites history by creating new commits with different SHAs. Interactive rebase (-i) allows you to squash, reorder, edit, or drop commits before they're replayed.
Synopsis
git rebase [-i | --interactive] [<options>] [--exec <cmd>] [--onto <newbase> | --keep-base] [<upstream> [<branch>]]
git rebase [-i | --interactive] [<options>] [--exec <cmd>] [--onto <newbase>] --root [<branch>]
git rebase (--continue | --skip | --abort | --quit | --edit-todo | --show-current-patch)Options
Choosing the Right Option
Comparing git rebase with git merge
Understanding when to use rebase vs merge for integrating changes
| Command | Aspect | Git Rebase | Git Merge |
|---|---|---|---|
History | Linear (rewrites commits) | Preserves divergent history | |
Commits | Creates NEW commits with different SHAs | Preserves original commits + merge commit | |
Destructive | Yes - changes commit hashes | No - only adds merge commit | |
Conflicts | Resolved per-commit during replay | Resolved once at merge point | |
Best for | Local/private branches before push | Public/shared branches | |
Traceability | Harder to see integration points | Clear record of branch integration |
Workflow Examples
git fetch originGet latest changes from remote
git checkout featureSwitch to your feature branch
git rebase origin/mainReplay your commits on top of latest main
git push --force-with-leasePush rebased branch (only if not shared)
git log --onelineCount how many commits to squash
git rebase -i HEAD~5Interactive rebase last 5 commits
(in editor) pick, squash, squash...Keep first, squash rest into it
(edit combined message)Write clean commit message
(make your fix)Fix the issue in the code
git commit --fixup=<commit-hash>Create a fixup commit for the target
git rebase --autosquash mainAutosquash arranges and squashes automatically (no -i needed since Git 2.44)
git rebase -i HEAD~4Interactive rebase last 4 commits
(in editor) reorder linesMove commit lines to new positions
save and close editorGit replays in new order
git rebase -i HEAD~3Start interactive rebase
(mark commit with 'edit')Stop at the commit to split
git reset HEAD^Undo commit, keep changes unstaged in working directory
git add file1 && git commit -m 'Part 1'Create first smaller commit
git add file2 && git commit -m 'Part 2'Create second smaller commit
git rebase --continueContinue with remaining commits
git reflogFind the commit before rebase
git reset --hard HEAD@{n}Reset to pre-rebase state
Troubleshooting
CONFLICT during rebase
Cause: Changes in your commit conflict with the base branch
Edit files to resolve conflicts, git add them, then git rebase --continue
git add <resolved-files> && git rebase --continueCannot rebase: You have unstaged changes
Cause: Working directory has uncommitted modifications
Commit, stash, or use --autostash to handle uncommitted changes
git rebase --autostash mainCommit becomes empty after rebase
Cause: The changes in this commit already exist in the target branch
A plain rebase drops it automatically; interactive rebase stops - run --skip to drop it, or 'git commit --allow-empty' to keep it
git rebase --skipRebase results in many conflicts
Cause: Branch has diverged significantly from base
Consider using merge instead, or abort and rebase in smaller steps
git rebase --abortLost commits after rebase
Cause: Original commits are orphaned (not deleted, just unreferenced)
Use reflog to find and recover original commits
git reflog && git branch recovery HEAD@{n}Push rejected (non-fast-forward) after rebase
Cause: Rebase rewrote commits, so your branch no longer contains the remote copy's history
Use --force-with-lease for a safer force push (it refuses if someone else pushed meanwhile)
git push --force-with-leasegit rebase says 'Current branch is up to date'
Cause: No commits to replay - branch already based on target
Use -f to force rebase anyway if you need new hashes
git rebase -f mainInteractive rebase todo list is empty
Cause: No commits in the specified range
Check your range - use HEAD~n or specify correct upstream
git log --oneline main..HEADTips & Pitfalls
Best Practices
- ✓Never rebase commits that have been pushed to a shared repository - this forces everyone to manually fix their branches
- ✓Always use 'git rebase --abort' if conflicts become too complex - you can try a different approach
- ✓Use interactive rebase (-i) to clean up commits before pushing or creating a pull request
- ✓Consider using '--autostash' to automatically handle uncommitted changes during rebase
- ✓Use 'git commit --fixup=<hash>' with '--autosquash' for a cleaner fixup workflow
- ✓Run tests after rebase to ensure the rebased commits still work correctly
- ✓Use '-x npm test' to automatically run tests after each replayed commit
- ✓Keep rebase operations small - rebase frequently rather than letting branches diverge significantly
- ✓Use 'git reflog' to recover if a rebase goes wrong - original commits are never immediately deleted
- ✓Prefer merge over rebase for public/shared branches to preserve history
- ✓Use '--force-with-lease' instead of '--force' when pushing rebased branches for safety
- ✓When squashing, write a new descriptive commit message that summarizes all squashed commits
Common Mistakes
- ✗Rebasing commits that have already been pushed to a shared repository. This rewrites history and forces everyone else to manually fix their branches.
- ✗Forgetting to stage resolved conflicts before running --continue. Always run 'git add' on resolved files first.
- ✗Using --skip carelessly, which drops the commit's changes from the rebased branch. Only use when intentionally removing a commit.
- ✗Not understanding that rebase creates NEW commits with different hashes. The original commits become orphaned.
- ✗Rebasing with uncommitted changes in the working directory. Use --autostash or manually stash first.
FAQ
Merge preserves history by creating a merge commit, keeping both branch histories intact. Rebase rewrites history by replaying your commits on top of another branch, creating a linear history. Use merge for shared branches, rebase for local cleanup.
Rebasing changes commit hashes. If others have based work on your original commits, their history will diverge from yours. They'll need to manually fix their branches, which is error-prone and frustrating.
Use 'git reflog' to find the commit before the rebase (look for the old branch tip), then 'git reset --hard HEAD@{n}' to restore it. By default, the original commits stay reachable from the reflog for 30 days. Right after a rebase, 'git reset --hard ORIG_HEAD' also works.
In interactive rebase: 'pick' keeps the commit as-is, 'squash' combines it with the previous commit (keeping both messages), 'fixup' combines but discards the message, 'reword' keeps the commit but lets you edit the message.
Run 'git rebase -i HEAD~n' where n is the number of commits. In the editor, keep 'pick' on the first commit and change the rest to 'squash' or 'fixup'. Save and edit the combined commit message.
--onto lets you transplant commits to a different base. For example, 'git rebase --onto main feature~3 feature' moves only the last 3 commits of feature onto main, skipping earlier commits.
Edit the conflicted files to resolve them, run 'git add' on resolved files, then 'git rebase --continue'. If it's too messy, 'git rebase --abort' returns you to the pre-rebase state.
--preserve-merges was removed in Git 2.34 (git now fails with '--preserve-merges was replaced by --rebase-merges'). Use --rebase-merges (-r) instead to preserve merge commits and branch structure during rebase. It recreates the merge topology on top of the new base.
Yes, use 'git rebase -i' and mark commits you want to keep with 'pick' and commits you want to remove with 'drop'. Or use --onto to specify exactly which range of commits to move.
A commit's hash is calculated from its content, message, author, date, AND parent commit. Since rebase changes the parent commit, all subsequent commits get new hashes even if their content is identical.