5.5 KiB
Module 08: Interactive Rebase
Learning Objectives
By the end of this module, you will:
- Understand what interactive rebase is and when to use it
- Learn the different interactive rebase commands (pick, reword, squash, fixup, drop)
- Clean up commit history by squashing related commits
- Reword commit messages to be more descriptive
- Use reset and commit techniques to achieve similar results
Challenge Description
You have a feature branch with messy commit history - multiple "WIP" commits, typos in commit messages, and commits that should be combined. Before submitting a pull request, you want to clean up this history.
Your task is to:
- Review the current commit history with its messy commits
- Combine the four feature commits into a single, well-described commit
- Keep the initial commit unchanged
- End up with a clean, professional commit history
Key Concepts
What is Interactive Rebase?
Interactive rebase (git rebase -i) is a powerful tool that lets you modify commit history. Unlike regular rebase which simply moves commits, interactive rebase lets you:
- Reorder commits
- Edit commit messages (reword)
- Combine multiple commits (squash/fixup)
- Split commits into smaller ones
- Delete commits entirely
- Edit the contents of commits
Interactive Rebase Commands
When you run git rebase -i HEAD~3, Git opens an editor with a list of commits and commands:
pick abc1234 First commit
pick def5678 Second commit
pick ghi9012 Third commit
You can change pick to other commands:
- pick: Keep the commit as-is
- reword: Keep the commit but edit its message
- edit: Pause the rebase to amend the commit
- squash: Combine this commit with the previous one, keeping both messages
- fixup: Like squash, but discard this commit's message
- drop: Remove the commit entirely
When to Use Interactive Rebase
Interactive rebase is perfect for:
- Cleaning up work-in-progress commits before creating a pull request
- Fixing typos in commit messages
- Combining related commits into logical units
- Removing debug commits or experimental code
- Creating a clean, professional history
Remember: Only rebase commits that haven't been pushed to a shared branch!
Alternative Approach: Reset and Recommit
Since interactive rebase requires an interactive editor, this challenge uses an alternative approach that achieves the same result:
-
Reset soft: Move HEAD back while keeping changes staged
git reset --soft HEAD~4This keeps all changes from the last 4 commits but "uncommits" them.
-
Create a new commit: Commit all changes with a clean message
git commit -m "Your new clean commit message"
This technique is useful when you want to combine multiple commits into one without using interactive rebase.
Useful Commands
# View commit history
git log --oneline
# See the last N commits in detail
git log -n 5
# Reset to N commits back, keeping changes staged
git reset --soft HEAD~N
# Reset to N commits back, keeping changes unstaged
git reset --mixed HEAD~N
# Reset to N commits back, discarding all changes (DANGEROUS!)
git reset --hard HEAD~N
# Create a new commit
git commit -m "message"
# Amend the last commit
git commit --amend -m "new message"
# Interactive rebase (requires interactive editor)
git rebase -i HEAD~N
# Check current status
git status
Verification
Run the verification script to check your solution:
.\verify.ps1
The verification will check that:
- You have exactly 2 commits total (initial + your combined feature commit)
- The feature commit contains all the changes from the original 4 feature commits
- The commit message is clean and descriptive
- All expected files are present
Challenge Steps
- Navigate to the challenge directory
- View the current messy commit history:
git log --oneline - Use
git reset --soft HEAD~4to uncommit the last 4 commits while keeping changes - Check status with
git status- you should see all changes staged - Create a new commit with a clean message:
git commit -m "Add user profile feature with validation" - Verify with
git log --oneline- you should now have clean history - Run the verification script
Tips
- Always check your history with
git log --onelinebefore and after git reset --softis safer than--hardbecause it keeps your changes- You can use
git reset --soft HEAD~Nwhere N is the number of commits to undo - If you make a mistake, run
.\reset.ps1to start over - In real projects with interactive rebase, you would use
git rebase -i HEAD~Nand modify the commit list in the editor
What is Git Reset?
git reset moves the current branch pointer to a different commit:
- --soft: Moves HEAD but keeps changes staged
- --mixed (default): Moves HEAD and unstages changes, but keeps them in working directory
- --hard: Moves HEAD and discards all changes (dangerous!)
Reset is useful for:
- Undoing commits while keeping changes (
--soft) - Unstaging files (
--mixed) - Discarding commits entirely (
--hard)
What You'll Learn
Cleaning up commit history is an important skill for professional development. Whether you use interactive rebase or reset-and-recommit, the goal is the same: create a clear, logical history that makes your code review easier and your project history more understandable. Messy WIP commits are fine during development, but cleaning them up before merging shows attention to detail and makes your codebase more maintainable.