Git Cherry Pick Explained with Examples (Single & Multiple Commits)

Git Cherry Pick Explained with Examples (Single & Multiple Commits)

Git cherry-pick allows you to apply a specific commit from one branch to another without merging the entire branch. It is commonly used to move bug fixes, small improvements, or urgent patches across branches such as development, staging, and production.

Instead of merging all changes from a branch, git cherry-pick selectively copies only the commits you need, making it a powerful tool for maintaining a clean Git history. You can also compare this approach with git merge or git rebase.


Git Cherry Pick Cheat Sheet (Commands + Examples)

If you only need to copy a bug fix from one branch to another without merging the entire branch, git cherry-pick is the safest and fastest option.

DescriptionCommand
Cherry pick a single commit from another branchgit cherry-pick <commit-hash>
Cherry pick multiple specific commitsgit cherry-pick hash1 hash2
Cherry pick a range of commits between two commit hashesgit cherry-pick A^..B
Cherry pick a commit referenced from another branchgit cherry-pick feature-branch~2
Resume cherry-pick after resolving conflictsgit cherry-pick --continue
Cancel the current cherry-pick operation and restore branch stategit cherry-pick --abort
Skip the commit that is causing a conflictgit cherry-pick --skip
Apply changes from a commit without automatically creating a commitgit cherry-pick --no-commit
Edit the commit message during cherry pickgit cherry-pick -e <commit-hash>
Review commit changes before applying themgit show <commit-hash>
Cherry pick a commit from a remote branchgit cherry-pick origin/branch~1
Cherry pick and add a Signed-off-by linegit cherry-pick -s <commit-hash>
Resolve conflicts automatically using a merge strategygit cherry-pick -X theirs <commit>
Verify commit history before selecting a commitgit log --oneline

Cherry Pick a Single Commit

Sometimes you may want to move a specific bug fix or small change from one branch to another without merging the entire branch. This is one of the most common use cases for git cherry-pick.

Identify the commit hash

First, find the commit you want to copy to another branch.

You can view recent commits using git log:

bash
git log --oneline

Example output:

text
d4f3e5b Fix login validation bug
a7c8e2f Improve API response handling
b9d0e3g Add new feature toggle

The short string at the beginning (d4f3e5b) is the commit hash that Git uses to reference the commit.

Apply commit to another branch

Switch to the branch where you want to apply the commit using git checkout or git switch.

bash
git checkout main

Then run the cherry-pick command:

bash
git cherry-pick d4f3e5b

Git will apply the changes from that commit onto the current branch.

A new commit will be created on the target branch with the same changes but a different commit hash.


Cherry Pick Multiple Commits

Cherry pick multiple commit hashes

You can apply multiple commits in one command by listing their hashes.

bash
git cherry-pick a7c8e2f b9d0e3g c1f2h3i

Git will apply the commits in the order provided.

This is useful when a series of fixes or improvements needs to be moved to another branch.

Cherry pick a range of commits

If the commits are sequential, you can cherry-pick a range.

bash
git cherry-pick a7c8e2f^..d4f3e5b

Explanation:

  • a7c8e2f^ includes the starting commit
  • d4f3e5b is the final commit in the range

This applies all commits between those two points.


Cherry Pick a Commit from Another Branch

Cherry-picking is commonly used to move changes from a feature branch to another branch such as main, release, or hotfix.

Cherry pick from feature branch to main

First switch to the target branch:

bash
git checkout main

Then apply the commit from the feature branch:

bash
git cherry-pick feature-branch~1

Alternatively, you can cherry-pick using the commit hash obtained from the feature branch.

Cherry pick from remote branch

You can also cherry-pick commits from a remote branch after fetching the latest updates using git fetch.

bash
git fetch origin

Then run:

bash
git cherry-pick origin/feature-branch~1

This applies the selected commit from the remote branch to your current branch.


Resolve Cherry Pick Conflicts

During a cherry-pick operation, Git attempts to apply changes from the selected commit to the current branch. If the same lines of code were modified differently in the target branch, Git cannot automatically merge them and raises a conflict.

Why conflicts occur during cherry pick

Conflicts typically happen when:

  • The same file was modified in both branches
  • The target branch has diverged significantly
  • The cherry-picked commit depends on earlier commits not present in the target branch

Git will pause the operation and display a message similar to:

text
error: could not apply d4f3e5b... Fix login validation bug

You can check conflicting files using git status.

bash
git status

Manually edit the files to resolve the conflict.

Continue cherry pick after conflict

After resolving the conflicts in the affected files, stage the changes and continue the cherry-pick process using git add.

bash
git add .
git cherry-pick --continue

Git will then create the new commit on the current branch with the resolved changes.

Abort cherry pick operation

If you decide not to proceed with the cherry-pick operation, you can cancel it.

bash
git cherry-pick --abort

This command restores the branch to the state it was in before the cherry-pick started.


Cherry Pick Without Commit

Sometimes you may want to apply the changes from a commit but review or modify them before creating a new commit.

Use git cherry-pick --no-commit

The --no-commit option applies the changes without automatically creating a commit.

bash
git cherry-pick --no-commit <commit-hash>

This allows you to inspect or modify the changes before committing them.

Modify changes before committing

After applying the changes, you can edit files as needed and then create a commit manually using git commit.

bash
git add .
git commit -m "Apply selected changes from commit"

This gives you full control over the final commit content and message.


Common Git Cherry Pick Errors

When using git cherry-pick, developers may encounter common errors related to commit history or conflicts.

cherry-pick already applied

This error appears when Git detects that the selected commit changes already exist in the target branch.

Example message:

text
The previous cherry-pick is now empty

Possible causes:

  • The commit was already merged
  • The same changes were applied earlier

In such cases you can skip the commit using git cherry-pick --skip, or review history using git reflog.

bash
git cherry-pick --skip

cherry pick conflict

If Git cannot automatically merge the commit changes, it will stop and mark the conflicting files.

Example message:

text
CONFLICT (content): Merge conflict in file.txt

Steps to resolve:

  1. Edit the conflicting files
  2. Remove the conflict markers
  3. Stage the resolved files
  4. Continue the cherry-pick
bash
git add .
git cherry-pick --continue

commit not found

This error occurs when Git cannot locate the specified commit hash.

Example message:

text
fatal: bad revision '<commit-hash>'

Possible reasons:

  • Incorrect commit hash
  • Commit exists only in another branch
  • Remote branch was not fetched

You can verify available commits using git log.

bash
git log --oneline

or fetch remote updates:

bash
git fetch origin

Frequently Asked Questions

1. What is git cherry pick used for?

The git cherry-pick command allows you to select a specific commit from one branch and apply it to another branch without merging the entire branch.

2. How do I cherry pick a commit in Git?

First identify the commit hash using git log, then run git cherry-pick <commit-hash> on the target branch where you want to apply the commit.

3. Can I cherry pick multiple commits in Git?

Yes. You can cherry-pick multiple commits by specifying multiple commit hashes or by using a commit range such as git cherry-pick A^..B.

4. How do I resolve conflicts during git cherry pick?

Resolve the conflicts manually, stage the resolved files using git add, and then continue the process using git cherry-pick --continue.

5. How do I abort a cherry pick operation?

If a cherry-pick operation is in progress and you want to cancel it, run git cherry-pick --abort. This restores the branch to its previous state before the cherry-pick began.

Summary

The git cherry-pick command allows developers to selectively apply commits from one branch to another without merging the entire branch. This makes it particularly useful for transferring bug fixes, hotfixes, or small improvements across branches such as development, staging, and production.

Understanding how to cherry-pick commits, resolve conflicts, and manage commit history helps maintain a clean and organized Git workflow. For recovery scenarios, see git reset.


Official Documentation

Steve Alila

Steve Alila

Specializes in web design, WordPress development, and data analysis, with proficiency in Python, JavaScript, and data extraction tools. Additionally, he excels in web API development, AI integration, and data presentation using Matplotlib and Plotly.