iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

Interactive Rebase

`git rebase -i` is the time machine: pick, reword, edit, squash, fixup, drop, or reorder past commits before pushing. Use it to polish a branch into a clean history that reads like the work was right the first time. After the branch is public, prefer fixup commits + autosquash to avoid rewriting shared history.

pick / squash / fixup / reword / autosquash patterns

EXAMPLE
# 1) Start an interactive rebase against the parent
git rebase -i main           # if your feature branched off main
git rebase -i HEAD~5         # last 5 commits

# Git opens an editor with one line per commit:
# pick 9fceb02 feat: add cart
# pick a1b2c3d wip
# pick e4f5g6h fix typo
# pick 7890abc refactor: extract item row
# pick 0fedcba feat: add quantity stepper

# ===== Operations =====
# pick    keep as-is
# reword  keep diff; change the message
# edit    pause AT this commit so you can amend (add files, split, etc.)
# squash  fold into previous commit; messages combine
# fixup   fold into previous commit; DISCARD this commits message
# drop    remove the commit entirely
# exec    run a shell command between commits
# break   pause; run 'git rebase --continue' when ready

# Reorder lines to change the commit order.

# ===== Common recipes =====

# a) Squash three WIP commits into one named release
# pick 9fceb02 feat: cart
# fixup a1b2c3d wip
# fixup e4f5g6h fix typo
# Save & quit.

# b) Reword a sloppy message
# reword 7890abc refactor: extract item row
# Save & quit; another editor opens for the new message.

# c) Drop a junk commit
# drop e4f5g6h fix typo

# d) Split one big commit into two
# edit 9fceb02 feat: cart
# Save & quit; rebase pauses there.
# git reset HEAD~                 # uncommit but keep changes
# git add cart/cart.tsx
# git commit -m 'feat: cart shell'
# git add cart/quantity.tsx
# git commit -m 'feat: quantity stepper'
# git rebase --continue

# e) Reorder commits
# Swap the lines in the editor. Conflicts? Resolve, then:
# git add path && git rebase --continue

# ===== Autosquash — the safer workflow =====
# Make small 'fixup' commits during development:
git commit --fixup=<sha-of-original>     # message becomes 'fixup! ...'

# When ready to clean up:
git rebase -i --autosquash main
# Lines pre-marked as 'fixup' fold into the right places. Just save.

# ===== When rebase is dangerous =====
# - You ALREADY pushed and other people have based work on the branch
# - You rebased main itself (NEVER do this)
# - You rebased a long-lived shared branch (e.g. 'develop')
# After rebase you must 'force-push' to the remote; use --force-with-lease:
git push --force-with-lease

# ===== Aborting mid-rebase =====
git rebase --abort           # back to where you started, untouched
git rebase --skip            # skip the current commit (already-applied)
git rebase --continue        # resume after fixing conflicts

# ===== Tools that help =====
# Magit (Emacs)    visual rebase
# git-gui          basic operations
# Lazygit          terminal UI; 's' = squash, 'F' = fixup, 'e' = edit
# IDE built-ins    VS Code (GitLens), JetBrains, Sublime Merge

# ===== Discipline =====
# - Rebase LOCAL history; merge PUBLIC history
# - One commit = one logical change; no 'wip' in the final tree
# - 'feat:' / 'fix:' / 'refactor:' / 'chore:' prefixes pair well with
#   automated changelogs (see cicd/release-notes lesson)

# ===== Recovery =====
# After a botched rebase:
git reflog                  # find where HEAD was BEFORE the rebase
git reset --hard HEAD@{N}   # back to that snapshot

Why it matters

Use `git commit --fixup` while coding and `git rebase -i --autosquash` once before opening the PR. The final history reads as if you got it right the first time, your teammates only see clean commits, and you never have to remember which lines to mark as fixup in the rebase editor — Git fills them in for you.

Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.

Example

Example
git rebase -i HEAD~5
# pick / squash / fixup / reword / drop — clean up before pushing
Try it Yourself »

Discussion

Loading…