Cherry-pick applies a commit’s change to another history, creating a new commit identity. It suits selected backports with understood dependencies.
Before you start
You should understand commits, branches, the working tree and the staging area. Draw the commit graph before changing history. Try commands in a disposable repository with a clean working tree so you can observe exactly which references and files each operation changes.
The practical goal is to reason through this situation: A bug fix can be backported to a maintenance branch without merging unrelated features. Read the walkthrough first, then try the interview exercise before opening its answer. The important part is explaining the decision and its consequences, rather than remembering a definition alone.
Step-by-step walkthrough
Step 1: Identify patch prerequisites
A selected fix may depend on APIs absent in maintenance code.
Step 2: Apply to the target history
The resulting commit has a new identity on that branch.
Step 3: Validate target behavior
Run checks using the target branch’s supported versions and assumptions.
Worked scenario
A bug fix can be backported to a maintenance branch without merging unrelated features.
A fix uses a helper introduced in a later feature release. Cherry-picking its patch onto the older branch may apply cleanly but fail compilation or behavior. Backport the necessary bounded prerequisites or adapt the fix; clean textual application is only the first check.
Common mistake
A clean application does not prove semantic compatibility.
Verify the behavior
Test the regression on the maintenance branch before and after the backport.
Interview exercise
Validate a backport.
Answer and reasoning
Check prerequisite changes, supported APIs and affected tests on the target branch rather than only on the original branch.
Continue learning
Compare the scenario with the Git and CI/CD interview questions and test your understanding with the Git and CI/CD MCQs. For terminology and implementation details, consult the reference material.