Help cherry-pick commits across branches safely
✓Works with OpenClaudeYou are a Git workflow expert. The user wants to safely cherry-pick specific commits from one branch to another while avoiding conflicts and maintaining commit integrity.
What to check first
- Run
git log --oneline <source-branch>to identify the exact commits you want to cherry-pick - Run
git statusto ensure your working directory is clean before starting - Run
git branch -ato confirm both source and target branches exist and are up-to-date
Steps
- Fetch the latest changes from remote with
git fetch originto ensure both branches are current - Check out the target branch where commits will be applied:
git checkout <target-branch> - Identify the commit hash(es) from
git log <source-branch>— copy the full SHA or use shorthand (e.g.,a1b2c3d) - Run
git cherry-pick <commit-hash>for a single commit, orgit cherry-pick <hash1> <hash2> <hash3>for multiple commits in order - If conflicts occur, Git will pause and mark conflicted files — resolve them in your editor, then run
git add <resolved-file> - Continue the cherry-pick process with
git cherry-pick --continueafter resolving conflicts - Verify the commits were applied correctly with
git log --oneline -n 5to see the new commits on your branch - Push to remote with
git push origin <target-branch>once you're satisfied with the result
Code
#!/bin/bash
# Cherry-pick commits safely with conflict handling and verification
SOURCE_BRANCH="${1:-main}"
TARGET_BRANCH="${2:-develop}"
COMMITS=("${@:3}")
# Colors for output
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m'
# Ensure clean working directory
if ! git diff-index --quiet HEAD --; then
echo -e "${RED}Error: Working directory is not clean. Commit or stash changes first.${NC}"
exit 1
fi
# Fetch latest from remote
echo -e "${YELLOW}Fetching latest changes...${NC}"
git fetch origin
# Switch to target branch
echo -e "${YELLOW}Checking out $TARGET_BRANCH...${NC}"
git checkout "$TARGET_BRANCH" || exit 1
git pull origin "$TARGET_BRANCH"
# Cherry-pick commits
for commit in "${COMMITS[@]}"; do
echo -e "${YELLOW}Cherry-picking $commit...${NC}"
if git cherry-pick "$commit"; then
echo -e "${GREEN}Successfully cherry-picked $commit${NC}"
else
echo -e "${RED}Conflict detected in $commit. Resolve manually.${NC}"
git status
read -p "Press enter once conflicts are resolved: "
if git cherry-pick --continue; then
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Git & Version Control Skills
Other Claude Code skills in the same category — free to download.
Smart Commit
Generate conventional commit messages by analyzing staged changes
Branch Cleanup
Find and delete merged/stale local and remote branches
Git Undo
Safely undo the last git operation (commit, merge, rebase, etc.)
Changelog Generator
Generate CHANGELOG.md from git history using conventional commits
Conflict Resolver
Analyze and suggest resolutions for merge conflicts
Git Bisect Helper
Automate git bisect to find the commit that introduced a bug
PR Description
Generate detailed PR descriptions from branch diff
Commit Splitter
Split a large commit into smaller, logical commits
Want a Git & Version Control skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.