Fundamentals 6 min read

Master Git Cherry-Pick: Precise Code Transfer Techniques & Tips

This guide explains the principle behind Git’s cherry-pick command, shows how to locate commit hashes, switch branches, execute the command, and highlights common scenarios such as urgent bug fixes, feature migration, and commit recovery, plus advanced options and cautions.

Lobster Programming
Lobster Programming
Lobster Programming
Master Git Cherry-Pick: Precise Code Transfer Techniques & Tips

1. Principle of cherry-pick

Git cherry-pick copies the changes introduced by a specific commit from one branch and creates a new commit with those changes on another branch, preserving the original code but generating a new hash.

For example, if commits A, B, C exist on master and a feature branch is created from B with additional commits D, E, F, cherry-picking commit E onto master will produce a new commit E1 that contains the same changes as E but has its own independent hash.

Commit graph showing A, B, C on master
Commit graph showing A, B, C on master
Feature branch with D, E, F and cherry-pick of E to master
Feature branch with D, E, F and cherry-pick of E to master

2. Operation steps

Find the target commit hash git log feature [your-branch] Locate the commit you want to cherry-pick and copy its hash (e.g., asdf1234 ).

Switch to the target branch git checkout [target-branch] Move to the branch where you want to apply the commit.

Execute cherry-pick git cherry-pick [commit-hash] The command creates a new commit on the current branch containing the changes from the selected commit.

IDEA and other IDEs also provide UI integration for these steps.

3. Common use cases

Urgent bug fix : When a critical bug is fixed on a development branch but needs to be applied to production without merging all development changes.

Feature migration : Transfer a specific feature from a feature branch to master while leaving other in‑progress work untouched.

Commit recovery : Retrieve a single commit that was accidentally removed or is needed from an older history.

4. Additional tips

Multiple commits: git cherry-pick abc123 def456 ghi789 picks several commits in one command.

Range syntax: git cherry-pick A..D copies all commits after A up to and including D.

No‑commit mode: git cherry-pick -n abc123 applies changes to the index and working tree without creating a commit, allowing further modifications or manual commit.

5. Cautions

Each cherry-pick creates a new commit, altering history; excessive use can make the commit graph harder to follow.

Prefer merge or rebase for larger integrations; cherry-pick is best for isolated commits.

In summary, cherry-pick enables precise extraction of specific changes from one branch to another, making it ideal for urgent fixes, selective feature migration, and targeted commit recovery.

cherry-pickcode transfer
Lobster Programming
Written by

Lobster Programming

Sharing insights on technical analysis and exchange, making life better through technology.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.