Skip to content

Cherry-Pick Commits

Medium ~30 min

What you'll build

A scenario where a bug fix committed to a feature branch is cherry-picked to main and a release branch without merging the whole feature.

Goals

  • Cherry-pick a single commit by hash
  • Cherry-pick a range of commits
  • Resolve conflicts during cherry-pick
  • Understand when to cherry-pick vs merge

Prerequisites

  • Basic Git commits and branches
  • Commit hashes (git log)
  • Conflict resolution basics

Steps

  1. 1

    Set up branches with a shared bug

    Create a feature branch with several commits, one of which is a bug fix. You want that fix on main now, without the rest of the feature.

    Code
    git checkout -b feature/big-feature
    echo 'feature work' >> feature.js && git add . && git commit -m 'Add feature'
    echo 'bug fix' >> bug.js && git add . && git commit -m 'Fix critical bug'
    git log --oneline  # note the hash of 'Fix critical bug'

    Expected result

    A feature branch with at least 2 commits; you've noted the bug fix hash.

  2. 2

    Cherry-pick the fix to main

    Switch to main and cherry-pick the bug fix commit by its hash. Git applies just that commit's changes, creating a new commit on main.

  3. 3

    Cherry-pick to a release branch

    Create a release branch (e.g. release/v1.2) from an older commit. Cherry-pick the same fix there so the released version also gets the bug fix.

  4. 4

    Handle a conflict

    If the cherry-pick conflicts (the surrounding code differs on the target branch), Git pauses. Resolve the conflict, git add the file, and git cherry-pick --continue.

  5. 5

    Cherry-pick a range

    Cherry-pick multiple commits at once with git cherry-pick A..B (exclusive of A, inclusive of B). Verify all commits land on the target branch.

    Code
    git cherry-pick <hash-A>..<hash-B>
    git log --oneline  # all commits in the range are now on this branch

    Expected result

    All commits in the range appear on the target branch.

Try it yourself

What you learned

You learned cherry-pick — the surgical tool for applying specific commits across branches, essential for maintaining release branches and backporting fixes.

Related Resources

Related Lessons

  • Branching

    Cherry-pick moves commits between branches.

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to topic