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
Cherry-picking multiple commits
📖 Scenario: You are working on a project with multiple branches. You want to bring specific changes from one branch into another without merging the entire branch. This is useful when you only need some fixes or features from a different branch.
🎯 Goal: Learn how to cherry-pick multiple commits from one branch to another using Git commands.
📋 What You'll Learn
Create a new branch called feature from main
Make three commits on the feature branch with exact commit messages
Switch back to main branch
Cherry-pick two specific commits from feature branch onto main
Display the commit log on main to verify cherry-pick
💡 Why This Matters
🌍 Real World
Cherry-picking is used when you want to apply specific changes from one branch to another without merging all changes. For example, applying a bug fix from a development branch to the main branch quickly.
💼 Career
Understanding cherry-pick helps in managing code changes efficiently in team projects, especially when working with multiple branches and needing selective updates.
Progress0 / 4 steps
1
Create the feature branch and make commits
Create a new branch called feature from main. Then make three commits on feature branch with these exact commit messages in order: "Add login feature", "Fix login bug", and "Improve login UI".
Git
Hint
Use git checkout -b feature main to create and switch to the feature branch. Use git commit --allow-empty -m "message" to create commits without changing files.
2
Switch back to main branch
Switch back to the main branch using the correct Git command.
Git
Hint
Use git checkout main to switch back to the main branch.
3
Cherry-pick two commits from feature branch
Cherry-pick the commits with messages "Add login feature" and "Improve login UI" from the feature branch onto the main branch. Use the commit hashes to cherry-pick these commits.
Git
Hint
Use git log feature --grep="commit message" --format=%H -n 1 to get the commit hash. Then use git cherry-pick <hash> to apply the commit.
4
Verify the cherry-picked commits
Run the command to show the commit log on the main branch and verify that the commits with messages "Add login feature" and "Improve login UI" are present.
Git
Hint
Use git log --oneline to see the short commit messages on the current branch.
Practice
(1/5)
1. What does the git cherry-pick command do when used with multiple commit hashes?
easy
A. It applies the changes from each specified commit onto the current branch.
B. It merges the entire branch containing those commits into the current branch.
C. It deletes the specified commits from the current branch.
D. It creates a new branch with the specified commits only.
Solution
Step 1: Understand cherry-pick purpose
The git cherry-pick command copies changes from specific commits to the current branch without merging the whole branch.
Step 2: Effect of multiple commits
When multiple commit hashes are listed, git applies each commit's changes one by one onto the current branch.
Final Answer:
It applies the changes from each specified commit onto the current branch. -> Option A
Quick Check:
Cherry-pick = copy specific commits [OK]
Hint: Cherry-pick copies commits, it does NOT merge branches [OK]
Common Mistakes:
Confusing cherry-pick with merge
Thinking it deletes commits
Assuming it creates a new branch
2. Which of the following is the correct syntax to cherry-pick multiple commits with hashes a1b2c3 and d4e5f6?
easy
A. git cherry-pick --all a1b2c3 d4e5f6
B. git cherry-pick a1b2c3,d4e5f6
C. git cherry-pick -m a1b2c3 d4e5f6
D. git cherry-pick a1b2c3 d4e5f6
Solution
Step 1: Review cherry-pick syntax
The correct way to cherry-pick multiple commits is to list their hashes separated by spaces, not commas or flags.
Step 2: Analyze options
git cherry-pick a1b2c3 d4e5f6 uses spaces between commit hashes without extra flags, which is the correct syntax.
Final Answer:
git cherry-pick a1b2c3 d4e5f6 -> Option D
Quick Check:
Multiple commits separated by spaces [OK]
Hint: Separate commit hashes by spaces, not commas [OK]
Common Mistakes:
Using commas between commit hashes
Adding unnecessary flags
Using incorrect options like --all
3. Given the following commands run on branch feature:
Fix the conflict, then run git cherry-pick --continue to proceed. -> Option A
Quick Check:
Fix conflicts + git cherry-pick --continue [OK]
Hint: Fix conflicts then run git cherry-pick --continue [OK]
Common Mistakes:
Skipping conflicts without fixing
Aborting instead of continuing
Resetting hard loses work
5. You want to cherry-pick commits 111aaa, 222bbb, and 333ccc from branch dev onto main. However, 222bbb depends on changes in 111aaa, but 333ccc is unrelated. Which command correctly cherry-picks only the dependent commits in order?
hard
A. git checkout main && git cherry-pick 333ccc 111aaa 222bbb
B. git checkout main && git cherry-pick 222bbb 111aaa
C. git checkout main && git cherry-pick 111aaa 222bbb
D. git checkout main && git cherry-pick 111aaa,222bbb
Solution
Step 1: Identify dependent commits
Since 222bbb depends on 111aaa, both must be cherry-picked in order to avoid errors.
Step 2: Choose correct command syntax and order
git checkout main && git cherry-pick 111aaa 222bbb checks out main and cherry-picks commits in the correct order separated by spaces.
Final Answer:
git checkout main && git cherry-pick 111aaa 222bbb -> Option C
Quick Check:
Dependent commits cherry-picked in order [OK]
Hint: Cherry-pick dependent commits in order, skip unrelated [OK]