git cherry-pick results in unmerged commits?

93 Views Asked by At

Attempting to cherry pick some commits.

On a subsequent cherry-pick, it printed out files indicated they were unmerged?

> git cherry-pick -n 08A7F306
MyClass1.java: unmerged (3632c18cc1522826e20e3a995cb2ec4bf3b3ab2b)
MyClass1.java: unmerged (89b187fadd2607fa85816a145a183da230aa873c)
MyClass1.java: unmerged (ac8765a8f2450aac261918af0f280b722843799f)
MyClass2.java: unmerged (6f9e8352ecf6d988583289b698c38cb927667848)
MyClass2.java: unmerged (96fd27c3f370fd9a2b721216d2f2fede02df1523)
MyClass2.java: unmerged (46b3c56a1513701474492ad0c73a4b5fa18d7c44)
error: your index file is unmerged.
fatal: cherry-pick failed

Seeing unmerged has me very concerned.

What does this mean?

Were some of the earlier commits actually unmerged? The commits listed aren't any that with which I have been working.

Are my files messed up now?

1

There are 1 best solutions below

2
VonC On BEST ANSWER

Were some of the earlier commits actually unmerged?

The term "unmerged" does not refer to the state of the commits themselves, but rather to the state of the affected files in your working directory/index during the cherry-pick operation. The commits you are trying to cherry-pick have already been merged in their original branches, but they are conflicting with your current working tree or index.

To resolve the merge conflicts, you would normally edit the conflicted files manually to resolve the differences, then add the resolved files to the index using git add <file>, and finally continue the cherry-pick operation using git cherry-pick --continue. Alternatively, if you wish to abort the cherry-pick operation and return to the state before you began, you can use the command git cherry-pick --abort.

The diff resolution would look like (using meld, but you can use Beyond Compare, KDiff3, etc.):

git switch -c temp-branch

git show 3632c18cc1522826e20e3a995cb2ec4bf3b3ab2b:MyClass1.java > MyClass1_base.java
git show 89b187fadd2607fa85816a145a183da230aa873c:MyClass1.java > MyClass1_ours.java
git show ac8765a8f2450aac261918af0f280b722843799f:MyClass1.java > MyClass1_theirs.java

meld MyClass1_base.java MyClass1_ours.java MyClass1_theirs.java
# Within the merge tool, resolve the conflicts between the three versions of the file. 
# Save the merged result back to MyClass1.java.

rm MyClass1_base.java MyClass1_ours.java MyClass1_theirs.java
git swich <original-branch>
git branch -D temp-branch

git add MyClass1.java
git cherry-pick --continue