Git Commit History Cleanup: Complete Guide to Interactive Rebase
Learn how to clean up Git commit history using interactive rebase, covering reword, edit, squash, fixup, and drop operations
Quick Navigation
Difficulty: 🟡 Intermediate
Estimated Time: 25-35 minutes
Prerequisites: Basic Git knowledge, Understanding of commits and branches, Command line experience, Familiarity with Git workflow
What You'll Learn
This tutorial covers essential Git commit history cleanup concepts and tools:
- Interactive Rebase Fundamentals - Understanding the rebase process and safety considerations
- Commit Operations - Reword, edit, squash, fixup, and drop operations
- History Cleanup Strategies - Best practices for organizing commit history
- Advanced Techniques - Complex rebase scenarios and conflict resolution
- Safety Measures - Backup strategies and recovery procedures
Prerequisites
- Basic Git knowledge and workflow understanding
- Understanding of commits, branches, and Git history
- Command line experience with Git commands
- Familiarity with Git workflow concepts
Related Tutorials
- Main Tutorials Hub - Step-by-step implementation guides
- DevOps Tutorials - Infrastructure automation and deployment guides
- Version Control Tutorials - Git and version control guides
Introduction
Maintaining a clean Git commit history is essential for collaboration and project maintenance. Interactive rebase is a powerful Git feature that allows you to modify, combine, reorder, and clean up commits in your history. This guide will teach you how to use interactive rebase effectively to create a professional and organized commit history.
Why Clean Up Commit History?
- Professional Appearance - Clean, logical commit history for code reviews
- Easier Debugging - Clear commit messages and logical grouping
- Better Collaboration - Team members can understand changes more easily
- Simplified Rollbacks - Easier to revert specific features or changes
- Repository Maintenance - Organized history for long-term projects
Step-by-Step Instructions
Step 1: Understanding Interactive Rebase
Interactive rebase allows you to modify commits in your Git history. The basic command is:
git rebase -i <commit-hash>
This opens an editor where you can specify what to do with each commit.
Step 2: Basic Interactive Rebase
Start with a simple interactive rebase:
# Rebase the last 3 commits
git rebase -i HEAD~3
# Or rebase to a specific commit
git rebase -i <commit-hash>
Step 3: Understanding Commit Operations
When you run interactive rebase, you'll see a list of commits with options:
pick abc1234 Initial commit
pick def5678 Add feature A
pick ghi9012 Fix bug in feature A
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
Step 4: Reword Commit Messages
Change commit messages without modifying the code:
# In the interactive rebase editor, change 'pick' to 'reword'
reword abc1234 Initial commit
pick def5678 Add feature A
pick ghi9012 Fix bug in feature A
# Save and close the editor
# Git will open another editor for each commit you marked as 'reword'
Step 5: Edit Commits
Modify the actual code in commits:
# Change 'pick' to 'edit'
pick abc1234 Initial commit
edit def5678 Add feature A
pick ghi9012 Fix bug in feature A
# Git will stop at the 'edit' commit
# Make your changes, then:
git add .
git commit --amend
git rebase --continue
Step 6: Squash Commits
Combine multiple commits into one:
# Change 'pick' to 'squash' for commits you want to combine
pick abc1234 Initial commit
pick def5678 Add feature A
squash ghi9012 Fix bug in feature A
# Git will combine the commits and open an editor for the new message
Step 7: Fixup Commits
Combine commits but discard the message of the second commit:
# Use 'fixup' to combine commits without editing the message
pick abc1234 Initial commit
pick def5678 Add feature A
fixup ghi9012 Fix bug in feature A
# The result will be one commit with the message from "Add feature A"
Step 8: Drop Commits
Remove unwanted commits entirely:
# Change 'pick' to 'drop' to remove commits
pick abc1234 Initial commit
drop def5678 Add feature A
pick ghi9012 Fix bug in feature A
# The "Add feature A" commit will be completely removed
Advanced Techniques
Complex Rebase Scenarios
Reordering Commits
# Change the order of commits in the interactive editor
pick ghi9012 Fix bug in feature A
pick abc1234 Initial commit
pick def5678 Add feature A
# Commits will be applied in the new order
Splitting Commits
# Mark a commit as 'edit'
edit def5678 Add feature A
# Git will stop here, then:
git reset HEAD~1
git add file1.txt
git commit -m "Add feature A part 1"
git add file2.txt
git commit -m "Add feature A part 2"
git rebase --continue
Interactive Rebase with Conflicts
# If conflicts occur during rebase:
git status # Check which files have conflicts
# Resolve conflicts manually
git add <resolved-files>
git rebase --continue
# Or abort the rebase entirely:
git rebase --abort
Rebase Across Branches
# Rebase your feature branch onto main
git checkout feature-branch
git rebase -i main
# This allows you to clean up commits before merging
Best Practices
1. Safety First
- Always backup your branch before rebasing
- Never rebase commits that have been pushed to shared repositories
- Use
--force-with-lease
if you must force push after rebasing - Test thoroughly after any rebase operation
2. Commit Organization
- Group related changes into logical commits
- Use descriptive commit messages that explain the "why" not just the "what"
- Keep commits atomic - one logical change per commit
- Order commits logically - dependencies first, then features
3. Interactive Rebase Workflow
# 1. Create a backup branch
git checkout -b backup-branch
git checkout feature-branch
# 2. Start interactive rebase
git rebase -i HEAD~5
# 3. Make your changes in the editor
# 4. Resolve any conflicts
# 5. Test your changes
# 6. Force push if necessary (with caution)
git push --force-with-lease origin feature-branch
4. Commit Message Standards
Follow conventional commit format:
# Format: type(scope): description
feat(auth): add OAuth2 authentication
fix(api): resolve user endpoint timeout
docs(readme): update installation instructions
style(ui): format login form components
refactor(database): optimize user queries
test(auth): add authentication unit tests
Common Use Cases
Feature Branch Cleanup
# Clean up a feature branch before merging
git checkout feature-branch
git rebase -i main
# Typical operations:
pick abc1234 Initial feature implementation
squash def5678 Add tests for feature
fixup ghi9012 Fix linting issues
squash jkl3456 Update documentation
Bug Fix Cleanup
# Clean up bug fix commits
git rebase -i HEAD~3
# Operations:
pick abc1234 Identify bug in user service
squash def5678 Implement fix for user service
squash ghi9012 Add test coverage for fix
Documentation Updates
# Combine documentation commits
git rebase -i HEAD~4
# Operations:
pick abc1234 Update API documentation
squash def5678 Fix typos in README
squash ghi9012 Add code examples
squash jkl3456 Update installation guide
Troubleshooting
Common Issues and Solutions
Rebase Conflicts
# During rebase, if conflicts occur:
git status # See conflicted files
# Edit files to resolve conflicts
git add <resolved-files>
git rebase --continue
# Or abort and start over:
git rebase --abort
Lost Commits
# If you accidentally lose commits:
git reflog # Find the commit hash
git checkout -b recovery-branch <commit-hash>
git checkout feature-branch
git cherry-pick <commit-hash>
Force Push Issues
# If you need to force push after rebasing:
git push --force-with-lease origin feature-branch
# This is safer than --force as it checks if others have pushed
Recovery Commands
# Abort current rebase
git rebase --abort
# Skip current commit
git rebase --skip
# Continue after resolving conflicts
git rebase --continue
# View rebase status
git status
Safety Measures
Backup Strategies
# Always create a backup branch before rebasing
git checkout -b backup-feature-branch
git checkout feature-branch
# Or use git reflog to track all operations
git reflog --oneline
Testing After Rebase
# Run your test suite after rebasing
npm test
# or
python -m pytest
# or
go test ./...
# Check that your application still works
npm start
# or
python app.py
# or
go run main.go
Safe Force Pushing
# Use --force-with-lease instead of --force
git push --force-with-lease origin feature-branch
# This prevents overwriting others' work
Conclusion
Interactive rebase is a powerful tool for maintaining clean Git commit history. By following the techniques in this guide, you can create professional, organized commit histories that make collaboration easier and project maintenance simpler.
Key Takeaways
- Interactive rebase allows you to modify, combine, and reorder commits
- Safety first - always backup and test after rebasing
- Common operations include reword, edit, squash, fixup, and drop
- Best practices ensure clean, logical commit organization
- Recovery tools are available if something goes wrong
Next Steps
- Practice with simple rebases on feature branches
- Learn advanced techniques like splitting commits
- Establish team standards for commit organization
- Integrate cleanup into your regular workflow
Start with simple interactive rebases and gradually build your confidence with more complex operations. Your Git history will thank you!
Tags: #Git #CommitHistory #InteractiveRebase #Cleanup #VersionControl #GitWorkflow #Rebase #Squash #CommitOrganization