Post

Git Cherry-Pick

Git Cherry-Pick

What is it?

Cherry-pick takes a commit from another branch and creates a new commit with the same changes on your current branch. It’s like copy-pasting commits selectively.

Basic Usage

1
2
3
4
5
6
7
8
# Pick a single commit
git cherry-pick <commit-hash>

# Pick multiple commits
git cherry-pick commit1 commit2 commit3

# Pick a range of commits
git cherry-pick start-commit..end-commit

Common Use Cases

1. Apply hotfix to multiple branches

1
2
git checkout main
git cherry-pick hotfix-commit-hash

2. Backport features to older releases

1
2
git checkout release-v1.0
git cherry-pick new-feature-commit

3. Select specific commits from a feature branch

1
2
git checkout main
git cherry-pick feature-branch~2  # Pick specific commit

Useful Flags

1
2
3
4
5
6
7
8
# Add reference to original commit
git cherry-pick -x <commit-hash>

# Don't auto-commit (stage changes only)
git cherry-pick -n <commit-hash>

# Edit commit message
git cherry-pick -e <commit-hash>

Quick Example

Say you have this structure:

1
2
3
main:     A---B---C
               \
feature:        D---E---F

To apply only commit E to main:

1
2
3
git checkout main
git log --oneline feature  # Find commit hash
git cherry-pick e4d5c6b    # Apply specific commit

Result:

1
2
3
main:     A---B---C---E'
               \
feature:        D---E---F

Key Points

  • Creates new commits with different hashes
  • Non-destructive - original commits stay intact
  • Use -x flag to track where commits came from
  • Best for selective commits, not entire branches

Perfect for hotfixes, backporting, and selective feature integration!

This post is licensed under CC BY 4.0 by the author.