Git Foundations / Branch and Merge

Copy a commit with cherry-pick

Lesson 19 of 33. Open to read. No account needed.

One Commit, Not the Whole Branch

Sometimes a branch holds one commit you need right now and several you do not. A typical case: while building a feature, you fixed a bug that also affects main, but the feature itself is unfinished. Merging the branch would bring the unfinished work along.

git cherry-pick <commit> copies just that one commit's changes onto the branch you are on, as a new commit.

What the Copy Is

Git works out what the chosen commit changed compared with its parent, applies that change to your current branch and commits the result. The new commit has the same message and author as the original, but it is a different commit:

  • Its parent is your branch's previous tip, which is normally a different commit from the original's parent.
  • Its snapshot is your branch's files plus the change.

Because the ID is calculated from those details, the copy gets a new ID. The original stays where it was on the other branch. Add -x to record the original's ID in the copy's message, as a line saying (cherry picked from commit <ID>), which helps others trace where it came from.

When the Copy Conflicts

If your branch changed the same lines differently, the cherry-pick stops with a conflict, just like a merge or a revert. <<<<<<< HEAD introduces your branch's version and the closing marker names the commit being copied. Then:

git add <file>                # after resolving and checking the file
git cherry-pick --continue    # finish; your editor opens with the message
git cherry-pick --abort       # or give up and go back

If the change is already on your branch, for example because you picked the same commit twice, Git stops and says The previous cherry-pick is now empty. Run git cherry-pick --skip to leave it out.

Cherry-Pick or Merge?

Copying commits means the same change exists twice in the history, once on each branch, under different IDs. When the feature branch is merged later, Git usually combines the identical change without trouble, but the history now shows it twice, which can confuse anyone reading it.

Use cherry-pick for a specific commit you need now, such as a fix. When you want all of a branch's work, merge the branch instead: the history stays connected and nothing is duplicated.

Exercise: Copy One Fix to main

The gallery branch has unfinished gallery work and, in the middle, a fix for a typo in the site footer's email address. main needs the fix today; the gallery is not ready.

Starting state: Git configured as in the configuration lesson.

  1. Run the setup:

    mkdir -p ~/git-practice/cherry-pick-practice
    cd ~/git-practice/cherry-pick-practice
    git init -b main
    cat > index.md <<'EOF'
    # Photo studio
    EOF
    cat > footer.md <<'EOF'
    Email: studio@exmaple.com
    Phone: 555-0199
    EOF
    git add index.md footer.md
    git commit -m "Add studio site"
    git switch -c gallery
    cat > gallery.md <<'EOF'
    # Gallery
    
    Coming soon.
    EOF
    git add gallery.md
    git commit -m "Start photo gallery"
    cat > footer.md <<'EOF'
    Email: studio@example.com
    Phone: 555-0199
    EOF
    git commit -am "Fix footer email address"
    cat > gallery.md <<'EOF'
    # Gallery
    
    Captions to come.
    EOF
    git commit -am "Add gallery captions"
    git switch main
    
  2. Find the fix and confirm what it changes:

    git log --oneline gallery
    git show gallery~1
    

    "Fix footer email address" is one step behind the tip of gallery, so gallery~1 names it. Its ID from the log works just as well.

  3. Copy it onto main:

    git cherry-pick gallery~1
    

    Git prints the new commit with the original message.

  4. Check the result, and compare the copy's ID with the original's:

    git log --oneline
    ls
    cat footer.md
    git log --oneline -1 gallery~1
    

    main now has two commits, the newest being "Fix footer email address". ls shows no gallery.md, and the footer has the corrected address. The two "Fix footer email address" lines show different IDs.

  5. Try copying the same fix again:

    git cherry-pick gallery~1
    

    Git stops with The previous cherry-pick is now empty, because main already has the change. Leave it out:

    git cherry-pick --skip
    
  6. Now a copy that conflicts. main adds the phone's opening hours while gallery corrects the phone number on the same line. Try to copy the correction, then back out:

    cat > footer.md <<'EOF'
    Email: studio@example.com
    Phone: 555-0199 (weekdays)
    EOF
    git commit -am "Show phone hours"
    git switch gallery
    cat > footer.md <<'EOF'
    Email: studio@example.com
    Phone: 555-0100
    EOF
    git commit -am "Correct the phone number"
    git switch main
    git cherry-pick gallery
    git status
    cat footer.md
    git cherry-pick --abort
    git status
    

    The cherry-pick reports a CONFLICT in footer.md, and the file shows both phone lines between markers. After --abort, git status is clean and the footer is back to the (weekdays) version.

Success: main contains the email fix exactly once, has no gallery.md, and the conflicting copy was cleanly abandoned.

If something differs:

What you seeLikely causeNext step
The previous cherry-pick is now emptyThe change is already on this branchgit cherry-pick --skip
CONFLICT and could not applyThe same lines changed on this branchResolve, git add, git cherry-pick --continue; or --abort
gallery.md appeared on mainThe wrong commit was copied, or the branch was mergedgit log --oneline to see what was added; undo an unshared copy with git reset --hard HEAD~1
An editor opened during --continueGit asks you to confirm the messageSave and close to accept it
  1. Clean up:

    cd ~/git-practice
    ls
    rm -rf cherry-pick-practice
    ls
    

Independent task: before cleaning up, copy "Correct the phone number" onto main again, and this time resolve the conflict so the footer keeps both intentions: the corrected number and the opening hours. Finish the cherry-pick and confirm that main still has no gallery.md.

Check Your Understanding

  1. You cherry-pick commit a1b2c3d from feature onto main. Why does the new commit on main have a different ID?
  2. A teammate wants all six commits from a finished feature branch on main and plans to cherry-pick them one by one. What would you suggest, and why?

Answer and Reasoning

Independent task

git cherry-pick gallery
cat > footer.md <<'EOF'
Email: studio@example.com
Phone: 555-0100 (weekdays)
EOF
git diff --check
git add footer.md
git cherry-pick --continue
git log --oneline -3

Save and close the editor when it opens with "Correct the phone number". The newest commit on main is "Correct the phone number", the footer reads Phone: 555-0100 (weekdays), and ls still shows no gallery.md.

Understanding check

  1. A commit's ID is calculated from its contents, including its parent and snapshot. The copy's parent is main's previous tip, and its snapshot is main's files plus the change, so the ID differs even though the message and author are the same.
  2. Merge the branch instead. Merging brings in all the work in one step, keeps the history connected and avoids having every change twice under different IDs. Cherry-pick is for taking a specific commit without the rest.

References

Study this course with an account

Course access adds revision notes, the Quick Reference, saved progress, bookmarks and your own notes.

INR 99 plus 18% GST. 90 days of online access. No auto-renewal.

See course access

This website is operated by VECTAMETRIC EDTECH (OPC) PRIVATE LIMITED, operating as Vectametric.

Corporate Identity Number (CIN): U85499KA2026OPC229822. GSTIN: 29AAMCV8281D1ZS. Registered office: RS NO/38/2A/1 BASTAND, ROAD MAHANTESH NAGAR, Ramdurg, Ramdurg, Belgaum- 591123, Karnataka.

Queries and grievances: Prajval Honnungar, Grievance Officer and Data Protection Contact.

Telephone: +91 94800 09310. Support: support@vectametric.com. Grievances: support@vectametric.com.

Copyright 2026 VECTAMETRIC EDTECH (OPC) PRIVATE LIMITED. All rights reserved.

Vectametric home · Roadmaps · Vecta IT Exams · Vecta Government Exams · Vecta Banking & Finance Exams · Vecta Entrance Exams · About us · Contact · Privacy Policy · Terms and Conditions · No-Refund Policy