Advanced Git Rebase
Interactive rebase allows you to edit, reorder, squash, or drop commits before pushing to a remote repository. This is essential for maintaining a clean commit history.
Use Case
Use this when you have multiple WIP commits that you want to clean up before creating a pull request. It helps maintain a professional and readable git history.
Code
1# Start interactive rebase for last N commits
2git rebase -i HEAD~N
3
4# Or rebase from a specific commit
5git rebase -i <commit-hash>
6
7# Common commands in interactive rebase:
8# pick = use commit
9# reword = use commit, but edit commit message
10# edit = use commit, but stop for amending
11# squash = use commit, but meld into previous commit
12# fixup = like squash, but discard commit message
13# drop = remove commit
Explanation
Interactive rebase opens your editor with a list of commits. Each line starts with a command (default is pick). You can change these commands to manipulate your commit history.
The commits are listed from oldest to newest (opposite of git log). Changes are applied from top to bottom.
Parameters/Options
HEAD~N: Rebase the last N commits<commit-hash>: Rebase from a specific commit (not including that commit)-i, --interactive: Start interactive rebase--autosquash: Automatically squash commits marked withfixup!orsquash!--continue: Continue after resolving conflicts--abort: Cancel the rebase and return to original state
Examples
Example 1: Squash last 3 commits
1# Start interactive rebase
2git rebase -i HEAD~3
3
4# In the editor, change:
5# pick abc123 First commit
6# pick def456 Second commit
7# pick ghi789 Third commit
8#
9# To:
10# pick abc123 First commit
11# squash def456 Second commit
12# squash ghi789 Third commit
13
14# Save and close editor
15# Edit the combined commit message
16# Save and close
Output:
1Successfully rebased and updated refs/heads/main.
Example 2: Reorder commits
1git rebase -i HEAD~4
2
3# Simply reorder the lines in the editor:
4# pick abc123 Feature A
5# pick def456 Feature B
6# pick ghi789 Fix for Feature A
7# pick jkl012 Feature C
8#
9# Becomes:
10# pick abc123 Feature A
11# pick ghi789 Fix for Feature A
12# pick def456 Feature B
13# pick jkl012 Feature C
Example 3: Edit a commit message
1git rebase -i HEAD~2
2
3# Change 'pick' to 'reword':
4# reword abc123 Old message
5# pick def456 Another commit
6
7# Editor will open for you to change the message
Notes
Always create a backup branch before rebasing:
1git branch backup-branch
Interactive rebase rewrites history, so only use it on commits that haven't been pushed to a shared remote, or be prepared to force push.
Gotchas/Warnings
- ⚠️ Never rebase commits that have been pushed to a public/shared repository unless you coordinate with your team
- ⚠️ Force push required after rebase: Use
git push --force-with-lease(safer than--force) - ⚠️ Conflicts may occur: Be prepared to resolve them with
git rebase --continueor abort withgit rebase --abort - ⚠️ Lost commits: If something goes wrong, use
git reflogto find and recover lost commits