Understanding cherry-pick in git: with Real-Life Examples

Farshad Tofighi
3 min readSep 2, 2024

--

git cherry-pick

Introduction

When working with Git, there are times when you need to apply specific changes from one branch to another without merging the entire branch. This is where git cherry-pick comes in handy. In this article, I'll walk you through what git cherry-pick is, when, and how to use it, and I'll provide a real-life example to help you understand it better.

What is git cherry-pick?

git cherry-pick is a command that allows you to pick a specific commit from one branch and apply it to another branch. Unlike a merge or rebase, which typically integrates the changes from one branch into another, cherry-pick is more selective. It's like picking a single cherry from a tree instead of taking the entire bunch.

When Should You Use git cherry-pick?

Hotfixes: Imagine you’ve fixed a bug in your development branch, but the same bug also exists in your main branch. Instead of merging the entire development branch into main, you can cherry-pick the commit that contains the bug fix.

Selective Feature Application: You might have developed a feature in a feature branch but realize that only part of the feature is needed in the main branch right now. Cherry-pick allows you to apply just the needed commits.

git cherry-pick
git cherry-pick

How to Use git cherry-pick

Let’s say you’re working on a project with two branches: main and feature. You've just fixed a critical bug in the feature branch, and you want to apply this fix to the main branch without merging all the other work from feature.

1. Identify the Commit: First, you need to find the commit hash that contains your bug fix. You can do this by running:

git log

Look for the commit that contains the bug fix. Let’s assume the commit hash is a1b2c3d4.

2. Switch to the Target Branch: Next, switch to the main branch where you want to apply the bug fix.

git checkout main

3. Cherry-Pick the Commit:

Now, apply the commit to the main branch using the cherry-pick command:

git cherry-pick a1b2c3d4

Git will now apply the changes from commit a1b2c3d4 to your main branch. If there are any conflicts, Git will prompt you to resolve them.

4. Resolve Conflicts (if any): If there are conflicts, you’ll need to manually resolve them. Once resolved, you can complete the cherry-pick by running:

git add .
git cherry-pick --continue

Alternatively, if you want to abort the cherry-pick due to conflicts, you can run:

git cherry-pick --abort

5. Push the Changes: Finally, push the changes to the remote repository.

git push origin main

And that’s it! You’ve successfully applied a specific commit from one branch to another using git cherry-pick.

Conclusion

git cherry-pick is a powerful tool that gives you the flexibility to apply specific changes across branches without the need for a full merge. It’s especially useful for hotfixes or when you need to apply selective commits to a different branch. As with all powerful tools, use it with care—it's best to fully understand the changes you're applying to avoid introducing unintended bugs or conflicts.

Next time you find yourself needing to apply a specific commit across branches, remember the simplicity and precision that git cherry-pick offers. Happy coding!

--

--