Bird
Raised Fist0
Gitdevops~10 mins

git cherry-pick a single commit - Step-by-Step Execution

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
Process Flow - git cherry-pick a single commit
Start on target branch
Identify commit hash to pick
Run git cherry-pick <commit-hash>
Git applies changes from commit
Check for conflicts?
YesResolve conflicts
Continue cherry-pick
Commit applied on target branch
Done
The flow shows starting on the target branch, picking a commit by its hash, applying it, resolving conflicts if any, and finishing with the commit added.
Execution Sample
Git
git checkout feature-branch
# switch to target branch

git cherry-pick a1b2c3d4
# apply commit a1b2c3d4 changes

# If conflicts, resolve and run:
git cherry-pick --continue
This sequence switches to the target branch and applies a single commit from another branch by its hash.
Process Table
StepActionCommandResultNotes
1Switch to target branchgit checkout feature-branchSwitched to branch 'feature-branch'Ready to apply commit here
2Run cherry-pickgit cherry-pick a1b2c3d4Git applies commit changesIf no conflicts, commit added
3Check for conflictsAutomaticNo conflicts detectedCherry-pick successful
4Cherry-pick completeAutomaticCommit a1b2c3d4 added to feature-branchDone applying commit
💡 Cherry-pick ends after commit is applied or conflicts resolved and continued
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
HEAD branchmainfeature-branchfeature-branchfeature-branchfeature-branch
Commit historymain branch commitsfeature-branch commitsfeature-branch + a1b2c3d4 commitfeature-branch + a1b2c3d4 commitfeature-branch + a1b2c3d4 commit
Conflict statenonenonenonenonenone
Key Moments - 3 Insights
What happens if you run cherry-pick on the wrong branch?
The commit will be applied to the current branch HEAD points to, which may cause unwanted changes. See execution_table step 1 where switching branch is important.
What if there are conflicts during cherry-pick?
Git pauses and asks you to resolve conflicts manually, then you run 'git cherry-pick --continue' to finish. This is shown in the concept_flow with the conflict branch.
Does cherry-pick change the original commit hash?
No, but the new commit on the target branch gets a new hash because it is a new commit object. The original commit remains unchanged.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what branch is HEAD on after step 1?
Aa1b2c3d4
Bfeature-branch
Cmain
Ddetached HEAD
💡 Hint
Check the 'Result' column in step 1 of execution_table
At which step does git apply the commit changes?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at the 'Action' and 'Result' columns in execution_table for when changes are applied
If conflicts occur, what command should you run after resolving them?
Agit commit --amend
Bgit cherry-pick --abort
Cgit cherry-pick --continue
Dgit merge --continue
💡 Hint
Refer to the execution_sample code comments about conflict resolution
Concept Snapshot
git cherry-pick <commit-hash>
- Applies a single commit from another branch onto current branch
- Must be on target branch before running
- If conflicts, resolve then run 'git cherry-pick --continue'
- Creates a new commit with same changes but new hash
- Useful to selectively copy changes without merging
Full Transcript
To cherry-pick a single commit, first switch to the branch where you want the commit applied. Then run 'git cherry-pick' with the commit hash. Git applies the changes from that commit onto your current branch. If there are conflicts, git will pause and ask you to resolve them. After fixing conflicts, run 'git cherry-pick --continue' to finish. The cherry-picked commit is added as a new commit on your branch with a new hash. This lets you copy specific changes without merging entire branches.

Practice

(1/5)
1. What does the git cherry-pick command do?
easy
A. Creates a new branch from the current commit
B. Deletes a commit from the current branch
C. Merges two branches completely
D. Copies a specific commit from another branch to the current branch

Solution

  1. Step 1: Understand the purpose of git cherry-pick

    The command is used to copy a single commit from one branch to another without merging all changes.
  2. Step 2: Compare with other git commands

    Unlike merge, cherry-pick applies only one commit, not the entire branch history.
  3. Final Answer:

    Copies a specific commit from another branch to the current branch -> Option D
  4. Quick Check:

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

Solution

  1. Step 1: Recall the basic cherry-pick syntax

    The command is git cherry-pick <commit-hash> to apply a single commit.
  2. Step 2: Check the options given

    Only git cherry-pick abc123 matches the correct syntax without extra or incorrect flags.
  3. Final Answer:

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

    Correct syntax = git cherry-pick commit_hash [OK]
Hint: Use git cherry-pick followed by commit hash [OK]
Common Mistakes:
  • Adding unnecessary flags
  • Using incorrect keywords like 'commit'
  • Confusing with merge options
3. Given the following commands run on branch main:
git checkout main
git cherry-pick 1a2b3c4
What will happen after these commands?
medium
A. The commit with hash 1a2b3c4 is applied to main branch
B. A new branch named 1a2b3c4 is created
C. The main branch is reset to commit 1a2b3c4
D. The commit 1a2b3c4 is deleted from the repository

Solution

  1. Step 1: Understand the effect of git cherry-pick

    Running git cherry-pick 1a2b3c4 applies that commit's changes onto the current branch, here main.
  2. Step 2: Analyze other options

    No new branch is created, no reset happens, and commits are not deleted by cherry-pick.
  3. Final Answer:

    The commit with hash 1a2b3c4 is applied to main branch -> Option A
  4. Quick Check:

    Cherry-pick applies commit to current branch [OK]
Hint: Cherry-pick applies commit changes to current branch [OK]
Common Mistakes:
  • Thinking it creates branches
  • Confusing cherry-pick with reset
  • Assuming it deletes commits
4. You run git cherry-pick abcdef but get a conflict error. What should you do next?
medium
A. Abort the cherry-pick with git cherry-pick --abort and try again
B. Manually resolve the conflicts, then run git cherry-pick --continue
C. Delete the conflicting files and commit
D. Run git reset --hard to fix the conflict

Solution

  1. Step 1: Understand conflict during cherry-pick

    Conflicts mean git cannot automatically apply changes; manual resolution is needed.
  2. Step 2: Resolve conflicts and continue

    After fixing conflicts in files, run git cherry-pick --continue to finish applying the commit.
  3. Final Answer:

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

    Resolve conflicts + git cherry-pick --continue [OK]
Hint: Fix conflicts, then run git cherry-pick --continue [OK]
Common Mistakes:
  • Aborting without trying to fix
  • Deleting files instead of resolving
  • Using reset which discards changes
5. You want to apply a commit from branch feature to main without merging all changes. The commit hash is f1e2d3c. Which sequence of commands correctly does this?
hard
A. git checkout main git merge f1e2d3c
B. git checkout feature git cherry-pick f1e2d3c git checkout main
C. git checkout main git cherry-pick f1e2d3c
D. git checkout main git rebase feature

Solution

  1. Step 1: Switch to the target branch

    You must be on main to apply the commit there.
  2. Step 2: Cherry-pick the specific commit

    Run git cherry-pick f1e2d3c to copy that commit from feature branch.
  3. Step 3: Analyze other options

    Switching to feature, cherry-picking there, then switching back to main does not apply the commit to main, as cherry-pick affects the current branch. Merge and rebase apply entire branches, not a single commit.
  4. Final Answer:

    git checkout main git cherry-pick f1e2d3c -> Option C
  5. Quick Check:

    Checkout target branch + cherry-pick commit [OK]
Hint: Checkout target branch first, then cherry-pick commit [OK]
Common Mistakes:
  • Cherry-picking on wrong branch
  • Using merge or rebase instead of cherry-pick
  • Not switching branches before cherry-pick