Bird
Raised Fist0
Gitdevops~7 mins

Cherry-picking multiple commits in Git - Commands & Configuration

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
Introduction
Sometimes you want to take specific changes from one branch and apply them to another without merging everything. Cherry-picking multiple commits lets you copy those exact changes one by one to your current branch.
When you fixed bugs in a feature branch and want to apply only those fixes to the main branch without merging unfinished features.
When you want to bring specific improvements from a development branch to a release branch.
When you accidentally committed changes to the wrong branch and want to move them to the correct one.
When you want to apply a set of commits from a colleague's branch without merging their entire work.
When you want to selectively update your branch with important commits from another branch.
Commands
Switch to the branch where you want to apply the commits.
Terminal
git checkout main
Expected OutputExpected
Switched to branch 'main'
Apply the changes from the three commits with hashes abc1234, def5678, and 9ab0cde to the current branch one by one.
Terminal
git cherry-pick abc1234 def5678 9ab0cde
Expected OutputExpected
[main 1a2b3c4] Commit message for abc1234 Author: Author Name <author@example.com> Date: Thu Apr 25 10:00:00 2024 +0000 Commit message for abc1234 [main 2b3c4d5] Commit message for def5678 Author: Author Name <author@example.com> Date: Thu Apr 25 11:00:00 2024 +0000 Commit message for def5678 [main 3c4d5e6] Commit message for 9ab0cde Author: Author Name <author@example.com> Date: Thu Apr 25 12:00:00 2024 +0000 Commit message for 9ab0cde
Verify that the last three commits on the current branch are the cherry-picked commits.
Terminal
git log --oneline -3
Expected OutputExpected
3c4d5e6 Commit message for 9ab0cde 2b3c4d5 Commit message for def5678 1a2b3c4 Commit message for abc1234
Key Concept

If you remember nothing else from this pattern, remember: cherry-picking copies specific commits from one branch to another without merging the whole branch.

Common Mistakes
Trying to cherry-pick commits without switching to the target branch first.
The commits will be applied to the wrong branch, causing confusion and possible conflicts.
Always switch to the branch where you want the commits before running git cherry-pick.
Using incorrect or partial commit hashes in the cherry-pick command.
Git will fail to find the commits and show an error.
Use full or unique abbreviated commit hashes exactly as shown by git log.
Cherry-picking commits that depend on other commits not included.
This can cause conflicts or broken code because dependencies are missing.
Ensure all dependent commits are cherry-picked together or in the correct order.
Summary
Switch to the branch where you want to apply commits using git checkout.
Use git cherry-pick followed by multiple commit hashes to apply those commits one by one.
Verify the commits applied correctly with git log --oneline.

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

  1. 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.
  2. 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.
  3. Final Answer:

    It applies the changes from each specified commit onto the current branch. -> Option A
  4. 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

  1. 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.
  2. Step 2: Analyze options

    git cherry-pick a1b2c3 d4e5f6 uses spaces between commit hashes without extra flags, which is the correct syntax.
  3. Final Answer:

    git cherry-pick a1b2c3 d4e5f6 -> Option D
  4. 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:
git checkout feature
git cherry-pick 123abc 456def
What will happen if both commits apply cleanly?
medium
A. Only the first commit 123abc will be applied, the second will be ignored.
B. The changes from commits 123abc and 456def will be added to the feature branch in order.
C. Git will merge the branches containing those commits into feature.
D. An error will occur because multiple commits cannot be cherry-picked at once.

Solution

  1. Step 1: Understand cherry-pick with multiple commits

    When multiple commits are cherry-picked, git applies each commit sequentially if no conflicts occur.
  2. Step 2: Effect on current branch

    Since the branch is feature, the changes from both commits will be added in the order listed.
  3. Final Answer:

    The changes from commits 123abc and 456def will be added to the feature branch in order. -> Option B
  4. Quick Check:

    Multiple commits apply sequentially [OK]
Hint: Cherry-pick applies commits one by one in order [OK]
Common Mistakes:
  • Assuming only first commit applies
  • Confusing cherry-pick with merge
  • Thinking multiple commits cause errors
4. You run git cherry-pick abc123 def456 but get a conflict on the second commit. What should you do to continue cherry-picking the remaining commits?
medium
A. Fix the conflict, then run git cherry-pick --continue to proceed.
B. Abort the cherry-pick with git cherry-pick --abort and start over.
C. Run git reset --hard to discard changes and continue.
D. Use git cherry-pick --skip immediately without fixing conflicts.

Solution

  1. Step 1: Handle conflicts during cherry-pick

    When a conflict occurs, you must manually fix it before continuing.
  2. Step 2: Continue cherry-pick process

    After fixing conflicts, running git cherry-pick --continue resumes applying remaining commits.
  3. Final Answer:

    Fix the conflict, then run git cherry-pick --continue to proceed. -> Option A
  4. 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

  1. Step 1: Identify dependent commits

    Since 222bbb depends on 111aaa, both must be cherry-picked in order to avoid errors.
  2. 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.
  3. Final Answer:

    git checkout main && git cherry-pick 111aaa 222bbb -> Option C
  4. Quick Check:

    Dependent commits cherry-picked in order [OK]
Hint: Cherry-pick dependent commits in order, skip unrelated [OK]
Common Mistakes:
  • Cherry-picking commits in wrong order
  • Including unrelated commits unnecessarily
  • Using commas instead of spaces