Git

Rebasing

Git · 8 entries

Rebase onto Another Branch

syntax
git rebase <base-branch>
example
# While on feature/payments:
git switch feature/payments
git rebase main
output
Successfully rebased and updated refs/heads/feature/payments.

Note Rebase replays your branch's commits on top of the target branch, creating a linear history. NEVER rebase commits that have been pushed to a shared branch — it rewrites commit hashes and will cause chaos for collaborators.

Interactive Rebase

syntax
git rebase -i HEAD~<n>
git rebase -i <commit>
example
# Rewrite the last 4 commits:
git rebase -i HEAD~4

# Editor opens with:
# pick a1b2c3d Add login page
# pick d4e5f6g Add signup page
# pick h7i8j9k Fix typo in login
# pick l0m1n2o Update styles
#
# Change 'pick' to: squash, reword, edit, drop, etc.

Note Commands: pick (keep as-is), reword (change message), squash (merge into previous commit), fixup (squash but discard message), edit (pause to modify), drop (delete commit). Save and close the editor to execute.

Squash Multiple Commits into One

syntax
git rebase -i HEAD~<n>
# Change 'pick' to 'squash' or 'fixup' for commits to merge
example
git rebase -i HEAD~3

# In the editor, change to:
# pick a1b2c3d Add user profile feature
# squash d4e5f6g Fix profile image upload
# squash h7i8j9k Add profile validation

# Save — a new editor opens to combine commit messages
output
[detached HEAD x1y2z3w] Add user profile feature
 Date: Mon Mar 30 14:22:01 2026 +0000
 3 files changed, 120 insertions(+), 15 deletions(-)

Note Use 'squash' to combine commit messages. Use 'fixup' (or just 'f') to discard the squashed commit's message entirely. Great for cleaning up WIP commits before merging to main.

Rebase onto a Different Base

syntax
git rebase --onto <new-base> <old-base> <branch>
example
# Move feature/settings from feature/auth onto main:
git rebase --onto main feature/auth feature/settings

Note This is for when you branched off the wrong branch. It takes the commits unique to <branch> (after <old-base>) and replays them onto <new-base>. Visualize it as transplanting a range of commits.

Abort a Rebase in Progress

syntax
git rebase --abort
example
# Things went wrong during rebase — bail out:
git rebase --abort

Note Restores your branch to exactly how it was before the rebase started. Safe to run at any point during a rebase. No work is lost.

Continue Rebase After Resolving Conflict

syntax
git rebase --continue
example
# After resolving conflicts in a file:
git add src/auth.js
git rebase --continue

Note When a conflict occurs during rebase, resolve it, stage the file with git add, then continue. Do NOT commit — rebase --continue handles the commit for you.

Skip a Conflicting Commit During Rebase

syntax
git rebase --skip
example
git rebase --skip

Note Drops the current conflicting commit entirely and moves to the next one. Use this when the conflict is caused by a commit that's no longer needed (e.g., it was already applied upstream).

Autosquash with Fixup Commits

syntax
git commit --fixup=<commit>
git rebase -i --autosquash <base>
example
# Make a commit that will auto-squash into a1b2c3d:
git commit --fixup=a1b2c3d

# Later, rebase with autosquash:
git rebase -i --autosquash main

Note Git automatically reorders and marks fixup commits to be squashed into their target. The commit message starts with 'fixup!' followed by the target's message. Set rebase.autoSquash=true in config to always enable this.