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.
-
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 -
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, sogallery~1names it. Its ID from the log works just as well. -
Copy it onto
main:git cherry-pick gallery~1Git prints the new commit with the original message.
-
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~1mainnow has two commits, the newest being "Fix footer email address".lsshows nogallery.md, and the footer has the corrected address. The two "Fix footer email address" lines show different IDs. -
Try copying the same fix again:
git cherry-pick gallery~1Git stops with
The previous cherry-pick is now empty, becausemainalready has the change. Leave it out:git cherry-pick --skip -
Now a copy that conflicts.
mainadds the phone's opening hours whilegallerycorrects 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 statusThe cherry-pick reports a
CONFLICTinfooter.md, and the file shows both phone lines between markers. After--abort,git statusis 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 see | Likely cause | Next step |
|---|---|---|
The previous cherry-pick is now empty | The change is already on this branch | git cherry-pick --skip |
CONFLICT and could not apply | The same lines changed on this branch | Resolve, git add, git cherry-pick --continue; or --abort |
gallery.md appeared on main | The wrong commit was copied, or the branch was merged | git log --oneline to see what was added; undo an unshared copy with git reset --hard HEAD~1 |
An editor opened during --continue | Git asks you to confirm the message | Save and close to accept it |
-
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
- You cherry-pick commit
a1b2c3dfromfeatureontomain. Why does the new commit onmainhave a different ID? - A teammate wants all six commits from a finished feature branch on
mainand 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
- 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 ismain's files plus the change, so the ID differs even though the message and author are the same. - 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.