This error occurs when cherry-picking a merge commit without specifying which parent to use as the base. Use the -m option to specify the parent number (typically -m 1 for the main branch).
When you run `git cherry-pick` on a merge commit, Git doesn't know which side of the merge should be considered the "mainline" or base. Unlike regular commits that have a single parent, merge commits have two or more parents representing different lines of development. Cherry-picking works by calculating the diff between a commit and its parent, then applying that diff to your current branch. With a merge commit, there are multiple parents to choose from, and each choice produces a different set of changes. For example, if you merged a feature branch into main, parent 1 would be the main branch and parent 2 would be the feature branch. Git requires you to explicitly specify which parent to use via the `-m` option. Using `-m 1` tells Git to treat the first parent as the base and apply the changes that came from the second parent (typically the merged branch). Using `-m 2` would do the opposite—it applies changes that existed in the first parent but not in the second.
First, verify that the commit you're trying to cherry-pick is indeed a merge commit:
git show --pretty=raw <commit-hash>Look for a line starting with Merge: which shows the parent commit hashes. For example:
commit abc1234...
Merge: def5678 ghi9012If you see two or more parent hashes, it's a merge commit and requires the -m option.
The parent number determines which changes get applied:
- Parent 1 (-m 1): Usually the branch being merged INTO (e.g., main/master)
- Parent 2 (-m 2): Usually the branch being merged FROM (e.g., feature branch)
To see what each parent represents:
git log --oneline -1 <parent1-hash>
git log --oneline -1 <parent2-hash>In most cases, you want -m 1 because you want to apply the changes that were introduced by the feature branch (the diff between parent 1 and the merge).
Run the cherry-pick with the appropriate parent number:
git cherry-pick -m 1 <merge-commit-hash>This tells Git to use the first parent as the base and apply the changes from the merged branch.
If you want the opposite (apply changes from the first parent's perspective):
git cherry-pick -m 2 <merge-commit-hash>If conflicts occur during the cherry-pick, resolve them as usual:
# See which files have conflicts
git status
# Edit conflicted files to resolve
# Then mark them as resolved
git add <resolved-file>
# Continue the cherry-pick
git cherry-pick --continueOr abort if needed:
git cherry-pick --abortOften a better approach is to cherry-pick the individual commits from the merged branch rather than the merge commit itself:
# Find commits in the merged branch
git log --oneline <parent1>..<parent2>
# Cherry-pick each commit individually
git cherry-pick <commit1> <commit2> <commit3>Or cherry-pick a range:
git cherry-pick <oldest-commit>^..<newest-commit>This preserves the original commit structure and history.
The same -m option applies when reverting merge commits:
git revert -m 1 <merge-commit-hash>This creates a new commit that undoes the changes introduced by the merge. Use -m 1 to revert the changes that came from the merged branch while keeping the mainline changes.
After a successful cherry-pick, verify the changes are correct:
# View the new commit
git log -1 --stat
# Compare with expected changes
git diff HEAD~1
# Run tests to ensure the cherry-picked changes workNote that cherry-picking a merge commit creates a single commit containing all the squashed changes, losing the individual commit history from the original branch.
### Understanding Parent Order
When you run git merge feature-branch while on main:
- Parent 1 = the commit that was HEAD on main before the merge
- Parent 2 = the tip of feature-branch
The merge commit represents a tree that combines both parents. The Merge: line in git show displays parents in order (parent1 parent2).
### Warning: Loss of History
When you cherry-pick a merge commit with -m 1, you're collapsing all the changes from the merged branch into a single commit. You lose:
- Individual commit messages
- Commit authorship information
- Bisect-ability of those changes
If preserving history matters, cherry-pick the individual commits instead.
### Octopus Merges
For octopus merges (merging more than two branches), you can specify any parent number (-m 1, -m 2, -m 3, etc.). The order follows how they were specified in the merge command.
### Scripting Considerations
When scripting cherry-picks, check if a commit is a merge before attempting:
if git rev-parse --verify ${commit}^2 >/dev/null 2>&1; then
# It's a merge commit, use -m 1
git cherry-pick -m 1 ${commit}
else
# Regular commit
git cherry-pick ${commit}
fi### GUI Tool Workarounds
Many Git GUIs (SourceTree, GitKraken, VS Code) don't expose the -m option. Use the command line for cherry-picking merge commits, or cherry-pick the individual commits from the branch instead.
warning: BOM detected in file, this may cause issues
UTF-8 Byte Order Mark (BOM) detected in file
fatal: Server does not support --shallow-exclude
Server does not support --shallow-exclude
warning: filtering out blobs larger than limit
Git partial clone filtering large blobs warning
fatal: Server does not support --shallow-since
Server does not support --shallow-since in Git
kex_exchange_identification: Connection closed by remote host
Connection closed by remote host when connecting to Git server