Skip to content
tezvyn:

Git Cherry-Pick: Copy a Commit to Another Branch

Source: git-scm.comHardHow cards are made

Think of git cherry-pick as copying a single commit's changes from one branch and reapplying them as a new commit on another. It's for backporting a bug fix without merging an entire feature branch.

Why it exists

Sometimes you need a specific change from one branch, but you can't merge the entire branch. For example, a single bug fix is on your develop branch, but you need it on a stable release branch immediately, without all the other new, unstable features. Cherry-pick solves this by isolating and reapplying just that one commit's changes.

The mental model

Think of git cherry-pick as a targeted copy-and-paste operation for commits. Instead of merging branches, which brings over an entire history, you are selecting a single "fruit" (a commit) from one branch's "tree" and placing it onto another. The result is a new commit on the target branch that contains the exact same code changes as the original, but with a new commit hash and history.

How it works

When you run git cherry-pick <commit-hash>, Git finds the specified commit and calculates the patch (the diff) it introduced. It then tries to apply this patch to your current HEAD. This requires your working tree to be clean. If the application is clean, Git creates a new commit with the same message as the original. If there are conflicts, the process pauses, marks the conflicting files with <<<<<<< and >>>>>>>, and waits for you to resolve them manually before continuing (--continue) or stopping (--abort).

When to use it

The primary use case is backporting fixes. Imagine a critical security patch is committed to your main branch. You can cherry-pick that single commit onto older, supported release branches. It's also useful for grabbing a small improvement from a feature branch without merging the whole incomplete feature. Use the -x flag to automatically add a "(cherry picked from commit...)" line to the message, which is crucial for tracking origins on public branches.

When not to use it

Avoid using cherry-pick as your main workflow instead of merging. If you find yourself cherry-picking many commits from a feature branch, you should probably just merge the branch. Overuse creates a messy, duplicated history that is hard to reason about and can cause complex merge conflicts later when the original and cherry-picked branches are eventually merged. It breaks the clear lineage that git merge or git rebase provides.

One canonical example

To apply a bug fix from the develop branch to your current release branch, first find the commit hash of the fix (e.g., a1b2c3d) using git log develop. Then, on the release branch, run git cherry-pick a1b2c3d. This creates a new commit on release with the same changes and message as the original commit on develop.

Interview question

When is git cherry-pick the most appropriate Git command to use?

  • a.To reorder and consolidate multiple commits on a local branch before pushing them.
  • b.To permanently remove a commit from a branch's history after it has been pushed.
  • c.To combine all new features from a completed feature branch into the main development line.
  • d.To apply a specific bug fix from a development branch to a stable release branch without including other changes.Correct
Why?

The card states that cherry-pick is for "backporting a bug fix without merging an entire feature branch" and for applying "a single bug fix... without all the other new, unstable features." Option C describes a standard git merge operation, not cherry-pick.

Just read this? Test yourself on what you have been reading.

Read the original → git-scm.com

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on git — each one lists the topics its interview covers.

See open roles