Bird
Raised Fist0
Gitdevops~15 mins

Cherry-picking multiple commits in Git - Deep Dive

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
Overview - Cherry-picking multiple commits
What is it?
Cherry-picking multiple commits means selecting specific changes from one branch and applying them onto another branch. Instead of merging entire branches, you pick only the commits you want. This helps when you need just a few fixes or features without bringing all other changes. It is like copying only certain pages from one book into another.
Why it matters
Without cherry-picking, developers would have to merge whole branches even if they only want a few changes. This can cause unwanted code, conflicts, or bugs to come along. Cherry-picking allows precise control over what changes move between branches, keeping code clean and stable. It saves time and reduces errors in collaborative projects.
Where it fits
Before learning cherry-picking multiple commits, you should understand basic git concepts like commits, branches, and single commit cherry-picking. After mastering this, you can explore advanced git workflows like rebasing, patch management, and conflict resolution.
Mental Model
Core Idea
Cherry-picking multiple commits is selectively copying specific changes from one branch to another without merging everything.
Think of it like...
Imagine you have two notebooks. Instead of copying all pages from one notebook to another, you pick only the pages with notes you need and paste them into your other notebook.
Source Branch
  ├─ Commit A
  ├─ Commit B
  ├─ Commit C
  └─ Commit D

Target Branch
  └─ (before cherry-pick)

After cherry-picking Commits B and D:

Target Branch
  ├─ Commit B (copied)
  └─ Commit D (copied)
Build-Up - 7 Steps
1
FoundationUnderstanding single commit cherry-pick
🤔
Concept: Learn how to apply one commit from another branch onto your current branch.
Use the command `git cherry-pick ` to copy a single commit's changes onto your current branch. This applies the changes as a new commit, preserving history but not merging branches.
Result
The selected commit's changes appear on your current branch as a new commit.
Knowing how to cherry-pick one commit is the base skill needed before handling multiple commits.
2
FoundationIdentifying commits to cherry-pick
🤔
Concept: Learn how to find commit hashes and decide which commits to pick.
Use `git log --oneline` on the source branch to see commit hashes and messages. Choose the commits you want by their hashes to cherry-pick.
Result
You have a list of commit hashes ready for cherry-picking.
Being able to identify commits clearly prevents mistakes and ensures you pick exactly what you need.
3
IntermediateCherry-picking a range of commits
🤔Before reading on: do you think you can cherry-pick multiple commits by listing them one by one or by specifying a range? Commit to your answer.
Concept: Learn how to cherry-pick several commits in a row using a range.
Use `git cherry-pick ^..` to pick all commits from the start commit up to the end commit inclusive. For example, `git cherry-pick a1b2c3^..d4e5f6` picks commits from a1b2c3 to d4e5f6 inclusive.
Result
All commits in the specified range are applied in order onto your current branch.
Using ranges saves time and ensures commits are applied in the correct sequence without missing any.
4
IntermediateCherry-picking multiple non-sequential commits
🤔Before reading on: do you think git allows cherry-picking multiple commits that are not next to each other in one command? Commit to your answer.
Concept: Learn how to cherry-pick multiple commits that are not in a continuous range.
You can list multiple commit hashes separated by spaces: `git cherry-pick `. Git applies them in the order you list them.
Result
Selected commits, even if scattered, are applied one by one onto your branch.
Knowing you can pick non-adjacent commits in one command improves efficiency and flexibility.
5
IntermediateHandling conflicts during cherry-pick
🤔Before reading on: do you think conflicts during cherry-pick stop the process completely or allow manual resolution? Commit to your answer.
Concept: Learn what happens when cherry-picking commits causes conflicts and how to resolve them.
If a conflict occurs, git pauses cherry-pick and marks conflicted files. You must manually fix conflicts, then run `git cherry-pick --continue` to proceed. Use `git cherry-pick --abort` to cancel.
Result
Conflicts are resolved, and cherry-pick continues or is safely aborted.
Understanding conflict handling prevents stuck states and data loss during cherry-picking.
6
AdvancedUsing cherry-pick with commit ranges and options
🤔Before reading on: do you think cherry-pick can be combined with options like --no-commit or --edit? Commit to your answer.
Concept: Learn advanced cherry-pick options to control commit creation and editing.
Use `git cherry-pick --no-commit ` to apply changes without committing immediately, allowing you to combine or edit them. Use `--edit` to modify commit messages during cherry-pick.
Result
You gain fine control over how cherry-picked commits appear in history.
Mastering options lets you tailor cherry-picks for cleaner, more meaningful commit history.
7
ExpertAvoiding pitfalls with cherry-pick in complex workflows
🤔Before reading on: do you think cherry-picking multiple commits can cause duplicate commits or history confusion? Commit to your answer.
Concept: Understand risks like duplicate commits, conflicts, and history divergence when cherry-picking multiple commits in large projects.
Cherry-picking copies commits as new ones, which can cause duplicates if later merging branches. It can also cause conflicts if branches diverge. Experts use cherry-pick carefully with clear communication and sometimes prefer rebasing or feature toggles.
Result
You avoid confusing history and reduce merge conflicts in collaborative projects.
Knowing cherry-pick's impact on history helps prevent long-term maintenance headaches.
Under the Hood
Cherry-pick works by taking the changes introduced by a commit (the difference from its parent) and applying those changes as a new commit on the current branch. It does not move the original commit but creates a new one with the same changes and a new hash. This means history is rewritten locally, and the commit appears as if made on the current branch.
Why designed this way?
Git was designed to track changes as snapshots, not just patches. Cherry-pick creates new commits to keep history linear and consistent on the target branch. This avoids merging unrelated histories and allows selective change transfer. Alternatives like merging bring all changes, which is not always desired.
Source Branch
  ┌─────────────┐
  │ Commit A    │
  │ Commit B    │
  │ Commit C    │
  └─────────────┘
        │
        ▼
Cherry-pick extracts diff from Commit B
        │
        ▼
Target Branch
  ┌─────────────┐
  │ Commit X    │
  │ Commit B'   │  <-- new commit with same changes as B
  └─────────────┘
Myth Busters - 4 Common Misconceptions
Quick: Does cherry-picking a commit move the original commit to the new branch? Commit yes or no before reading on.
Common Belief:Cherry-picking moves the original commit from one branch to another.
Tap to reveal reality
Reality:Cherry-picking copies the changes as a new commit; the original commit stays on its branch.
Why it matters:Thinking it moves commits can cause confusion about history and lead to duplicate commits or merge conflicts.
Quick: Can cherry-picking multiple commits always be done without conflicts? Commit yes or no before reading on.
Common Belief:Cherry-picking multiple commits is always smooth and conflict-free.
Tap to reveal reality
Reality:Conflicts often happen, especially if commits touch the same code or branches diverged.
Why it matters:Ignoring conflict risk leads to broken code or stalled workflows.
Quick: Does cherry-picking preserve the original commit hashes? Commit yes or no before reading on.
Common Belief:Cherry-picked commits keep their original commit hashes.
Tap to reveal reality
Reality:Cherry-picked commits get new hashes because they are new commits on the target branch.
Why it matters:Misunderstanding this can cause problems with tracking changes and code reviews.
Quick: Is cherry-picking the best way to share many commits between branches? Commit yes or no before reading on.
Common Belief:Cherry-picking is always the best way to transfer multiple commits between branches.
Tap to reveal reality
Reality:For many commits or ongoing work, merging or rebasing is often better to keep history clean.
Why it matters:Overusing cherry-pick can clutter history and cause maintenance issues.
Expert Zone
1
Cherry-picking does not preserve commit metadata like author date or original branch context, which can affect auditing.
2
Using cherry-pick with --no-commit allows combining multiple commits into one, useful for cleaning history before pushing.
3
Cherry-picking can cause duplicate commits if the same changes are later merged, so tracking cherry-picks carefully is essential.
When NOT to use
Avoid cherry-picking when you need to transfer large sets of commits or entire features; prefer merging or rebasing instead. Also, avoid cherry-picking in public branches where history rewriting can confuse collaborators.
Production Patterns
In production, cherry-picking is often used to backport bug fixes from main branches to release branches. Teams use it carefully with clear commit messages and tracking to avoid duplication and conflicts.
Connections
Git Rebase
Cherry-picking and rebasing both rewrite commit history by applying commits onto a new base.
Understanding cherry-pick helps grasp rebasing because both involve replaying commits, but rebasing moves entire branches while cherry-pick selects specific commits.
Patch Management
Cherry-picking is like applying patches selectively, similar to how patch files are used in software maintenance.
Knowing cherry-pick clarifies how patches work as isolated changes applied to codebases.
Selective Copying in Document Editing
Cherry-picking is analogous to copying specific paragraphs from one document to another without copying the whole document.
This cross-domain link shows how selective transfer of information is a common pattern in many fields.
Common Pitfalls
#1Trying to cherry-pick a commit that depends on previous commits not included.
Wrong approach:git cherry-pick d4e5f6 # where d4e5f6 depends on earlier commits not cherry-picked
Correct approach:git cherry-pick a1b2c3^..d4e5f6 # cherry-pick the full range including dependencies
Root cause:Not recognizing commit dependencies causes broken or incomplete changes.
#2Ignoring conflicts and forcing cherry-pick to continue without resolution.
Wrong approach:git cherry-pick --continue # without fixing conflicts first
Correct approach:Resolve conflicts manually, then run git cherry-pick --continue
Root cause:Misunderstanding conflict resolution process leads to corrupted code.
#3Cherry-picking many commits one by one manually instead of using ranges or lists.
Wrong approach:git cherry-pick a1b2c3 git cherry-pick d4e5f6 git cherry-pick f7g8h9
Correct approach:git cherry-pick a1b2c3 d4e5f6 f7g8h9 # or use a range if sequential
Root cause:Lack of knowledge about cherry-pick syntax causes inefficiency.
Key Takeaways
Cherry-picking multiple commits lets you selectively copy changes from one branch to another without merging everything.
You can cherry-pick commits by specifying ranges or listing individual commits, handling both sequential and non-sequential cases.
Conflicts during cherry-pick require manual resolution before continuing, preventing broken code.
Cherry-picking creates new commits with new hashes, so it does not move or rename original commits.
Use cherry-pick carefully in production to avoid duplicate commits and confusing history; sometimes merging or rebasing is better.

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