Bird
Raised Fist0
Gitdevops~20 mins

Why cherry-pick is useful in Git - Challenge Your Understanding

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Cherry-pick Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why use git cherry-pick?

Which situation best explains why git cherry-pick is useful?

ATo delete a commit from the history permanently
BTo merge all changes from one branch into another, including all commits
CTo apply a specific commit from one branch onto another without merging the entire branch
DTo create a new branch from a specific commit
Attempts:
2 left
💡 Hint

Think about applying only one change, not all changes.

💻 Command Output
intermediate
2:00remaining
Output of git cherry-pick command

What is the output of this command if the cherry-pick is successful?

git cherry-pick 9fceb02
Afatal: bad revision '9fceb02'
B
[master 1a2b3c4] Fix typo in README
 Date: Thu Apr 1 12:00:00 2024 +0000
 1 file changed, 1 insertion(+), 1 deletion(-)
Cerror: could not apply 9fceb02... Fix typo in README
DAlready up to date.
Attempts:
2 left
💡 Hint

Successful cherry-pick shows commit info and changes summary.

Troubleshoot
advanced
3:00remaining
Resolving conflicts during cherry-pick

You run git cherry-pick abc123 but get a conflict error. What is the best next step?

AEdit the conflicting files to fix conflicts, then run <code>git cherry-pick --continue</code>
BRun <code>git cherry-pick --abort</code> to cancel the cherry-pick and lose changes
CRun <code>git reset --hard</code> to discard all changes and continue cherry-pick
DRun <code>git merge --continue</code> to finish the cherry-pick
Attempts:
2 left
💡 Hint

Conflicts need manual fixing before continuing.

🔀 Workflow
advanced
2:00remaining
Cherry-pick in a hotfix workflow

In a hotfix workflow, you fix a bug on the hotfix branch and want to apply the fix to develop without merging all hotfix changes. What command do you use?

Agit merge hotfix
Bgit checkout develop && git pull
Cgit rebase develop hotfix
Dgit cherry-pick <commit-hash-of-fix>
Attempts:
2 left
💡 Hint

You want only the bug fix commit, not the whole branch.

Best Practice
expert
3:00remaining
Best practice when cherry-picking multiple commits

You need to cherry-pick several commits from one branch to another. What is the best practice?

ACherry-pick commits in the original order to avoid conflicts and maintain history
BUse <code>git merge</code> instead of cherry-pick for multiple commits
CSquash all commits into one before cherry-picking
DCherry-pick each commit one by one in any order
Attempts:
2 left
💡 Hint

Order matters to keep changes consistent.

Practice

(1/5)
1. What is the main purpose of the git cherry-pick command?
easy
A. To merge two branches completely
B. To create a new branch from a commit
C. To delete a branch safely
D. To copy specific commits from one branch to another

Solution

  1. Step 1: Understand cherry-pick function

    git cherry-pick copies individual commits, not whole branches.
  2. Step 2: Compare with other commands

    Merging combines all changes; cherry-pick selects specific commits only.
  3. Final Answer:

    To copy specific commits from one branch to another -> Option D
  4. Quick Check:

    Cherry-pick = copy commits [OK]
Hint: Cherry-pick copies commits, merge combines branches [OK]
Common Mistakes:
  • Confusing cherry-pick with merge
  • Thinking cherry-pick deletes branches
  • Assuming cherry-pick creates new branches
2. Which of the following is the correct syntax to cherry-pick a commit with hash abc123?
easy
A. git pick abc123
B. git cherry-pick abc123
C. git cherry abc123
D. git commit cherry abc123

Solution

  1. Step 1: Recall cherry-pick syntax

    The correct command is git cherry-pick <commit-hash>.
  2. Step 2: Check options for syntax errors

    The incorrect options use invalid git commands or wrong order such as 'git pick', 'git cherry', or 'git commit cherry'.
  3. Final Answer:

    git cherry-pick abc123 -> Option B
  4. Quick Check:

    Correct syntax = git cherry-pick [OK]
Hint: Use full command: git cherry-pick commit_hash [OK]
Common Mistakes:
  • Using 'git cherry' instead of 'git cherry-pick'
  • Omitting 'pick' keyword
  • Mixing cherry-pick with commit command
3. Given the following scenario:
git log --oneline on branch feature shows:
1a2b3c Fix typo in README
4d5e6f Add new login feature

You run git checkout main and then git cherry-pick 4d5e6f.
What will happen on the main branch?
medium
A. No commits are copied, cherry-pick fails
B. Both commits are copied to main
C. Only the 'Add new login feature' commit is copied to main
D. The entire feature branch is merged into main

Solution

  1. Step 1: Identify the commit cherry-picked

    The command cherry-picks commit 4d5e6f which is 'Add new login feature'.
  2. Step 2: Understand cherry-pick effect on main

    Only the specified commit is copied; other commits remain unchanged.
  3. Final Answer:

    Only the 'Add new login feature' commit is copied to main -> Option C
  4. Quick Check:

    Cherry-pick copies single commit [OK]
Hint: Cherry-pick copies one commit by hash [OK]
Common Mistakes:
  • Assuming all commits from feature branch copy
  • Thinking cherry-pick merges branches
  • Believing cherry-pick fails without conflicts
4. You tried to cherry-pick a commit but got a conflict error. What is the best way to fix this?
medium
A. Manually resolve the conflict, then run git cherry-pick --continue
B. Abort the cherry-pick with git cherry-pick --abort and try again
C. Delete the branch and recreate it
D. Run git merge --abort to fix cherry-pick conflicts

Solution

  1. Step 1: Understand cherry-pick conflict handling

    When conflicts occur, you must manually fix them in files.
  2. Step 2: Continue cherry-pick after resolving conflicts

    After fixing, run git cherry-pick --continue to finish the process.
  3. Final Answer:

    Manually resolve the conflict, then run git cherry-pick --continue -> Option A
  4. Quick Check:

    Fix conflicts + cherry-pick continue [OK]
Hint: Fix conflicts manually, then git cherry-pick --continue [OK]
Common Mistakes:
  • Using git merge commands to fix cherry-pick conflicts
  • Aborting without trying to resolve conflicts
  • Deleting branches unnecessarily
5. You have a bug fix commit on branch hotfix that you want to apply to both main and develop branches without merging all changes from hotfix. What is the best approach?
hard
A. Use git cherry-pick to copy the bug fix commit to both branches
B. Merge hotfix into main and develop
C. Rebase hotfix onto main and develop
D. Create a patch file and apply it manually

Solution

  1. Step 1: Identify the need to apply a single commit selectively

    You want only the bug fix commit, not all changes from hotfix.
  2. Step 2: Choose cherry-pick for selective commit copying

    git cherry-pick copies specific commits to multiple branches without merging entire branches.
  3. Final Answer:

    Use git cherry-pick to copy the bug fix commit to both branches -> Option A
  4. Quick Check:

    Cherry-pick = selective commit copy [OK]
Hint: Cherry-pick copies single commits to multiple branches easily [OK]
Common Mistakes:
  • Merging whole branches causing unwanted changes
  • Using rebase which rewrites history
  • Manually patching which is error-prone