Git/
Lesson

Interactive rebaseWhat is rebase?A Git operation that replays your branch's commits on top of another branch's latest commit, creating a linear history instead of a merge commit. is the editing suite for your commitWhat is commit?A permanent snapshot of your staged changes saved in Git's history, identified by a unique hash and accompanied by a message describing what changed. history. Regular rebase replays your commits automatically. Interactive rebase pauses and asks you what to do with each one: keep it, change its message, combine it with another, delete it, or reorder it. It is the tool that turns a messy series of "WIP" commits into a clean, professional history.

Think of it like editing a draft. Your first pass is rough: typos, reorganizations, false starts. Interactive rebase lets you polish everything before anyone else sees it. The final version reads like you wrote it perfectly the first time.

AI coding assistants generate commits as you work, and the result is often a messy history: "WIP", "fix typo", "debugging console.log", "another fix". Interactive rebase is how you clean up that mess before submitting a PR. AI won't do this step for you.

Starting an interactive rebaseWhat is rebase?A Git operation that replays your branch's commits on top of another branch's latest commit, creating a linear history instead of a merge commit.

# Modify last 3 commits
git rebase -i HEAD~3

# Modify all commits since branching from main
git rebase -i main

Your default text editor opens with a list of commits:

pick abc1234 Add login form
pick def5678 WIP: fix typo in validation
pick ghi9012 WIP: debugging auth
pick jkl3456 Finalize login feature

# Commands:
# p, pick   = use commit
# r, reword = use commit, but edit the commit message
# s, squash = use commit, but meld into previous commit
# f, fixup  = like squash, but discard this commit's log message
# d, drop   = remove commit
# e, edit   = use commit, but stop for amending

Change the action word at the start of each line, save, and close the editor. Git executes your instructions top to bottom.

02

The six actions

ActionWhat it doesWhen to use
pickKeep the commit as-isDefault, for commits that are already good
rewordKeep the commit, edit its messageFix typos or improve descriptions
squashCombine with previous commit, edit combined messageMerge related commits together
fixupCombine with previous commit, discard this messageClean up "WIP" and "fix" commits
dropDelete the commit entirelyRemove accidental or unwanted commits
editPause at this commit for modificationsSplit a commit or change its content
03

Practical examples

Cleaning up WIP commits

The most common use case. You have messy commits from a coding sessionWhat is session?A server-side record that tracks a logged-in user. The browser holds only a session ID in a cookie, and the server looks up the full data on each request.:

pick abc1234 Add authentication system
pick def5678 WIP: fix typo
pick ghi9012 WIP: debugging console.log
pick jkl3456 WIP: another fix
pick mno7890 Finalize auth tests

Change to:

pick abc1234 Add authentication system
fixup def5678 WIP: fix typo
fixup ghi9012 WIP: debugging console.log
fixup jkl3456 WIP: another fix
pick mno7890 Finalize auth tests

Result: 2 clean commits instead of 5 messy ones. The three WIP commits are absorbed into "Add authenticationWhat is authentication?Verifying who a user is, typically through credentials like a password or token. system" with their messages discarded.

Use fixup instead of squash when you do not care about the WIP commit messages. squash opens an editor to combine the messages; fixup silently uses the previous commit's message.

Fixing a commitWhat is commit?A permanent snapshot of your staged changes saved in Git's history, identified by a unique hash and accompanied by a message describing what changed. message

You wrote "ad login" instead of "add login form":

git rebase -i HEAD~3

Change:

pick abc1234 ad login

To:

reword abc1234 ad login

Save and close. Another editor opens where you fix the message to "feat: add login form".

Deleting an accidental commit

You committed a file with passwords:

pick abc1234 Add feature
pick def5678 Oops: committed .env file
pick ghi9012 Continue feature work

Change to:

pick abc1234 Add feature
drop def5678 Oops: committed .env file
pick ghi9012 Continue feature work

The commit and its changes are permanently removed from history.

Reordering commits

Sometimes commits make more sense in a different order:

pick abc1234 Add API endpoint
pick def5678 Add database schema

Swap them so the schemaWhat is schema?A formal definition of the structure your data must follow - which fields exist, what types they have, and which are required. comes first:

pick def5678 Add database schema
pick abc1234 Add API endpoint

Reordering can cause conflicts if later commits depend on earlier ones. Git will pause and let you resolve any issues.
04

Splitting a commitWhat is commit?A permanent snapshot of your staged changes saved in Git's history, identified by a unique hash and accompanied by a message describing what changed.

You made one big commit that should be two separate ones:

git rebase -i HEAD~2

Mark the commit as edit:

pick abc1234 First commit
edit def5678 Big commit with multiple changes

Git stops at that commit. Now split it:

# Undo the commit but keep changes in working directory
git reset HEAD~1

# Stage and commit separately
git add src/auth.js
git commit -m "feat: add authentication module"

git add tests/auth.test.js
git commit -m "test: add authentication tests"

# Continue the rebase
git rebase --continue
05

Quick reference

TaskSteps
Clean up WIP commitsgit rebase -i HEAD~n, change pick to fixup on WIP lines
Fix a commit messagegit rebase -i HEAD~n, change pick to reword
Delete a commitgit rebase -i HEAD~n, change pick to drop
Combine commitsgit rebase -i HEAD~n, change pick to squash or fixup
Split a commitgit rebase -i HEAD~n, change pick to edit, then git reset HEAD~1
Reorder commitsgit rebase -i HEAD~n, rearrange the lines