Interactive Rebase
Understanding Interactive Rebase
Interactive rebase is a powerful Git feature that allows you to modify, reorder, squash, or split commits in your branch's history.
Basic Interactive Rebase Workflow
graph LR
A[Original Commit History] --> B[Interactive Rebase]
B --> C[Modified Commit History]
Starting an Interactive Rebase
## Rebase last 3 commits
git rebase -i HEAD~3
Interactive Rebase Commands
Command |
Description |
Usage |
pick |
Use commit as-is |
Default action |
reword |
Modify commit message |
Change commit description |
edit |
Stop and modify commit |
Alter commit contents |
squash |
Combine commits |
Merge multiple commits |
drop |
Remove commit |
Delete specific commit |
reorder |
Change commit sequence |
Rearrange commit order |
Practical Examples
1. Reordering Commits
## Open interactive rebase
git rebase -i HEAD~3
## In the editor, change commit order by moving lines
2. Squashing Commits
## Combine multiple commits into one
git rebase -i HEAD~4
## Change 'pick' to 'squash' for commits to merge
3. Editing a Specific Commit
## Start interactive rebase
git rebase -i HEAD~3
## Change 'pick' to 'edit' for target commit
git commit --amend
git rebase --continue
Advanced Rebase Techniques
Splitting Commits
## Mark commit for editing during rebase
git rebase -i HEAD~3
## Use 'edit' command
git reset HEAD~
git add specific_files
git commit
git rebase --continue
Rebase Safety Guidelines
- Never rebase shared branches
- Always backup your repository
- Resolve conflicts carefully
- Use with local branches only
Handling Conflicts
## If conflicts occur during rebase
git status
## Manually resolve conflicts
git add conflicted_files
git rebase --continue
Best Practices
- Use interactive rebase to clean up local branch history
- Keep commits logical and focused
- Avoid rebasing published commits
- Understand the impact before proceeding
Interactive rebase is a sophisticated tool for managing Git history. LabEx recommends practicing in a safe environment to master these techniques.