In the world of Git development, you’ll often face situations like: “I only need one small change from the feature-X
branch, but I don’t want to merge the whole branch into main
right now.” Or: “There’s a critical hotfix in the develop
branch that I need to apply immediately to the release
branch.”
If you’ve ever been confused in these cases, git cherry-pick
is the lifesaver you’re looking for. It’s one of the most powerful and precise tools in Git’s arsenal, allowing you to “pick” individual commits—like ripe cherries—from one branch and apply them to another.
Let’s dive deeper into this awesome tool!
What is Git cherry-pick? Picking "sweet cherries" from another branch 🍒
Simply put, git cherry-pick
is a command used to apply a specific commit from any branch onto the tip (HEAD) of your current branch.
Imagine each branch as a cherry tree with many fruits (commits). Instead of cutting down the whole tree (git merge
) or transplanting it (git rebase
), cherry-pick
lets you walk over to another tree, pick just the cherries you want, and attach them to your own tree.
The result is a new commit on your branch. This new commit has the same changes (diff) and commit message as the original, but it gets a completely new hash because it has a different parent and is created at a different time.
When should (and shouldn’t) you "pick cherries"?
git cherry-pick
is extremely useful, but using it incorrectly can cause unnecessary trouble. Knowing when to use it—and when not to—is key to becoming a Git pro.
🎯 Use it when:
- Urgent hotfixes: The most common case. A critical bug is fixed in the
develop
branch or a feature branch. You need to apply this fix immediately tomain
orrelease
without waiting for the whole feature to be merged. - Taking a small feature: The
feature-A
branch has a useful refactor commit, but the whole feature isn’t ready. You cancherry-pick
that refactor into your ownfeature-B
branch to continue working. - Restoring lost changes: You accidentally
git reset
and lost some important commits, but they still exist in another remote branch. Usecherry-pick
to get them back. - Collaborative work: A teammate has completed a commit that solves a small part of your task. Instead of merging their whole branch, you can
cherry-pick
that commit into your workflow.
⚠️ Avoid it when:
- Moving a large number of commits: If you find yourself cherry-picking a long sequence of commits, you’re probably doing it wrong. Consider using
git rebase
orgit merge
instead. - Working on public branches:
cherry-pick
creates duplicate commits (same content, different hashes). If you cherry-pick fromdevelop
tomain
, later mergingdevelop
intomain
can confuse Git due to duplicate changes. This can be avoided, but it complicates your commit history.
How to cherry-pick correctly
The basic syntax is simple, but the real power of cherry-pick
lies in its options.
1. Picking a single commit
First, identify the “cherry” you want to pick. Use git log
on the source branch to get the commit hash.
# On the source branch (e.g., develop), view history
git log --oneline
# Example output:
# fdb321e Fix: Correct user authentication flow
# a412cbe Feat: Add new user profile page
# c876dab Style: Update button styles
Suppose you want to pick commit fdb321e
and apply it to main
.
# 1. Switch to the branch you want to apply the commit to
git switch main
# 2. Cherry-pick using the hash
git cherry-pick fdb321e
Done! The changes from fdb321e
are now on main
with a new hash.
2. Picking multiple commits
You can pick several commits at once by listing their hashes.
# Pick two specific commits
git cherry-pick fdb321e a412cbe
Or pick a range (note: commitA
is not included).
# Pick all commits after commitA up to and including commitB
git cherry-pick commitA..commitB
3. Additional options
--no-commit
or-n
: This applies the changes to your working directory without creating a new commit. Useful if you want to combine changes from multiple commits into one, or review before committing.git cherry-pick -n <commit-hash> # ...review or modify code... git add . git commit -m "My new commit message"
--edit
or-e
: Before finishing the cherry-pick, Git opens your editor so you can edit the commit message. Useful for adding context about why you picked this commit.
Handling "bruised cherries": Resolving conflicts 💥
Cherry-picking isn’t always smooth. If the commit you’re applying changes the same code as your current branch, a conflict will occur.
Don’t panic! Git will pause the cherry-pick and tell you which files are in conflict. The process is similar to resolving merge or rebase conflicts:
- Open the conflicted files. You’ll see Git’s
<<<<<<<
,=======
,>>>>>>>
markers. - Edit the file, remove the markers, and keep the final code you want.
- After resolving all conflicts, tell Git:
git add <fixed-file>
- Continue the cherry-pick:
git cherry-pick --continue
If you get stuck and want to cancel everything, just run:
git cherry-pick --abort
Everything will return to the state before you started the cherry-pick.
Cherry-pick vs. Merge vs. Rebase
To better understand the value of cherry-pick
, let’s compare it to its siblings: merge
and rebase
.
Criteria | git cherry-pick | git merge | git rebase |
---|---|---|---|
Purpose | Apply one or a few specific commits. | Combine the entire history of one branch into another. | Rewrite history by moving a sequence of commits to a new base. |
Commit history | Creates new commits, may duplicate content. | Creates a "merge commit" connecting two histories. Branching is clear. | Creates a straight, clean history, but changes commit hashes. |
Use case | Hotfixes, picking small features. | Integrating a completed feature branch into the main branch. | Cleaning up personal branch history before merging. |
Conclusion: Master cherry-pick for more flexible workflows
git cherry-pick
is a sharp and precise tool. It lets you manage your codebase flexibly, solving urgent problems without disrupting the main development flow. Like any powerful tool, use it thoughtfully, communicate clearly with your team, and you’ll find your work much easier.
Happy cherry-picking and happy coding! 🚀