Code of the Day
IntermediateGit in practice

Stash and cherry-pick

Set work aside temporarily, and move individual commits between branches.

FundamentalsIntermediate8 min read
Recommended first
By the end of this lesson you will be able to:
  • Shelve unfinished work with git stash and restore it later
  • Copy a single commit onto another branch with cherry-pick
  • Recognise when each tool is the right fit

Two focused tools handle common "I'm in the middle of something" situations: parks unfinished work so you can switch contexts, and copies one specific commit from one branch to another.

git stash — park your work

You're halfway through a change when an urgent fix comes in on another branch. You don't want to commit half-finished work, but you can't switch branches with a dirty working tree. Stash it:

git stash            # shelve all uncommitted changes; working tree goes clean
git switch main      # deal with the urgent thing
git switch my-work
git stash pop        # bring your shelved changes back

git stash list shows everything you've stashed; pop applies the most recent and removes it from the list (apply keeps it). Think of it as a clipboard for work-in-progress.

git cherry-pick — copy one commit

Sometimes you want just one commit from another branch — a bug fix that's needed on main now, without merging the whole feature it lives in:

git switch main
git cherry-pick a1b2c3d   # apply that one commit here, as a new commit

Cherry-pick replays the change as a fresh commit on your current branch. It's perfect for backporting a fix, but use it sparingly — if you find yourself cherry-picking many commits, a merge or rebase is usually the right tool instead.

Do it yourself: start an edit, git stash, confirm git status is clean, then git stash pop to get it back. Then commit a fix on one branch and cherry-pick it onto another. Both are quick to feel out in a scratch repo.

Where to go next

Next: reviewing changes and pull requests — reading a proposed change critically, with or without an agent.

Finished reading? Mark it complete to track your progress.

On this page