Interactive Git Rebase Guide
Interactive rebase is a powerful Git feature that allows you to modify your commit history. This guide will show you how to use interactive rebase to clean up, reorganize, and perfect your commit history.
What You’ll Learn
- How to start an interactive rebase
- Different rebase commands and their effects
- How to modify commit messages
- How to combine, split, and reorder commits
- Best practices for rebasing
Implementation Steps
-
Starting Interactive Rebase
# Rebase last 3 commits git rebase -i HEAD~3
- Opens editor with commit list
- Commits are listed in reverse chronological order
- Each line shows commit hash, message, and available commands
-
Basic Rebase Commands
pick abc1234 First commit reword def5678 Second commit edit ghi9012 Third commit
pick
: Keep commit as isreword
: Change commit messageedit
: Stop for amendingsquash
: Combine with previous commitfixup
: Combine and discard messagedrop
: Remove commit
-
Modifying Commit Messages
# After selecting 'reword' git commit --amend git rebase --continue
- Edit message in editor
- Save and close to continue rebase
-
Combining Commits
pick abc1234 First commit squash def5678 Second commit squash ghi9012 Third commit
- Combines multiple commits into one
- Allows editing final commit message
Best Practices
-
Before Starting Rebase
- Create a backup branch
- Ensure working directory is clean
- Check if commits are pushed
- Review commit history
-
During Rebase
- Take your time to review changes
- Test after each significant change
- Keep commit messages clear and descriptive
- Use
git status
to check progress
-
After Rebase
- Test the codebase thoroughly
- Verify commit history is correct
- Push changes if necessary
- Communicate with team members
-
Safety Measures
- Use
git reflog
to recover from mistakes - Keep backup branches
- Don’t rebase shared commits
- Test after each rebase operation
- Use
Common Use Cases
-
Cleaning Up Commit History
git rebase -i HEAD~5 # Reorder and combine commits
-
Fixing Commit Messages
git rebase -i HEAD~3 # Change 'pick' to 'reword' for target commit
-
Splitting a Commit
git rebase -i HEAD~3 # Change 'pick' to 'edit' for target commit git reset HEAD~1 git add file1.txt git commit -m "First part" git add file2.txt git commit -m "Second part" git rebase --continue
-
Removing Sensitive Data
git rebase -i HEAD~5 # Change 'pick' to 'edit' for target commit git reset HEAD~1 # Remove sensitive data git add . git commit -m "Remove sensitive data" git rebase --continue
Advanced Usage
-
Rebasing onto Another Branch
git rebase -i main
-
Using Rebase to Update Feature Branch
git checkout feature git rebase -i main
-
Interactive Rebase with Conflicts
# Resolve conflicts git add resolved-files git rebase --continue
-
Aborting Rebase
git rebase --abort
Common Issues and Solutions
-
Merge Conflicts
# Resolve conflicts git add resolved-files git rebase --continue
-
Lost Commits
git reflog git reset --hard HEAD@{1}
-
Wrong Rebase Target
git rebase --abort # Start over with correct target
Conclusion
Interactive rebase is a powerful tool for maintaining a clean Git history. Remember to:
- Always create backup branches
- Test thoroughly after rebasing
- Be careful with shared commits
- Keep commit messages clear
- Use appropriate rebase commands
Next Steps
After mastering interactive rebase, you might want to:
- Learn about Git hooks for automation
- Explore advanced Git workflows
- Study Git’s internal mechanisms
- Practice with different rebase scenarios