Git Cherry Pick
Introduction
When working with Git, you'll often need to apply specific changes from one branch to another. While merging and rebasing are common ways to combine branches, sometimes you only want to pick specific commits rather than the entire branch. This is where Git's cherry-pick
command comes in handy.
Git cherry-pick allows you to select individual commits from one branch and apply them to another branch. Think of it as "picking the cherries" (specific commits) that you want, rather than taking the entire tree (branch).
What is Git Cherry Pick?
Cherry picking in Git is the act of selecting a specific commit from one branch and applying it to another branch. Unlike merge or rebase operations that typically work with the entire branch history, cherry-pick focuses on individual commits.
In the diagram above, we cherry-picked commit "D" from the feature branch and applied it to the main branch.
Basic Syntax
The basic syntax for cherry-pick is:
git cherry-pick <commit-hash>
Where <commit-hash>
is the unique identifier of the commit you want to apply to your current branch.
When to Use Cherry Pick
Cherry picking is particularly useful in the following scenarios:
- Hotfixes: When you need to apply a bug fix to multiple branches
- Feature extraction: When you want to pull specific features from a development branch into a release branch
- Recovery: When you need to recover specific work from a branch that won't be merged
- Selective merging: When you want to apply only certain commits from a team member's branch
Step-by-Step Examples
Example 1: Basic Cherry Picking
Let's say you're working on a project with two branches: main
and feature
. You've made several commits in the feature
branch, but you only want to apply one specific commit to main
.
First, find the commit hash you want to cherry-pick:
git log feature
Output:
commit abc123... (feature)
Author: Your Name <[email protected]>
Date: Mon Feb 12 14:22:48 2024 -0500
Add responsive navigation menu
commit def456...
Author: Your Name <[email protected]>
Date: Mon Feb 12 13:15:32 2024 -0500
Fix button styling issues
commit ghi789...
Author: Your Name <[email protected]>
Date: Mon Feb 12 10:30:21 2024 -0500
Implement user authentication
Now, switch to the branch where you want to apply the commit:
git checkout main
Then cherry-pick the commit:
git cherry-pick def456
Now the "Fix button styling issues" commit has been applied to your main
branch, without bringing along the other commits from the feature
branch.
Example 2: Cherry Picking Multiple Commits
You can cherry-pick multiple commits in one command:
git cherry-pick <commit-hash-1> <commit-hash-2> <commit-hash-3>
Or, to pick a range of commits:
git cherry-pick <start-commit>..<end-commit>
Let's say you want to pick three consecutive commits from the feature
branch:
git cherry-pick ghi789..abc123
This will apply all commits between ghi789
(exclusive) and abc123
(inclusive) to your current branch.
Example 3: Handling Cherry Pick Conflicts
Sometimes, cherry-picking can result in conflicts, just like merging. When this happens, Git will pause the cherry-pick operation and ask you to resolve the conflicts.
For example:
git cherry-pick abc123
Output:
error: could not apply abc123... Add responsive navigation menu
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'
hint: and commit the result with 'git commit'
To resolve these conflicts:
- Edit the conflicted files to resolve the conflicts
- Stage the resolved files:
git add <resolved-files>
- Continue the cherry-pick:
git cherry-pick --continue
Alternatively, you can abort the cherry-pick operation:
git cherry-pick --abort
Advanced Cherry Pick Options
Cherry Pick Without Committing
If you want to apply the changes from a commit but not create a new commit:
git cherry-pick -n <commit-hash>
This adds the changes to your working directory and staging area, but doesn't commit them automatically. You can review the changes and make your own commit later.
Cherry Pick With Edit
If you want to edit the commit message during cherry-pick:
git cherry-pick -e <commit-hash>
This opens your default text editor to allow you to modify the commit message.
Cherry Pick a Merge Commit
Merge commits are special because they have multiple parent commits. To cherry-pick a merge commit, you need to specify which parent to follow:
git cherry-pick -m 1 <merge-commit-hash>
The -m
flag followed by a number specifies which parent to use as the mainline. Usually, -m 1
refers to the branch you merged into, and -m 2
refers to the branch that was merged.
Real-World Applications
Scenario 1: Backporting Fixes to an Older Version
Imagine you're maintaining multiple versions of your software. You fixed a critical bug in the latest version (version 2.0), but you also need to apply this fix to an older supported version (version 1.5).
- Find the commit hash for the bug fix in version 2.0:
git log -p
- Checkout the maintenance branch for version 1.5:
git checkout maintenance/1.5
- Cherry-pick the bug fix:
git cherry-pick abc123
Now your bug fix has been backported to the older version!
Scenario 2: Moving Experimental Features to a New Branch
You've been experimenting with several features in an experimental branch, but you realize that one specific feature is ready for inclusion in your development branch.
- Identify the commit that implements the ready feature:
git log experimental
- Create or switch to your development branch:
git checkout development
- Cherry-pick the feature commit:
git cherry-pick def456
Now you have selectively moved just that one feature to your development branch.
Best Practices for Cherry Picking
-
Use sparingly: Cherry picking can make your repository's history harder to understand because the same changes appear in multiple places.
-
Document your cherry picks: It's helpful to mention in the commit message that the commit was cherry-picked from another branch.
-
Consider merge/rebase first: If you're cherry-picking many commits, consider whether a merge or rebase might be more appropriate.
-
Be aware of dependencies: When cherry-picking, be careful about commit dependencies. If commit B depends on changes introduced in commit A, you should cherry-pick both (and in the right order).
-
Watch for conflicts: Cherry picking can create conflicts if the target branch has diverged significantly from the source branch.
Summary
Git cherry-pick is a powerful tool that allows you to selectively apply specific commits from one branch to another. It's useful for backporting fixes, extracting specific features, and selectively integrating changes.
While it's not a replacement for regular merging or rebasing operations, cherry-pick provides fine-grained control over which changes you integrate and when.
Key points to remember:
- Cherry-pick applies a specific commit to your current branch
- It creates a new commit with the same changes but a different hash
- It can cause conflicts that need manual resolution
- It should be used judiciously, as it can complicate repository history
Additional Resources
Exercises
-
Create two branches:
exercise-main
andexercise-feature
. Make several commits to the feature branch, then practice cherry-picking specific commits to the main branch. -
Practice cherry-picking multiple commits in one command.
-
Deliberately create a conflict by modifying the same file in both branches, then practice resolving the conflict during a cherry-pick operation.
-
Try cherry-picking a merge commit and observe what happens with and without the
-m
option. -
Use the
--no-commit
option to cherry-pick a commit, then modify the changes before committing them.
If you spot any mistakes on this website, please let me know at [email protected]. I’d greatly appreciate your feedback! :)