Sub topic 15
Cherry-picking a commit
This chapter assumes you can create and switch branches, read git log --oneline, and resolve a merge conflict. It covers how git cherry-pick works, when to use it, and the errors you will meet.
What cherry-pick does
git cherry-pick <commit> takes the changes introduced by one existing commit and applies them as a new commit on the branch you are on. The original commit stays where it is. The new commit has the same diff and message, but it is a different commit with a different SHA, because its parent and timestamp differ.
git switch release/1.0
git cherry-pick <commit-sha>Git works out what the chosen commit changed by comparing it with its parent. It then applies that diff to the current HEAD. Internally this is a three-way merge: the base is the picked commit's parent, "ours" is your current commit, and "theirs" is the picked commit. This is why cherry-pick can conflict in the same way a merge does.
placeholderThe diagram below shows the result. Commit C on feature is copied onto release as C'.
When to use it
Cherry-pick fits when you need one commit, not a whole line of work.
- Backporting a fix. A bug fix lands on
main, and you also need it onrelease/1.0. Mergingmainwould bring in every other change, so you pick only the fix. - Rescuing a commit made on the wrong branch. You committed on
mainby mistake. Switch to the right branch, cherry-pick the commit, then remove it frommain. - Pulling one change out of an unfinished branch. A teammate's branch contains a useful small commit you need now.
It is a poor choice when you want most of a branch. Picking many commits by hand creates duplicates, and Git will not remember that they are related.
Finding the commit and checking the result
Find the SHA on the source branch, then pick it.
git log --oneline feature/login
git switch release/1.0
git cherry-pick 4f2a9c1
git log --oneline -3You can also name a commit relative to a branch tip. git cherry-pick feature/login~1 picks the second-newest commit on feature/login, and git cherry-pick feature/login picks its tip.
Useful options
| Option | Effect |
|---|---|
| `-x` | Appends "(cherry picked from commit ...)" to the message. Useful for backports, so readers can trace the origin. |
| `-n` or `--no-commit` | Applies the changes to the working tree and index but does not commit. Lets you combine several picks into one commit. |
| `-e` or `--edit` | Opens the editor so you can change the message. |
| `-m <parent>` | Required to pick a merge commit. Chooses which parent is the mainline. |
| `--continue`, `--abort`, `--skip`, `--quit` | Control a pick that stopped on a conflict or in a sequence. |
A short backport example:
git switch release/1.0
git cherry-pick -x 4f2a9c1Picking several commits
You can list several commits. Git applies them in the order you give them.
git cherry-pick 4f2a9c1 8be03d7You can also pick a range. A..B excludes A and includes B, so the range below picks everything after A up to and including B.
git cherry-pick A..B
git cherry-pick A^..BThe second form includes A itself. Merge commits inside a range are a problem, so check the range with git log --oneline A..B first.
Conflicts
If the target branch has changed the same lines, the pick stops. Git reports a conflict and leaves markers in the files, as in a merge. Resolve it, stage the files, and continue.
git cherry-pick 4f2a9c1
# CONFLICT (content): Merge conflict in app/config.py
# Edit app/config.py to remove <<<<<<<, =======, >>>>>>> markers, then:
git add app/config.py
git cherry-pick --continueTo give up and return to the state before the pick:
git cherry-pick --abortUse git status at any point. It tells you that a cherry-pick is in progress and lists unmerged files.
Common errors and edge cases
- Empty pick. If the target branch already contains the same change, Git stops with a message that the cherry-pick is now empty. Run
git cherry-pick --skipto drop it, orgit cherry-pick --abortto stop. - Merge commit error. Picking a merge commit fails with "is a merge but no -m option was given". Use
-m 1to treat the first parent as the mainline. This picks the changes the merge brought in relative to that parent. Do this rarely, and only when you understand which parent is which. - Dirty working tree. Git refuses to start if uncommitted changes would be overwritten. Commit or stash first.
- Wrong commit. The pick succeeded, but it was the wrong one. If you have not pushed,
git reset --hard HEAD~1removes the new commit. Use it only when the commit is the one you just made and you have nothing else uncommitted.git reflogcan recover from mistakes. - Missing context. A commit may depend on earlier commits, such as a renamed function. The pick can apply cleanly but produce code that does not compile. Build and test after every pick.
Cherry-pick, merge, and rebase
All three combine work, but they differ in what they preserve.
| Operation | Moves | History effect |
|---|---|---|
| Merge | Whole branch | Keeps original commits, may add a merge commit |
| Rebase | Whole branch | Replaces your commits with copies on a new base |
| Cherry-pick | Chosen commits | Copies them to the current branch, leaves the originals |
Because cherry-pick creates copies, the same change then exists as two commits. Later, if you merge the source branch into the target, Git compares file contents and not commit identity. Usually it recognises that both sides made the identical change and merges cleanly. Edits made around the copied change can still produce conflicts. Backports to a long-lived release branch are the standard case, and -x helps people see the relationship.
To see which commits on one branch have no equivalent on another, use:
git cherry -v release/1.0 mainThis lists commits on main that are missing from release/1.0. A leading + means no equivalent change was found. A leading - means an equivalent already exists.
Summary of the workflow
- Identify the commit with
git log. - Switch to the target branch and make sure the working tree is clean.
- Run
git cherry-pick -x <sha>. - If it conflicts, resolve,
git add, andgit cherry-pick --continue. - Build and test.
- Push the target branch.
Exercise
You fixed a bug in commit 4f2a9c1 on main. You need the same fix on release/1.0, but main contains other unreleased work that must not go to that branch. Which commands do you run, and why does the new commit have a different SHA than the original?
Show answer
Run git switch release/1.0 and then git cherry-pick -x 4f2a9c1. The -x option records the original commit in the message. The SHA differs because a commit's SHA is computed from its content, parent, author and committer data, and timestamp. The copy has a different parent (the tip of release/1.0), so it is a new commit even though its diff is the same.
Exercise
A cherry-pick stops with a conflict in app/config.py. What are the steps to finish it, and how do you abandon it?
Show answer
Edit app/config.py to remove the conflict markers, run git add app/config.py, then run git cherry-pick --continue. To abandon the pick and restore the previous state, run git cherry-pick --abort.