Introduction
Rebase and cherry-pick are advanced Git operations for rewriting and selectively applying commits. They help maintain a clean project history.
Interactive Rebase
# Rebase last 3 commits
git rebase -i HEAD~3
# In the editor:
# pick = keep commit
# squash = combine with previous
# reword = edit message
# drop = remove commit
# Continue after resolving conflicts
git rebase --continue
# Abort rebase
git rebase --abort
Rebase vs Merge
# Merge creates a merge commit
git checkout main
git merge feature
# Rebase replays commits on top
git checkout feature
git rebase main
git checkout main
git merge feature
Cherry-pick
# Apply a specific commit to current branch
git cherry-pick abc123
# Cherry-pick multiple commits
git cherry-pick abc123 def456
# Cherry-pick without committing
git cherry-pick --no-commit abc123
Summary
Rebase creates linear history by replaying commits. Cherry-pick applies specific commits. Use rebase for cleaning up before merging and cherry-pick for hotfixes.
YouTip