[Advanced Git] What is Git cherry-pick? The "Magic" Command for Selective Commits

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.

What is Git cherry-pick? The "magic" command for selective commits

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:

  1. 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 to main or release without waiting for the whole feature to be merged.
  2. Taking a small feature: The feature-A branch has a useful refactor commit, but the whole feature isn’t ready. You can cherry-pick that refactor into your own feature-B branch to continue working.
  3. Restoring lost changes: You accidentally git reset and lost some important commits, but they still exist in another remote branch. Use cherry-pick to get them back.
  4. 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:

  1. 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 or git merge instead.
  2. Working on public branches: cherry-pick creates duplicate commits (same content, different hashes). If you cherry-pick from develop to main, later merging develop into main 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:

  1. Open the conflicted files. You’ll see Git’s <<<<<<<, =======, >>>>>>> markers.
  2. Edit the file, remove the markers, and keep the final code you want.
  3. After resolving all conflicts, tell Git:
    git add <fixed-file>
    
  4. 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.

Git cherry-pick vs. Merge vs. Rebase

Criteriagit cherry-pickgit mergegit rebase
PurposeApply 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 historyCreates 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 caseHotfixes, 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! 🚀

Related Posts

[Advanced Git] What is Git Tag? Usage and Real-World Applications

Want to mark important versions of your project? Git Tag is the tool you need. Learn how to use Git Tag to easily track and manage releases, boosting your workflow efficiency.

[Advanced Git] Git Rebase Guide: Understand and Use Effectively

Git Rebase is used to rearrange, combine, and modify commits. This article helps you distinguish Git Rebase from Git Merge, and master how to use Git Rebase effectively and safely.

[Advanced Git] How to Use Git Stash to Handle Unfinished Changes

When you are working on a new feature and an urgent request comes up, git stash is your lifesaver. Learn how git stash can temporarily save your work, giving you flexibility to switch between branches.

[Advanced Git] Git reflog: Recovering commit history and every action in Git

What is Git reflog? A detailed guide on how to use git reflog to review your action history and recover changes, ensuring you never lose your code again.