Master Git Cherry-Pick: Selective Commit Transfer Made Easy
Learn how to use Git's cherry-pick command to selectively copy individual or ranges of commits from one branch to another, with step‑by‑step examples, command syntax, and tips for handling multiple commits and postponing automatic commits.
When a team needs only certain features from a development branch—say feature A and B while leaving feature C for later—creating a new branch and reverting the unwanted changes works but is inefficient.
The Git cherry-pick command provides a cleaner solution by allowing you to pick specific commits from any branch and apply them onto the current branch, effectively performing a customized merge.
Example: picking two features
Assume the develop branch contains three commits that implement feature A, feature B, and feature C. The commit graph is shown below:
To merge only feature A and B into master, run the following commands:
Each git cherry-pick creates a new commit on master with a different hash from the original. git cherry-pick commitID If you need to pick a large number of commits, Git supports range selection. The syntax is left‑open, right‑closed, meaning the first commit in the range is excluded while the last is included. git cherry-pick commit1..commit100 Because commit1 is not applied, you must adjust the range accordingly. The following illustration shows how to pick commits 2 through 100:
By default, each cherry‑pick creates a commit immediately. To postpone committing until all desired commits are selected, use the -n (no‑commit) option:
git cherry-pick -n commitIDAfter picking all required commits with -n, you can create a single combined commit manually with git commit. This approach keeps the history tidy and satisfies scenarios where only selected features need to be released.
Using git cherry-pick thus enables developers to respond quickly to changing requirements without unnecessary branch gymnastics.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
