Git Rebase Tutorial — Interactive Rebase Explained
In this tutorial, you'll learn about Git Rebase Tutorial. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.
What You'll Learn
Master Git rebase — basic rebase to update branches, interactive rebase to clean up history, squash commits, reword messages, and resolve rebase conflicts.
Why It Matters
A clean commit history makes code review easier, debugging faster, and collaboration smoother. Interactive rebase is your cleanup tool.
Real-World Use
Squashing 10 "WIP" commits into one meaningful commit before a PR, reordering commits to group related changes, or editing a commit message that has a typo.
Basic Rebase
Update your feature branch with the latest main:
# On feature branch
git checkout feature/login
# Rebase onto main
git rebase main
# Before:
# A---B---C feature
# /
# D---E---F main
# After:
# A'---B'---C' feature
# /
# D---E---F main
Interactive Rebase
git rebase -i HEAD~5
This opens an editor with the last 5 commits:
pick a1b2c3 Add login form
pick d4e5f6 Fix CSS on login
pick g7h8i9 Add validation
pick j1k2l3 Fix typo in validation
pick m4n5o6 Add tests for login
Available actions:
| Command | Effect |
|---|---|
pick |
Use the commit as-is |
reword |
Change commit message |
edit |
Stop to amend the commit |
squash |
Combine with previous commit |
fixup |
Like squash but discard message |
exec |
Run a command |
break |
Stop here |
drop |
Remove the commit |
reorder |
Move lines to reorder |
Squashing Commits
Combine related commits into one:
pick a1b2c3 Add login form
squash d4e5f6 Fix CSS on login ← Merged into previous
squash g7h8i9 Add validation ← Merged into previous
pick j1k2l3 Add tests for login
# After squash:
# a1b2c3 Add login form + Fix CSS + Add validation
# j1k2l3 Add tests for login
The editor opens again for the combined commit message.
Rewording a Commit
pick a1b2c3 Add login form
reword d4e5f6 Fix CSS on login ← Editor opens to change message
Reordering Commits
Reorder lines to change commit order — useful for grouping:
# Before:
pick a1b2c3 Add login form
pick j1k2l3 Add tests for login
pick d4e5f6 Fix CSS on login
# After reorder:
pick a1b2c3 Add login form
pick d4e5f6 Fix CSS on login ← Moved up
pick j1k2l3 Add tests for login ← Moved down after fix
Splitting a Commit
# Mark the commit as "edit"
pick a1b2c3 Add login form → edit a1b2c3 Add login form
# Git stops after applying this commit
# Now unstage changes and create separate commits:
git reset HEAD^
git add form.html
git commit -m "Add login form HTML"
git add login.js
git commit -m "Add login form JS"
git rebase --continue
Resolving Rebase Conflicts
When rebase hits a conflict:
Auto-merging login.py
CONFLICT (content): Merge conflict in login.py
error: could not apply d4e5f6... Add validation
Resolve all conflicts, then run 'git rebase --continue'
# 1. Fix the conflict in login.py
# 2. Stage the resolved file
git add login.py
# 3. Continue rebase
git rebase --continue
# 4. Or skip this commit
git rebase --skip
# 5. Or abort entirely
git rebase --abort
Using Reflog for Safety
# If rebase goes wrong, use reflog
git reflog
# Find your pre-rebase position
git reset --hard HEAD@{5}
When NOT to Rebase
❌ Never rebase commits that exist on a shared branch
❌ Never rebase a branch that others have pulled
❌ Never rebase if you're not sure (merge instead)
Commit hashes change after rebase. If someone else has your old commits, their history will diverge from yours.
Practice
# 1. Create a branch with 5 messy commits
git checkout -b messy-feature
echo "a" > file.txt && git add . && git commit -m "WIP 1"
echo "b" >> file.txt && git add . && git commit -m "WIP 2"
echo "c" >> file.txt && git add . && git commit -m "WIP 3"
echo "d" >> file.txt && git add . && git commit -m "WIP 4"
# 2. Interactive rebase to squash
git rebase -i HEAD~4
# Change pick to squash for commits 2-4
# 3. Result: one commit with all work
git log --oneline
# a1b2c3 WIP 1 + WIP 2 + WIP 3 + WIP 4
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro