Advanced

Stealing a single commit

Cherry-pick taught me that a commit's *change* is separate from the commit itself. I used it to rescue a fix I had made on the wrong branch, a mistake that became a lifeline.

What you will see

[main def7890] fix-on-feature 1 file changed, 1 insertion(+), 1 deletion(-) def7890 fix-on-feature abc4567 initial

About 2 min of reading

Quick check

Before this new lesson, one small question about the lesson you just finished. Recall it, do not look it up.

Try It

Copy a single commit from one branch onto another as a brand-new commit. I did this to rescue a fix I had made on the wrong branch, and it felt like stealing the change without the baggage.

1

Create a base commit on main, then a feature branch with its own fix. git log --oneline on the feature branch shows both commits.

printf 'v1\n' > app.py git add app.py git commit -m initial git checkout -b feature printf 'fixed\n' > app.py git add app.py git commit -m fix-on-feature git log --oneline

Before you run it, say out loud what it will print. Then open the answer.

What it prints
9ab1234 fix-on-feature abc4567 initial
Copy the short hash of `fix-on-feature`: the newer commit sitting on top of `initial`: before you continue.
2

Switch back to main and cherry-pick that one commit. A new commit with a new hash appears on top of main's initial, carrying exactly the change the fix made.

git checkout main git cherry-pick 9ab1234 git log --oneline git status --short

Before you run it, say out loud what it will print. Then open the answer.

What it prints
[main def7890] fix-on-feature 1 file changed, 1 insertion(+), 1 deletion(-) def7890 fix-on-feature abc4567 initial
The copy gets a fresh hash (def7890 here, not 9ab1234). The original commit on the feature branch is untouched; you only copied what it changed.

Put it in order

Before you write anything, put these lines back in the right order. Click a line to move it up one step. This support step is easier than writing code from scratch, and it is here to make the last example stick.

Data Flow

A commit's change is copied onto a new branch as a new commit, conflict or no conflict.
1 Identify the commit to copy
↓
2 Apply its change on the new branch
↓
3 Create a brand-new commit
↓
4 Resolve any conflict the surface
↓
5 Finish or quit the pick
Why this works
When cherry-pick is the right tool

Cherry-pick shines for targeted moves:

  • Applying a bug fix to a release branch that never had it.
  • Pulling a single great commit out of a messy feature branch.
  • Rearranging the order of a few commits.

git cherry-pick <hash>

Remember it copies the change, not the commit's identity. The copy gets a new hash, which matters if the original is still shared with other people.

Conflicts are part of the job

A pick does not always fit where you land it. When it does not:

git add .
git cherry-pick --continue

Resolve the conflict like any other merge, stage it, and continue. Git keeps the pick waiting until you finish, so it will remind you whenever you try to commit while a pick is half done.

Examples

Copy one commit onto this branch

git cherry-pick abc1234

This takes the change from one commit and applies it on top of your current branch as a brand-new commit with a new hash. The original commit stays exactly where it was; you have just made a copy of what it did. A clean way to move a fix without dragging along the whole branch.

Grab several in a row

git cherry-pick a b c

List the hashes you want and git applies them in order, each as its own new commit. I reach for this when a colleague sent me three commits I only need one of the changes from, and building them onto my branch by hand would have taken longer.

When you lose your nerve mid-pick

git cherry-pick --quit
git cherry-pick --continue

A pick can hit a conflict, and then you are not sure whether to push through or bail. --continue finishes after you resolve the conflict; --quit walks away cleanly and leaves your branch as it was before the pick started.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← Undoing yourself on purpose
Previous Lesson
Undoing yourself on purpose
Next Lesson
Cleaning up the mess before you share it
Cleaning up the mess before you share it →