Sub topic 5
Cherry-pick: copy a commit to another branch
git cherry-pick copies the change introduced by an existing commit and applies it as a new commit on your current branch. This chapter assumes you know how to add, commit, and switch branches, and that a commit is a snapshot with a parent and a unique hash. It also assumes you know the difference between the working tree, the index, and commits.
What cherry-pick does
A merge brings in all the history of another branch. Cherry-pick brings in one change. Git looks at the commit you name, computes the diff between that commit and its parent, and applies that diff to your current HEAD. It then records a new commit with the same message and author, but with a different parent and a different hash.
This matters because the new commit is a copy, not the original. Git does not remember that the two commits are related, except in the message if you ask for it (covered in the next chapter).
A basic example
A common use is a bug fix that was made on the main line and must also go into a release branch. First find the hash of the fix, then switch to the target branch and pick it.
git log --oneline main
git switch release-1.4
git cherry-pick 3f2a9c1Git creates a new commit on release-1.4. Check it with git log --oneline -3. The message matches the original, but the hash differs.
Requirements and rules
- Your working tree and index should be clean. Cherry-pick refuses to start if your uncommitted changes would be overwritten. Use
git stashfirst if you need to. - The commit can come from any branch, or from no branch at all, as long as it is reachable in the repository. A hash is enough.
- Cherry-pick applies the commit's diff, not its full snapshot. If the target branch has changed the same lines, you get a conflict. Conflicts are covered later in this tutorial.
- Picking a commit that depends on earlier commits can produce code that does not compile, even with no conflict. Git cannot know about logical dependencies.
Checking what happened
After a cherry-pick, compare the two commits to confirm that they carry the same change.
git show 3f2a9c1 --stat
git show HEAD --stat
git range-diff 3f2a9c1^! HEAD^!The --stat output should list the same files. git range-diff with the ^! notation compares exactly one commit on each side. If the target branch differs a lot, the line counts can differ slightly because of context.
Cherry-pick versus merge
| Aspect | Cherry-pick | Merge |
|---|---|---|
| What it brings in | The change from chosen commits | All commits not yet in the target |
| Resulting commit | New commit with a new hash | Merge commit with two parents, or a fast-forward |
| Link to the original | None, unless you add it to the message | Recorded in history |
| Typical use | Backporting a fix to a release branch | Combining finished lines of work |
Common mistakes
- Picking the same change twice. If you later merge the source branch into the target, Git may see the same change arriving again. It often resolves this cleanly when the content is identical, but it can also produce confusing conflicts. Prefer to merge or cherry-pick, not both, for the same work.
- Picking on the wrong branch. Run
git branch --show-currentbefore you start. The new commit lands on whichever branch is checked out. - Using the hash of a merge commit. A merge commit has two parents, so Git cannot tell which side to diff against. It stops with an error that asks for
-m. The next chapter explains this option.
Exercise
You run git cherry-pick 3f2a9c1 while on release-1.4. Is the new commit identical to 3f2a9c1? Name what stays the same and what changes.
Show answer
It is not identical. The diff, the message, and the author stay the same. The parent commit, the commit hash, and the committer date change, because the copy is a new commit built on top of the current HEAD of release-1.4.
Exercise
You have uncommitted edits in your working tree and need to cherry-pick a fix onto another branch. What is a safe way to proceed?
Show answer
Run git stash push -m "wip" to shelve the edits, switch to the target branch, run git cherry-pick <hash>, switch back, and run git stash pop to restore the edits.