Exploring git cherry and git cherry-pick



This content originally appeared on DEV Community and was authored by Dhamith Kumara

Git is the lifeblood of modern software development. From hobbyists working on personal projects to massive organizations managing enterprise codebases, Git provides the version control backbone. While the learning curve can be steep at first, most developers quickly become familiar with a handful of commands and workflows. But Git is far richer than what most of us use on a daily basis.

count gif

Did you know Git has over 150 commands available through its CLI? That number jumps even higher if you count all the flags, aliases, and plumbing commands under the hood. Most developers only scratch the surface using perhaps a dozen commands regularly. That’s fine for basic workflows, but understanding some of Git’s lesser known capabilities can help you become more effective, avoid pitfalls, and better manage complex codebases.

What is git cherry?

git cherry is a comparison tool. It helps you determine which commits in one branch are missing from another, based on content (not just commit hashes).

When Would You Use It?

Imagine you’re working on a long lived feature branch and want to know which of its commits are not yet in the main branch. Or maybe you’re preparing a release and want to ensure nothing was forgotten from a development branch.

Syntax

git cherry code

How It Works

Unlike git log, which compares by commit hash, git cherry compares patches. So if two commits have different hashes but the same content (a cherry-pick, for example), git cherry considers them duplicates.

This makes git cherry ideal for code review and release preparation tasks.

Oh I Get It Jimmy Fallon GIF

What is git cherry-pick?

While git cherry is a read only diagnostic tool, git cherry-pick modifies your codebase. It allows you to take one or more specific commits from another branch and apply them to your current branch—without merging everything from that source branch.

When Would You Use It?

Let’s say a developer fixed a critical bug on a dev branch, but the main branch also needs that fix right now. Instead of merging all of dev, you cherry-pick just that one bug fix commit.

Syntax

cherry-pick syntax

Important Notes

  • Cherry-picking creates new commit hashes, even though the content is the same.
  • Cherry-picking multiple commits? Order matters! Pick them in the sequence they were created.
  • Conflicts may arise if the codebase has changed significantly since the original commit.

git cherry vs git cherry-pick

git cherry vs git cherry-pick

A step by step guide to understanding how it works

Step 1 – Create a New Repository

mkdir git-cherry-demo
cd git-cherry-demo
git init -b main

Step 2 – Add an Initial Commit

echo "# Git Cherry Demo" > README.md
git add README.md
git commit -m "Initial commit with README"

Step 3 – Create the hero-section Branch

git checkout -b hero-section

Step 4 – Add a Unique Commit

echo "This is unique to hero-section branch" >> README.md
git add README.md
git commit -m "Add unique content to README in hero-section"

Step 5 – Add a Commit That Will Be Duplicated

echo "This is shared content" >> README.md
git add README.md
git commit -m "Add shared content to README"

Step 6 – Cherry-pick That Commit into main

First, find the commit hash for the “shared content” commit

git log --oneline

output

Copy the hash (a1549a7) and run

git checkout main
git cherry-pick a1549a7

Step 7 – Compare Branches with git cherry

git cherry main hero-section

You should see something like

output2

Final Thoughts

If Git were a toolbox, most developers stick to the hammer, screwdriver, and tape measure. But tucked away inside are precision tools like git cherry and git cherry pick that can help in nuanced, high stakes situations like release branches, hotfixes, and cross-team merges.

Learning to wield these commands doesn’t just make you a better Git user. It makes you a more capable collaborator, a faster debugger, and a smarter code reviewer.

So next time you’re facing a tangled merge problem or wondering what’s unique in a feature branch, try reaching for git cherry or git cherry-pick. You might find that small, precise changes can make a world of difference.


This content originally appeared on DEV Community and was authored by Dhamith Kumara