If you need to bring one bug fix from branch A into branch B without dragging the whole feature branch along then cherry pick is your friend and your mild curse. This guide walks the command line workflow for git cherry pick while keeping history readable and handling conflict resolution like a grownup.
Cherry pick copies a commit from one branch and reapplies it on another branch as a new commit. That is perfect for backporting fixes or picking an isolated change. Abuse it and you will create duplicate commits across git branches and make future merges awkward for your future self.
Use git log --oneline
or your favorite GUI to locate the commit hash. Copy the short or full COMMIT_HASH for the next step.
Make sure you are on the branch where the change should land. You can use either git switch target-branch
or the older git checkout target-branch
. Update first with git fetch
if you need to stay current with origin.
Apply a single commit with
git cherry-pick COMMIT_HASH
To apply a contiguous range use either git cherry-pick A..B
or git cherry-pick A^..B
depending on which commits you want.
If Git finds conflicts it will stop and tell you. Check git status
to see the files. Resolve each file, then stage with git add path/to/file
. Finish the operation with git cherry-pick --continue
. If you decide the pick was a bad idea use git cherry-pick --abort
to roll back cleanly.
Once the pick is committed locally push with git push origin target-branch
. Inspect the log with git log --oneline
to confirm the commit arrived as expected.
git cherry-pick -n COMMIT_HASH
to apply changes without committing and then edit the index before committing.Use git cherry-pick -x COMMIT_HASH
to add a tiny reference to the original commit in the new commit message. Future you will thank present you for that breadcrumb trail when playing detective.
Cherry pick is a neat tool in the developer workflow toolbelt for one off fixes, backports, and surgical changes across git branches. Use it with care and keep the conflict resolution tools ready. Now go fix that bug and try not to anger your repo history.
I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!
This is a dedicated watch page for a single video.