Interactive Commit Editing
Understanding Interactive Rebase
Interactive rebase provides developers with powerful tools to modify commit history precisely and efficiently. It allows you to edit, squash, reorder, or drop commits before finalizing your branch.
Starting Interactive Rebase
## Interactive rebase for last 3 commits
git rebase -i HEAD~3
Interactive Rebase Commands
Command |
Action |
Description |
pick |
Keep commit |
Default action, uses commit as-is |
reword |
Modify message |
Change commit description |
edit |
Stop and modify |
Alter commit contents |
squash |
Combine commits |
Merge multiple commits |
drop |
Remove commit |
Delete specific commits |
Practical Editing Scenarios
1. Modifying Commit Messages
## Start interactive rebase
git rebase -i HEAD~3
## Change 'pick' to 'reword' for desired commits
## Save and exit editor
## Modify commit messages when prompted
2. Squashing Commits
graph LR
A[Original Commits] --> B[Commit 1]
B --> C[Commit 2]
C --> D[Commit 3]
E[After Squash] --> F[Combined Commit]
3. Splitting Commits
## Use 'edit' mode to break a commit into multiple commits
git rebase -i HEAD~3
## Mark commit for editing
git reset HEAD~
git add specific_files
git commit
git rebase --continue
Advanced Editing Techniques
Reordering Commits
- Simply rearrange lines in interactive rebase editor
- Useful for organizing logical commit sequence
Dropping Unnecessary Commits
- Remove commits that don't add value
- Helps maintain clean, meaningful project history
Common Pitfalls
Issue |
Solution |
Modifying shared branches |
Avoid rebasing public branches |
Losing work |
Always create backup before complex rebase |
Merge conflicts |
Resolve conflicts carefully |
LabEx Pro Tips
- Use interactive rebase to clean up local branch history
- Practice in a safe environment before applying to main projects
- Communicate with team about history modifications
Example Workflow
## Typical interactive rebase workflow
git checkout feature-branch
git rebase -i main
## Edit commits as needed
git push --force-with-lease
By mastering interactive commit editing, developers can create more meaningful, organized commit histories with precision and control.