Git Cherrypicking
Links: 106 Git Index
Cherry-picking¶
- Cherry-pick a very straightforward way of saying that you would like to copy a series of commits below your current location (
HEAD
). - Initial state:
We first checkout the branch where we want the specific commits and then run the cherrypick command
git cherry-pick C2 C4
- We wanted commits
C2
andC4
and git plopped them down right below us.
- Order matters in cherry picking
- Git cherry-pick is great when you know which commits you want (and you know their corresponding hashes)
Interactive Rebasing¶
- Initial state:
git rebase -i HEAD~4
- After some commit reordering
Do NOT use Interactive Rebase on commits that you've already pushed/shared on a remote repository
Instead, use it for cleaning up your local commit history before merging it into a shared team branch.
Use cases¶
- Cherry-picking should only be used in very specific situations
- Your main way of integration should always be merge and rebase.
Example 1¶
- Problem Statement:
- I'm trying to track down a bug but it is quite elusive. In order to aid in my detective work, I put in a few debug commands and a few print statements. All of these debugging/print statements are in their own commits. Finally I track down the bug, fix it, and rejoice!
- Only problem is that I now need to get my
bugFix
back into themain
branch. If I simply fast-forwardedmain
, thenmain
would get all my debug statements which is undesirable. There has to be another way...
- In this example could have been done using either interactive rebasing or cherry-picking
- Solution :
git checkout main; git cherry-pick c4
Example 2¶
- You made a commit to the main branch whereas it should have been on the feature branch
Last updated: 2022-06-21