git rebase

Branching and Merging

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)
[ ] optional| or<arg> placeholder... repeatable--flag option

Options

Click any option to expand

Beginner (3)

Resume rebasing after resolving merge conflicts. Run this after staging the resolved files with git add.

RecommendedThe normal way to resume after resolving and staging conflicts

Example

git rebase --continue

Cancel the rebase and return to the original state before rebase started. Use when conflicts are too complex or you changed your mind.

RecommendedSafe escape hatch: restores your branch to its pre-rebase state

Example

git rebase --abort

Skip the current commit and continue with the next one. Use when a commit becomes empty or its changes are no longer needed.

When neededDrops the current commit — only when its changes are truly unwanted

Example

git rebase --skip

Intermediate (3)

Open an editor to reorder, squash, edit, or drop commits before replaying them. The most powerful way to clean up commit history.

RecommendedBest way to tidy local commits before sharing them

Example

git rebase -i HEAD~3

Automatically stash uncommitted changes before rebase and restore them after. Saves you from manually stashing.

When neededHandy with a dirty tree, but re-applying the stash can conflict

Example

git rebase --autostash main

Force rebasing even when the branch is up-to-date. Creates new commits with new hashes for all commits in the range.

When neededOnly when you need new hashes for already up-to-date commits

Example

git rebase -f main

Advanced (5)

Transplant commits to a different base. Allows you to rebase commits from one branch onto another specific commit.

When neededMoves a precise commit range onto a new base — double-check the range

Example

git rebase --onto main feature~3 feature

Automatically move and squash fixup!/squash! commits (made with git commit --fixup or --squash) into their targets. Works with or without -i since Git 2.44.

RecommendedPairs with commit --fixup for painless history cleanup

Example

git rebase -i --autosquash main

Preserve merge commits and branching structure during rebase. Recreates the merge topology instead of flattening history.

When neededWhen a branch's merge structure must survive the rebase

Example

git rebase -r main

Execute a shell command after each commit is applied. Useful for running tests to ensure each commit builds.

When neededRuns tests on every replayed commit; slow on long branches

Example

git rebase -i -x 'npm test' main

Rebase all commits reachable from HEAD, including the root commit. Allows you to rewrite the entire history.

Not recommendedRewrites the whole history from the first commit; rarely needed

Example

git rebase -i --root

Choosing the Right Option

Comparing git rebase with git merge

Understanding when to use rebase vs merge for integrating changes

CommandAspectGit RebaseGit Merge
HistoryLinear (rewrites commits)Preserves divergent history
CommitsCreates NEW commits with different SHAsPreserves original commits + merge commit
DestructiveYes - changes commit hashesNo - only adds merge commit
ConflictsResolved per-commit during replayResolved once at merge point
Best forLocal/private branches before pushPublic/shared branches
TraceabilityHarder to see integration pointsClear record of branch integration

Workflow Examples

1
git fetch origin

Get latest changes from remote

2
git checkout feature

Switch to your feature branch

3
git rebase origin/main

Replay your commits on top of latest main

4
git push --force-with-lease

Push rebased branch (only if not shared)

1
git log --oneline

Count how many commits to squash

2
git rebase -i HEAD~5

Interactive rebase last 5 commits

3
(in editor) pick, squash, squash...

Keep first, squash rest into it

4
(edit combined message)

Write clean commit message

1
(make your fix)

Fix the issue in the code

2
git commit --fixup=<commit-hash>

Create a fixup commit for the target

3
git rebase --autosquash main

Autosquash arranges and squashes automatically (no -i needed since Git 2.44)

1
git rebase -i HEAD~4

Interactive rebase last 4 commits

2
(in editor) reorder lines

Move commit lines to new positions

3
save and close editor

Git replays in new order

1
git rebase -i HEAD~3

Start interactive rebase

2
(mark commit with 'edit')

Stop at the commit to split

3
git reset HEAD^

Undo commit, keep changes unstaged in working directory

4
git add file1 && git commit -m 'Part 1'

Create first smaller commit

5
git add file2 && git commit -m 'Part 2'

Create second smaller commit

6
git rebase --continue

Continue with remaining commits

1
git reflog

Find the commit before rebase

2
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 --continue

Cannot rebase: You have unstaged changes

Cause: Working directory has uncommitted modifications

Commit, stash, or use --autostash to handle uncommitted changes

git rebase --autostash main

Commit 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 --skip

Rebase results in many conflicts

Cause: Branch has diverged significantly from base

Consider using merge instead, or abort and rebase in smaller steps

git rebase --abort

Lost 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-lease

git 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 main

Interactive 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..HEAD

Tips & 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

A:

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.

A:

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.

A:

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.

A:

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.

A:

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.

A:

--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.

A:

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.

A:

--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.

A:

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:

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.