Skip to content
Bash

Git Cherry-pick

Selectively merge commits.

By EZ4Code Team
gitcherry-pick

Code

#!/bin/bash

# Basic usage: pick single commit
git cherry-pick <commit-hash>

# Pick multiple commits
git cherry-pick <hash1> <hash2> <hash3>

# Pick range (excluding start)
git cherry-pick <start>..<end>
git cherry-pick A..D  # B, C, D (excluding A)

# Pick range (including start)
git cherry-pick <start>^..<end>
git cherry-pick A^..D  # A, B, C, D

# Options
git cherry-pick --no-commit <hash>   # Apply changes without commit
git cherry-pick --edit <hash>        # Open editor to modify commit message
git cherry-pick -x <hash>            # Add (cherry picked from commit ...) info
git cherry-pick --signoff <hash>     # Add Signed-off-by

# Resolve conflicts
git cherry-pick <hash>
# On conflict:
# 1. Manually resolve conflict
# 2. git add .
# 3. git cherry-pick --continue
# Or: git cherry-pick --abort  # Abort
# Or: git cherry-pick --skip   # skip current

# Scenario: fix bug from feature to main
git checkout main
git cherry-pick <bugfix-commit>
git push origin main

# Backport fix from release to develop
git checkout develop
git cherry-pick <hotfix-hash> -x
git push origin develop

# Notes:
# 1. cherry-pick creates new commit (new hash)
# 2. May cause conflicts
# 3. -x records source for traceability

Explanation

git cherry-pick selectively applies specific commits to the current branch, commonly used for bug fix backporting and multi-branch maintenance.

More Bash Snippets