Skip to main content

Git Cherry-Pick: How to Select and Apply Specific Commits

Learn how to use git cherry-pick with step-by-step examples, best practices, and troubleshooting tips!
Mar 5, 2025  · 15 min read

Working with branches in collaborative software development environments is essential to isolate features, bug fixes, or experiments. However, there are times when you need to take specific changes from one branch and apply them to another without merging the entire branch. This is where git cherry-pick becomes invaluable. 

The goal of this tutorial is to provide a comprehensive guide on how to use git cherry-pick effectively. You'll learn the command’s syntax, understand how to handle conflicts and explore best practices and common pitfalls to avoid. Let’s get into it!

What is Git Cherry-Pick?

The git cherry-pick command is a fundamental Git command that gives developers granular control over their source code. 

Unlike other Git operations, such as merge or rebase, which work with entire branches, cherry-pick allows you to take specific commits from one branch and apply them to another. This provides precision, especially in scenarios where you only need to integrate particular changes rather than all the modifications in a branch.

The git cherry-pick command works by copying the content of the selected commits and creating new ones in the target branch, maintaining the integrity of the commit history.

When to use git cherry-pick

Use cases for git cherry-pick include: 

  • Backporting bug fixes: You’ve resolved a bug in your development branch and need the same fix in a stable or release branch. Cherry-picking lets you move the bug fix without bringing over unrelated changes.
  • Applying hotfixes: When production requires a critical fix while development branches continue to evolve, cherry-picking allows you to extract and apply the fix to the production branch.
  • Feature isolation for testing: Only specific commits related to a feature may need to be tested during testing. Instead of merging the entire feature branch, cherry-picking the required commits keeps the testing branch clean and efficient.
  • Correcting misplaced commits: If a commit was mistakenly pushed to the wrong branch, you can cherry-pick the commit to the appropriate branch without disrupting the project history.
  • Reusing changes across multiple branches: In cases where you need the same update across several branches, cherry-picking allows you to replicate changes in different branches without redoing work or introducing branch complexity.

Learn Git Fundamentals Today

For beginners: Master version control using Git.
Start Learning For Free

Syntax of Git Cherry-Pick

Understanding the git cherry-pick syntax is key to effectively using this command. It’s not just about selecting commits but about applying them precisely to achieve the desired outcome.

The basic syntax for cherry-picking a single commit is:

git cherry-pick <commit-hash>
  • git cherry-pick: The command that initiates the operation.
  • <commit-hash>: The unique identifier (SHA-1 hash) of the commit you want to cherry-pick. This hash can be found by running git log to list the commit history.

When executing the above command, Git applies the changes from the specified commit to the current branch, creating a new commit with the same changes but a different hash. 

It’s important to note that the command only transfers the commit itself, not the context or parent history from the original branch.

New to Git and GitHub? Get started with version control in this beginner-friendly GitHub and Git tutorial.

How to Use Git Cherry-Pick: Step-by-Step Examples

Now that you understand the basic syntax of git cherry-pick, it's time to see the command in action.

This section provides hands-on examples that walk you through common basic and more complex scenarios where cherry-picking is useful. Each example illustrates how to apply the changes from one or more commits to another branch. 

Example 1: Cherry-picking a single commit

Let’s say you’ve made a fix on a feature branch that you want to apply to the main branch without merging the entire feature branch.

  • First, find the hash of the commit you want to cherry-pick by running:
git log
  • Locate the commit’s hash. 
  • Switch to the main branch:
git checkout main
  • Run the cherry-pick command (assume the hash is abc1234):
git cherry-pick abc1234

When you run this command, Git will apply the changes from the commit identified by abc1234 to your current branch (which is main in this case). Git creates a new commit on the main branch that contains the same changes as the original commit but with a new commit hash.

Example 2: Cherry-picking multiple commits

In some situations, you may need to apply several distinct commits from one branch to another. Suppose you have three separate commits in your feature branch that you need to bring into the main branch.

  • Find the commit hashes for each commit you want to cherry-pick using git log:
git log
  • Switch to the main branch:
git checkout main
  • Run the cherry-pick command, listing the commits:
git cherry-pick abc1234 def5678 ghi7890

Example 3: Cherry-picking a commit range

Let’s say you’ve made a series of commits on the feature branch and want to apply them to the main branch in one go without specifying each commit individually. How would you do it?

  • Use git log to identify the start and end commits you want to cherry-pick (e.g., abc1234 to ghi7890).
  • Switch to the main branch:
git checkout main
  • Run the cherry-pick command with a commit range:
git cherry-pick abc1234...ghi7890

Example 4: Cherry-picking a commit from a remote branch

Sometimes, a critical fix exists in a remote branch, and you want to apply it to your local branch without merging the entire branch. Here’s how to do it:

  • Fetch the latest changes from the remote repository
git fetch origin
  • List the commits in the remote branch to find the hash you need:
git log origin/feature_branch --oneline
  • Assume the commit hash you need is abc1234.
  • Switch to your local main branch:
git checkout main
  • Cherry-pick the commit from the remote branch:
git cherry-pick abc1234

This lets you apply a commit from a remote branch without merging the entire branch.

Need to check out a remote branch? Follow this step-by-step guide on Git checkout for remote branches.

Example 5: Cherry-picking a commit and modifying it

If you cherry-pick a commit but need to make slight modifications before committing, you can use Git’s interactive mode. Here’s how:

  • Switch to the target branch:
git checkout main
  • Cherry-pick the commit but stop before committing:
git cherry-pick -n abc1234

The -n (or --no-commit) flag applies the changes but does not create a commit.

  • Modify the files as needed.
  • Stage and commit the changes manually:
git add .
git commit -m "Modified cherry-picked commit from feature_branch"

This is useful when you need to tweak a cherry-picked commit before finalizing it.

Handling Conflicts During Git Cherry-Pick

Conflicts are inevitable when cherry-picking commits between branches, especially when the codebase has diverged significantly. 

While cherry-pick is designed to apply changes cleanly, it can’t always reconcile differences automatically. In these cases, conflicts must be resolved manually. Understanding how conflicts arise and how to handle them is crucial for completing a cherry-pick operation.

How conflicts happen

Conflicts typically occur when the changes from the cherry-picked commit overlap with or contradict changes already present in the target branch. For example:

  • Same line modified in both branches: If the same line of code is modified in both the source and target branches, Git won’t know which version to apply.
  • File deleted in one branch but modified in another: If a file is deleted in one branch but modified in the cherry-picked commit, Git will not know whether to retain it or apply the changes.
  • Unrelated changes but same file: Even when the changes seem unrelated, if they occur within the same file, Git may flag it as a potential conflict.

When a conflict occurs, Git will stop the cherry-pick operation, leaving your working directory in a conflicted state that must be resolved before proceeding.

Resolving conflicts

Once a conflict arises, Git will provide indicators of the conflicting files, and you’ll need to manually resolve the discrepancies. Here’s how to resolve conflicts:

1. Check the conflicting files: Run the following command to see which files conflict:

git status

The command will display the list of files with conflicts. 

2. Resolve the conflicts: You can manually edit the conflicting files to resolve the issues. Remove the conflict markers (<<<<<<<, =======, >>>>>>>) and determine which changes to keep or how to combine them.

3. Use Git Mergetool (optional): If resolving conflicts manually is cumbersome, you can use a merge tool to assist in visualizing and resolving conflicts:

git mergetool

Depending on your configuration, the above tool will open a visual merge tool, which makes it easier to review and resolve conflicts.

4. Mark the conflicts as resolved: After resolving conflicts, mark the files as resolved using:

git add <conflicted-file>

5. Complete the cherry-pick: Once all conflicts are resolved and the files are staged, finalize the cherry-pick by running:

git cherry-pick --continue

Many modern IDEs and tools, such as Visual Studio Code and GitHub, offer built-in merge conflict resolution features. GitHub’s web interface allows you to resolve conflicts directly in pull requests, making it easier to collaborate on shared repositories without switching to a local environment.

Skipping a commit after a conflict

Sometimes, resolving a conflict may be too complex, or you may realize the commit is not necessary after all. In such cases, you can skip the commit entirely.

1. Abort the current cherry-pick process: If the conflict is too complicated, and you don’t want to apply the commit, you can skip it by running:

git cherry-pick --skip

This will abandon the conflicting commit and move on to the next one (if you’re cherry-picking multiple commits).

2. Abort the entire cherry-pick: If you want to abort the cherry-pick operation altogether, you can run:

git cherry-pick --abort

This command will restore your working directory to the state it was in before you started the cherry-pick. 

Want to clean up your commit history? Learn how to combine multiple commits into one with this Git squash tutorial.

Best Practices for Using Git Cherry-Pick

Misusing cherry-pick can lead to complicated histories and confusion in your project’s version control. To avoid these pitfalls, adhering to best practices ensures you’re using cherry-pick effectively without introducing unnecessary complexity to your codebase.

Keep it small and specific

Cherry-picking is most effective when used for small, specific commits that address well-defined tasks, such as bug fixes or minor feature enhancements. 

Avoid cherry-picking large, complex commits that bundle together multiple changes, as these can introduce conflicts and make it harder to manage the codebase. The more targeted the commit, the easier it is to apply without unintended side effects.

Document your cherry-picks

To maintain a clear history, always provide proper context when cherry-picking. This can be done through detailed commit messages or annotations in the documentation. 

The main gist is that you must explain why a cherry-pick was necessary. This is particularly important when cherry-picking across long-lived branches or collaborative environments, as it helps future contributors understand why changes were selectively applied.

Review the commit history

Before cherry-picking, review the commit history of both the source and target branches. This step helps identify whether the commit you’re about to cherry-pick depends on other changes. Missing dependencies can result in incomplete functionality or bugs, so ensure the cherry-pick doesn’t introduce half-baked features or updates.

Avoid overuse of cherry-picking

While cherry-picking is convenient for applying specific changes, overusing it can lead to a fragmented history with duplicate commits across branches. This can make it difficult to trace the origin of certain changes or understand the broader development context. 

Always evaluate whether merging or rebasing is a more appropriate strategy before cherry-picking. Use cherry-pick sparingly and purposefully to avoid cluttering the commit history.

Struggling with unnecessary files in Git? Learn how to use .gitignore effectively with this Git ignore tutorial.

Troubleshooting Common Issues with Git Cherry-Pick

Troubleshooting problems that arise when using cherry-pick requires a clear understanding of Git’s underlying mechanisms. In this section, I'll cover some common problems you might encounter while cherry-picking and how to resolve them.

Cherry-picking a commit that doesn’t exist

Sometimes, you may attempt to cherry-pick a commit that isn’t accessible from the current branch or has already been merged. This typically results in an error message stating that the commit couldn’t be found or applied.

1. Commit not found: This happens when the commit hash you’re trying to cherry-pick doesn’t exist in your current repository or branch context. Ensure you’re referencing the correct commit by checking the commit history with git log on the branch where the commit exists. Solution:

  • Double-check the commit hash to ensure it’s correct.
  • Verify that the commit exists on a branch you have access to.
  • If the commit is in a remote branch, ensure the branch has been fetched with git fetch.

2. Commit already applied: If the commit has already been merged or cherry-picked into the target branch, Git will prevent you from duplicating it. This safeguard keeps the history clean and avoids redundant changes. Solution:

  • Use git log to check if the commit is already in the target branch.
  • If needed, skip the cherry-pick operation since the changes have already been applied.

Cherry-picking after a rebase or a merge

Cherry-picking after a rebase or merge can introduce complexities due to the altered history of your branches. Rebasing rewrites commit histories while merging combines branches, both of which may impact the applicability of a cherry-pick.

1. Conflicts due to rebased commits: After a rebase, the commit history is rewritten, which can cause issues if you attempt to cherry-pick commits that have been altered during the rebase process. You may run into conflicts as the cherry-pick tries to apply changes that don’t align with the rewritten history. Solution:

  • Carefully review the commit history after a rebase using git log to ensure the commit you’re cherry-picking hasn’t already been modified.
  • Resolve conflicts as you would in a standard cherry-pick process, using git status and manual edits.

2. Duplicate commits after a merge: Merging branches can lead to a situation where a commit you want to cherry-pick has already been included in the merged history. Cherry-picking it again can result in duplicate commits, which can clutter your history and make it difficult to track changes. Solution:

  • Before cherry-picking, inspect the commit history on both branches to confirm whether the commit has already been merged.
  • Avoid cherry-picking the same commit if it’s already in the target branch.

Need to undo changes in Git? Learn when to use git reset vs. git revert in this Git reset and revert tutorial.

Conclusion

git cherry-pick is a powerful way to apply specific commits from one branch to another without merging the entire branch. Whether you're moving a bug fix, a feature update, or just selectively applying changes, it helps keep your Git history clean and focused.

In this guide, I covered how to cherry-pick single and multiple commits, resolve conflicts, and follow best practices to avoid common pitfalls. By using cherry-pick wisely, you can improve your workflow while keeping your repository organized.

If you're looking to deepen your Git skills, check out Foundations of Git for a strong starting point. You can also explore Introduction to GitHub Concepts or take a structured approach with the GitHub Foundations skill track.

Now that you know how to cherry-pick like a pro, go ahead and try it in your next project!

Learn Git Fundamentals Today

For beginners: Master version control using Git.

FAQs

What happens if I cherry-pick the same commit twice?

If you attempt to cherry-pick a commit that has already been applied to the target branch, Git may create a duplicate commit with a new hash. To check if a commit has already been applied, use:

git log --oneline

Alternatively, if you're unsure, use:

git cherry-pick -n <commit-hash>

This applies the changes without committing them, allowing you to review them before finalizing.

Can I cherry-pick commits from a deleted branch?

Yes! Even if the branch has been deleted, the commits may still exist in Git’s history.

Use git reflog to find the commit hash, then cherry-pick it as usual.

However, if Git has already garbage-collected the branch, the commits may be lost.

How do I cherry-pick a commit from a different repository?

To cherry-pick from another repository, first add it as a remote:

git remote add other-repo <repository-url>
git fetch other-repo

Then, identify the commit and cherry-pick it:

git cherry-pick <commit-hash>

Can I cherry-pick a commit that includes file deletions?

Yes, cherry-picking preserves all changes in the commit, including file deletions. If a commit deletes a file, cherry-picking it will remove the file in the target branch.

How do I undo a cherry-pick?

If you’ve cherry-picked a commit by mistake and haven’t pushed it yet, you can undo it using:

git reset --hard HEAD~1

If you’ve already pushed the cherry-pick, use:

git revert <commit-hash>

This creates a new commit that undoes the cherry-picked changes.

What’s the difference between git cherry-pick and git apply?

  • git cherry-pick copies a specific commit and applies it to the target branch while maintaining commit metadata.
  • git apply is used to apply patch files (created with git diff) but does not create a new commit or preserve commit history.

How can I cherry-pick a commit while keeping the original author?

By default, git cherry-pick sets you as the author. To keep the original commit author, use:

git cherry-pick -x <commit-hash>

This adds a reference to the original commit in the new commit message.

Can I cherry-pick multiple non-sequential commits?

Yes! Instead of specifying a range, list the individual commit hashes:

git cherry-pick <commit1> <commit2> <commit3>

This is useful when you only need select commits from a branch.

How do I prevent conflicts when cherry-picking across branches?

Before cherry-picking, ensure your branch is up to date:

git pull origin main

Also, review the commit history to confirm that the changes don’t depend on other missing commits. If conflicts occur, resolve them manually and continue with:

git cherry-pick --continue

When should I avoid using git cherry-pick?

Overusing git cherry-pick can lead to a messy commit history with duplicated commits across branches. It’s best avoided when:

  • The changes involve multiple dependent commits—a merge or rebase might be a better choice.
  • You need to regularly sync branches—rebasing or merging keeps the history cleaner.
  • The commit includes major refactors, as cherry-picking only moves specific changes, not broader branch context.

Kurtis Pykes 's photo
Author
Kurtis Pykes
LinkedIn
Topics

Learn more about Git with these courses!

course

Introduction to Git

2 hr
15.5K
Discover the fundamentals of Git for version control in your software and data projects.
See DetailsRight Arrow
Start Course
See MoreRight Arrow
Related

blog

Top 20 Git Commands with Examples: A Practical Guide

This guide covers the most essential Git commands with examples to help you work more efficiently!
Srujana Maddula's photo

Srujana Maddula

15 min

tutorial

Git Squash Commits: A Guide With Examples

Learn how to squash commits on a branch using interactive rebase, which helps maintain a clean and organized commit history.
François Aubry's photo

François Aubry

7 min

tutorial

Git Diff Explained: A Complete Guide with Examples

Learn how to use git diff to track code changes effectively, from basic comparisons to advanced techniques. Discover visualization tools, specialized commands, and workflows for better code management.
Bex Tuychiev's photo

Bex Tuychiev

12 min

tutorial

Git Switch Branch: A Guide With Practical Examples

Learn how to switch a branch in Git using git switch and understand the differences between git switch and git checkout.
François Aubry's photo

François Aubry

8 min

tutorial

Git Revert Merge Commit: A Guide With Examples

Learn how to safely undo a Git merge using `git revert`, preserving commit history and resolving potential conflicts.
François Aubry's photo

François Aubry

7 min

tutorial

How to Use .gitignore: A Practical Introduction with Examples

Learn how to use .gitignore to keep your Git repository clean. This tutorial covers the basics, common use cases, and practical examples to help you get started!
Kurtis Pykes 's photo

Kurtis Pykes

17 min

See MoreSee More