Git cherry-pick¶
Git cherry-pick is a Git operation used to apply changes introduced by specific commits to the current branch.^[600-developer-tools-git-git-patch-cherry-pick.md] Unlike merging, which integrates the entire history of a branch, cherry-picking allows for the selective application of individual changes.^[600-developer-tools-git-git-patch-cherry-pick.md]
This technique is often utilized in workflows where specific hotfixes or features need to be propagated between branches without merging the full branch history.^[600-developer-tools-git-git-patch-cherry-pick.md]
Related Commands¶
The source material discussing cherry-pick covers several related Git commands used for patch management and history manipulation:^[600-developer-tools-git-git-patch-cherry-pick.md]
git format-patch: Used to generate patch files from commits.^[600-developer-tools-git-git-patch-cherry-pick.md]git apply: A utility to check and apply patch files to the working directory.^[600-developer-tools-git-git-patch-cherry-pick.md]git am: Applies a patch file generated byformat-patchas a commit.^[600-developer-tools-git-git-patch-cherry-pick.md]git reset: Described in the source as a "regret medicine" (後悔藥) to undo changes, such asgit reset HEAD^ --hard.^[600-developer-tools-git-git-patch-cherry-pick.md]
Patch Workflow¶
The workflow for creating and applying patches involves generating a file containing the diff and then applying it to a target repository.^[600-developer-tools-git-git-patch-cherry-pick.md]
Creating Patches¶
- Single commit:
git format-patch -1 <commit>^[600-developer-tools-git-git-patch-cherry-pick.md] - Range of commits:
git format-patch <start>...<end>(includes the specified commits).^[600-developer-tools-git-git-patch-cherry-pick.md] - Since a specific commit:
git format-patch <commit>(does not include the specified commit itself).^[600-developer-tools-git-git-patch-cherry-pick.md]
Applying Patches¶
Once the patch file is copied to the target directory:^[600-developer-tools-git-git-patch-cherry-pick.md]
1. Check statistics: git apply --stat <patch-file>
2. Verify compatibility: git apply --check <patch-file>
3. Apply: git am -s < <patch-file>
Applying patches may result in merge conflicts that require manual resolution.^[600-developer-tools-git-git-patch-cherry-pick.md]
Sources¶
600-developer-tools-git-git-patch-cherry-pick.md