How to cherry-pick a commit with Git |Video upload date:  · Duration: PT5M25S  · Language: EN

Step by step guide to cherry pick a Git commit across branches with examples and tips for avoiding conflicts and keeping history tidy.

Step by step git cherry pick guide for backports bug fixes and conflicts

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.

Quick idea of what cherry pick does

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.

Steps to cherry pick a commit

  1. Find the commit

    Use git log --oneline or your favorite GUI to locate the commit hash. Copy the short or full COMMIT_HASH for the next step.

  2. Switch to the target branch

    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.

  3. Apply the commit

    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.

  4. Handle conflicts

    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.

  5. Push and verify

    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.

Troubleshooting and common gotchas

  • Duplicate commits If the same change exists on both branches you may get an empty commit or conflicts while rebasing later. Use cherry pick sparingly for backports and isolated fixes.
  • Signed commits Cherry pick creates a new commit id. If you need the original signature you will not keep it unless you manually set GPG signing options.
  • Partial changes If you only want some hunks use git cherry-pick -n COMMIT_HASH to apply changes without committing and then edit the index before committing.

Handy tip

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.