How to Merge Specific Commits from One Branch to Another in Git

Nov 29, 2025 · Programming · 9 views · 7.8

Keywords: Git | cherry-pick | branch merging

Abstract: This technical article provides an in-depth exploration of selectively merging specific commits from one branch to another in the Git version control system. Through detailed analysis of the git cherry-pick command's core principles and usage scenarios, combined with practical code examples, the article comprehensively explains the operational workflow for selective commit merging. It also compares the advantages and disadvantages of different workflows including cherry-pick, merge, and rebase, while offering best practice recommendations for real-world development scenarios. The content ranges from basic command usage to advanced application scenarios, making it suitable for Git users at various skill levels.

Comprehensive Analysis of Selective Commit Merging in Git

In distributed version control systems, Git offers multiple branch management strategies, with selective merging of specific commits being a common requirement in daily development workflows. When developers have made numerous commits on BranchA (such as 113 commits) but only need to merge a subset of these commits (like the recent 10) into BranchB, traditional merge operations fail to provide this level of precise control.

Core Mechanism of the cherry-pick Command

The git cherry-pick command was specifically designed to address this selective merging requirement. The command operates on the following principle:

git cherry-pick <commit-hash>

This command extracts the changes introduced by the specified commit and reapplies these changes on the current branch, creating a new commit. Unlike full branch merging, cherry-pick focuses solely on the change set of individual commits without introducing unrelated commit history.

Practical Operation Workflow Example

Assuming we need to merge the last 10 commits from BranchA into BranchB, the following steps can be followed:

First, switch to the target branch:

git checkout BranchB

Then retrieve the commit history from BranchA to identify the required commit hashes:

git log BranchA --oneline -10

Next, perform cherry-pick operations individually or in batches:

git cherry-pick <commit1> <commit2> ... <commit10>

For consecutive commit ranges, use:

git cherry-pick <start-commit>^..<end-commit>

Technical Details and Important Considerations

Cherry-pick operations may encounter conflicts, in which case Git will pause the operation and prompt for conflict resolution. After resolving conflicts, use git cherry-pick --continue to complete the operation, or git cherry-pick --abort to cancel the entire operation.

Compared to full merging, cherry-pick offers several advantages:

However, it also presents some limitations:

Comparison with Alternative Workflows

In Git workflow selection, cherry-pick, merge, and rebase each have their appropriate use cases:

Merge workflow is suitable for scenarios requiring complete branch history preservation, but introduces all commits.

Rebase workflow can rewrite commit history but changes commit hashes.

Cherry-pick workflow provides the finest granularity of control, particularly ideal for selectively merging specific features or fixes.

Practical Application Recommendations

In real project development, consider the following recommendations:

By appropriately applying cherry-pick techniques, developers can manage code changes more flexibly, enhance team collaboration efficiency, while maintaining a clean and maintainable codebase.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.