Interactive Rebase: Rewriting History Like a Pro
git rebase -i
(interactive rebase) lets you rewrite commit history before sharing it with others.
Common Use Cases:
- Squashing multiple commits into one
- Rewording commit messages
- Reordering commits
- Dropping or editing specific commits
How to Use It:
# Rebase last 5 commits
git rebase -i HEAD~5
This opens an editor with commands for each commit:
pick a1b2c3d Add user authentication
squash b2c3d4e Fix typo in login form
reword c3d4e5f Update README
drop d4e5f6g Debug statements
Important Notes:
- Only use on local branches - rewriting shared history can cause problems
- Backup your branch first (
git branch backup-branch
) - After rebasing, you'll need to force push (
git push --force-with-lease
) if the branch was already pushed
Cherry-picking Commits: Selective Change Application
git cherry-pick
applies a specific commit from one branch to another.
When to Use Cherry-picking:
- Applying a hotfix to multiple branches
- Moving a specific feature without merging entire branches
- Recovering lost commits from deleted branches
Basic Usage:
# Identify the commit hash you want to apply
git log --oneline feature/branch
# Cherry-pick it to your current branch
git cherry-pick a1b2c3d
Advanced Options:
# Cherry-pick without auto-committing
git cherry-pick -n a1b2c3d
# Cherry-pick a range of commits
git cherry-pick a1b2c3d..e5f6g7h
# Resolve conflicts and continue
git cherry-pick --continue
Warning: Cherry-picking creates duplicate commits with new hashes. Overuse can make history harder to follow.
Git Stash: The Temporary Storage Drawer
git stash
temporarily shelves changes so you can switch contexts.
Basic Stash Workflow:
# Save current changes
git stash push -m "Work in progress on feature X"
# View your stashes
git stash list
# Apply most recent stash and keep it in stash list
git stash apply
# Apply and remove from stash list
git stash pop
# Apply a specific stash
git stash apply stash@{2}
Advanced Stash Techniques:
- Stash specific files:
git stash push path/to/file
- Include untracked files:
git stash -u
- Create a branch from stash:
git stash branch new-branch stash@{1}
- View stash diff:
git stash show -p stash@{0}
Stash Management:
# Clear all stashes
git stash clear
# Drop a specific stash
git stash drop stash@{1}
# Create a permanent branch from stash
git stash branch rescue-branch stash@{3}
Pro Tip: Name your stashes clearly (-m "message"
) as they can accumulate quickly!
Choosing the Right Tool
Situation | Recommended Command |
---|---|
Clean up local commits before pushing | git rebase -i |
Apply one specific fix to multiple branches | git cherry-pick |
Switch branches with uncommitted changes | git stash |
Combine several small commits into one | git rebase -i (squash) |
Recover work from a deleted branch | git cherry-pick or git stash branch |
Mastering Git's Power Tools
These advanced commands give you surgical control over your repository. Remember:
- Interactive rebase is for local history cleanup
- Cherry-pick is for targeted commit movement
- Stash is for temporary change storage
With great power comes great responsibility - use these commands judiciously, especially when working with shared branches.
0 Comments