Git Cherry-Pick Explained
git cherry-pick applies the changes introduced by selected commit or commits to your current branch. It is useful when you need one specific fix without merging every change from another branch. Learn how to inspect a commit, cherry-pick it safely, and recover if conflicts occur.
What does git cherry-pick do?
Cherry-picking takes the change made by an existing commit and applies it to the branch you currently have checked out. If successful, Git creates a new commit on that branch. The new commit has a different commit ID because it is a separate commit in a different history context, even when it contains the same change.
Unlike git merge, cherry-pick does not bring over the source branch’s full history or all its commits. It is a targeted operation, not a way to synchronize two branches. If you need the source branch’s complete set of changes, merging or rebasing is usually more appropriate. For a comparison, see Git merge vs. rebase.
When should you cherry-pick a commit?
- Apply an urgent, isolated fix from a development branch to a release branch.
- Move a small, independent change to another branch without integrating unrelated work.
- Backport a fix to a maintained older version of a project.
Cherry-picking can duplicate changes if the same commit is later merged through another path. Coordinate with your team and note which branches already contain the change.
How to cherry-pick a commit step by step
1. Check your working tree and branch
First, confirm you are on the branch that should receive the change and that your work is saved. Cherry-pick works on the current branch.
git status
git branch --show-current2. Find and inspect the commit
Find the commit ID on the source branch and review its summary and diff before applying it:
git log --oneline SOURCE-BRANCH
git show COMMIT-IDReplace SOURCE-BRANCH and COMMIT-ID with the actual branch name and commit ID. If the commit exists only on a remote branch, fetch first with git fetch, then inspect the fetched history.
3. Apply the commit to the current branch
Once you have confirmed the target branch and commit, run:
git cherry-pick COMMIT-IDIf there are no conflicts, Git creates a new commit on the current branch. Check the result with git status and git log --oneline -5.
Cherry-pick multiple commits
To apply several specific commits, list their IDs. Git applies them in the order you provide:
git cherry-pick COMMIT-ID-1 COMMIT-ID-2Use this for selected commits that are safe to apply in that order. If one depends on changes in a commit you did not select, it may not work by itself.
To apply a contiguous range, Git’s range syntax excludes the first endpoint and includes the second. For example, git cherry-pick START..END applies commits after START through END. Inspect the range first with git log --oneline START..END; ranges can include commits you did not intend to copy.
Resolve a cherry-pick conflict
A conflict means Git could not apply the commit automatically, usually because the target branch changed the same lines. Git pauses the operation and marks conflicted files. Review each file, edit it to the intended result, then stage the resolved files:
git status
git add FILE
git cherry-pick --continueReplace FILE with each resolved path. Review the resulting commit after Git finishes. To stop the in-progress cherry-pick and return the branch to its pre-operation state, run:
git cherry-pick --abortAvoid git cherry-pick --skip unless you deliberately want to omit the current commit from a multi-commit operation.
Common cherry-pick mistakes
- Cherry-picking onto the wrong branch: check
git branch --show-currentbefore you start. - Copying a dependent commit alone: inspect the change and its context; it may rely on earlier work from the source branch.
- Cherry-picking instead of integrating: repeated selections can make branch history confusing. If you need all branch changes, consider merging or rebasing instead.
- Cherry-picking shared changes again: confirm whether another branch or pull request has already brought the change into the target.
Frequently asked questions
Does cherry-pick remove the commit from the source branch?
No. Cherry-pick applies the commit’s changes to your current branch; it does not delete or move the original commit on its source branch.
Does cherry-pick preserve the original commit ID?
No. Git creates a new commit for the applied change, so its commit ID differs from the original. The change may be equivalent, but the commits are separate objects in the project history.