4c Cherry-Picking Cherry-picking is a powerful Git command that allows you to apply a specific commit from one branch to another. Instead of merging entire branches, cherry-picking lets you selectively choose individual commits, which is particularly useful for applying hotfixes or taking specific changes from feature branches.
Basic Cherry-Pick Command git cherry-pick [commit-hash] This command applies the changes introduced by a specific commit to your current branch, creating a new commit with a different commit hash.
Common Options and Flags -n or --no-commit: Apply changes but don't commit automatically -e or --edit: Edit the commit message before committing -m or --mainline: Specify which parent branch to consider as the mainline -s or --signoff: Add a Signed-off-by line to the commit
Practical Examples Example 1: Applying a Hotfix from Main Branch Let's say you're working on a feature branch but need to apply a critical bug fix that was committed to the main branch:
Check the commit history
Apply the security fix to your feature branch
Verify the change was applied
Example 2: Applying Multiple Commits You can cherry-pick multiple commits in sequence:
Apply commits one by one Or apply a range of commits
Example 3: Cherry-Picking from a Different Branch Suppose you have a stable branch with bug fixes that you want to incorporate into your development branch: Switch to your development branch
Check what commits are available in the stable branch
Cherry-pick these commits into develop
Verify the commits were applied
#### Advanced Cherry-Pick Scenarios Cherry-Picking a Range of Commits Apply commits from abc1234 up to ghi9012 (inclusive)
Apply commits from abc1234 up to ghi9012, excluding abc1234
Apply a single commit with a specific message
Cherry-Picking with Conflicts When cherry-picking commits that conflict with your current working directory:
Resolve conflicts manually then stage the resolved files
Complete the cherry-pick
Cherry-Picking Without Creating a New Commit Apply changes without creating a new commit (squash mode)
#### Can you answer these Questions 1. What is the primary purpose of git cherry-pick and when would you use it instead of merging? 2. How does cherry-picking create a new commit compared to other Git operations like merge or rebase? 3. Can you cherry-pick a commit that has already been cherry-picked into your current branch? What happens in such a case? 4. What is the difference between git cherry-pick abc1234 and git cherry-pick abc1234..ghi9012? 5. How do you resolve conflicts that occur when using git cherry-pick? 6. What are the advantages and disadvantages of using cherry-picking in a collaborative environment? 7. When might you use git cherry-pick -n and what is its practical application? 8. What happens to the original commit when you cherry-pick it into another branch? Does it get removed from the source branch? 9. How can you see which commits have already been cherry-picked into your current branch? 10. What is the purpose of using git cherry-pick -m with a custom message? 11. Why might you choose to cherry-pick specific commits rather than merging entire branches in a feature workflow? 12. How does git cherry-pick handle commits that were made on different branches but affect the same files? 13. What is the significance of the commit hash being different after a cherry-pick operation? 14. Can you cherry-pick commits from a remote branch, and if so, how would you do it? 15. What are some best practices for managing cherry-picked commits in your project history? 16. When using git cherry-pick with multiple commits, what happens if one commit fails due to conflicts? 17. How does the --signoff flag work with git cherry-pick, and when might you use it? 18. What are some common mistakes people make when using git cherry-pick? 19. How can you undo a cherry-pick if you realize it was applied incorrectly? 20. In what scenarios would you prefer using git cherry-pick over creating a new branch and merging specific changes?