Rebase Interactive Guide
Interactive Rebase Fundamentals
Interactive rebase provides powerful tools for manipulating commit history with precision and control.
Interactive Rebase Commands
flowchart TD
A[Interactive Rebase] --> B{Possible Actions}
B --> |pick| C[Keep Commit Unchanged]
B --> |reword| D[Modify Commit Message]
B --> |edit| E[Stop and Modify Commit]
B --> |squash| F[Combine Commits]
B --> |drop| G[Remove Commit]
Command Reference Table
Command |
Purpose |
Behavior |
pick |
Retain commit |
Keeps commit in original state |
reword |
Edit message |
Modifies commit message |
edit |
Modify commit |
Pauses rebase for changes |
squash |
Combine commits |
Merges commits together |
drop |
Remove commit |
Deletes commit from history |
Practical Interactive Rebase Workflow
Repository Setup
## Create project directory
mkdir interactive-rebase-demo
cd interactive-rebase-demo
## Initialize Git repository
git init
git config user.name "LabEx Developer"
git config user.email "[email protected]"
## Create multiple commits
echo "First feature" > feature1.txt
git add feature1.txt
git commit -m "Implement first feature"
echo "Second feature" > feature2.txt
git add feature2.txt
git commit -m "Add second feature"
echo "Third feature" > feature3.txt
git add feature3.txt
git commit -m "Complete third feature"
Interactive Rebase Demonstration
## Start interactive rebase for last 3 commits
git rebase -i HEAD~3
Interactive Rebase Scenarios
1. Reordering Commits
## In interactive rebase editor
## Change commit order by rearranging lines
pick 1a2b3c "Implement first feature"
pick 4d5e6f "Complete third feature"
pick 7g8h9i "Add second feature"
2. Combining Commits
## Squash multiple commits
pick 1a2b3c "Initial implementation"
squash 4d5e6f "Minor refinements"
squash 7g8h9i "Final adjustments"
3. Editing Specific Commits
## Modify a specific commit during rebase
pick 1a2b3c "Original commit message"
edit 4d5e6f "Commit to modify"
Advanced Techniques
Splitting Commits
## During interactive rebase with 'edit'
git reset HEAD^
git add specific_file
git commit -m "Partial commit"
git add remaining_files
git commit -m "Remaining changes"
git rebase --continue
Best Practices
- Only rebase local, unpublished branches
- Use interactive rebase to clean up commit history
- Avoid rebasing shared branches
- Communicate with team about history changes
Common Pitfalls
- Accidentally dropping important commits
- Creating complex, hard-to-understand history
- Conflicts during rebase process
LabEx Workflow Recommendations
- Use interactive rebase for code reviews
- Maintain clean, meaningful commit histories
- Practice in safe, local environments first
By mastering interactive rebase, developers can transform messy commit histories into clear, logical narratives that enhance project understanding and collaboration.