4

I'm having issues with cherry-picking a range of commits and keep having conflicts and can't figure out what's the problem. The conflicts shouldn't occur because the commits being cherry-picked add/modify code which isn't present in the receiving branch.

Walkthough:

The commits I'm cherry-picking come from a branch called FeatureX, and the commits look like this:

>> git log --oneline

cbd0d94ca312 (HEAD -> FeatureX) Version 6
e1da344f20b7 Version 5
a9875369a8e4 Version 4
44d7975d975f Version 3
6955a667be84 Version 2
c7c70076c435 Version 1

Now, I have another branch called MainCode, on which I'd like to cherry-pick the above commits from FeatureX branch. If I cherry-pick them individually:

>>git cherry-pick c7c70076c435
[MainCode 7850ed1c4a4d] Version 1
 Date: Sat Jun 13 19:59:04 2020 -0700
 2 files changed, 321 insertions(+), 1 deletion(-)
 create mode 100644 STHelper.cs

>>git cherry-pick 6955a667be84
[MainCode df98023a9ac9] Version 2
 Date: Tue Jun 16 15:36:24 2020 -0700
 1 file changed, 68 insertions(+), 15 deletions(-)

>>git cherry-pick 44d7975d975f
[MainCode beede732db3f] Version 3
 Date: Tue Jun 23 14:34:27 2020 -0700
 1 file changed, 104 insertions(+), 23 deletions(-)

>>git cherry-pick a9875369a8e4
[MainCode 77664b1d36c2] Version 4
 Date: Wed Jul 8 21:25:27 2020 -0700
 1 file changed, 25 insertions(+), 15 deletions(-)

>>git cherry-pick e1da344f20b7
[MainCode 8eb1c3cf9828] Version 5
 Date: Fri Jul 10 03:39:47 2020 -0700
 1 file changed, 67 insertions(+), 19 deletions(-)

>>git cherry-pick cbd0d94ca312
[MainCode a07e9f4dbed9] Version 6
 Date: Fri Jul 10 14:28:10 2020 -0700
 3 files changed, 174 insertions(+), 35 deletions(-)
 create mode 100644 config_debug.xml

...it works fine. But if I try to cherry-pick them together ...

>>git cherry-pick c7c70076c435^..cbd0d94ca312
error: could not apply 6955a667be84... Version 2
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'
hint: and commit the result with 'git commit'

>>git status
On branch MainCode
Cherry-pick currently in progress.
  (fix conflicts and run "git cherry-pick --continue")
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

Unmerged paths:
  (use "git add/rm <file>..." as appropriate to mark resolution)
        deleted by us:   STHelper.cs

no changes added to commit (use "git add" and/or "git commit -a")

... there are conflicts. I also tried using their changes (i.e. changes from FeatureX branch) to try to resolve conflicts ...

>>git cherry-pick c7c70076c435^..cbd0d94ca312 --strategy=recursive -X theirs
error: could not apply 6955a667be84... Version 2
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'
hint: and commit the result with 'git commit'

... but it still doesn't work.

Can someone tell me what's the issue here ?

[edit] I'm using cmd.exe as a shell, on windows.

LeGEC
  • 46,477
  • 5
  • 57
  • 104
Ahmad
  • 12,886
  • 30
  • 93
  • 146
  • 1
    Does `git log --oneline c7c70076c435^..cbd0d94ca312` print the related commits in the same order with `git log --oneline`? – ElpieKay Jul 17 '20 at 08:44
  • Can you double check that the commit range you are specifying is correct ? the details of the conflict you describe seem to indicate that the first commit is not applied. – LeGEC Jul 17 '20 at 08:45
  • It could be a shell issue : if the shell you are using eats up the `^` from your command line, the first commit would be skipped – LeGEC Jul 17 '20 at 08:47
  • @ElpieKay, if I run the command you gave, while being on `Feature X` branch, I get the same order of commits as mentioned in my post. Only difference is that first commit (with hash `c7c70076c435`) isn't mentioned in that list. – Ahmad Jul 17 '20 at 08:49
  • @LeGEC, range is correct and is exactly as I've posted above. Regarding the shell, I'm not sure but I'm using Windows cmd.exe – Ahmad Jul 17 '20 at 08:50
  • 2
    Try `echo a^b` and you'll see the problem, the `^` character is part of the escape characters used by cmd, so it'll be stripped out. Try using `~1` instead. – Lasse V. Karlsen Jul 17 '20 at 08:55
  • would quoting the range work ? try `git log --oneline 'c7c70076c435^..cbd0d94ca312'` (with single quotes around the range) to see if the first commit appears. Or run your command from `git-bash` – LeGEC Jul 17 '20 at 09:02
  • Ah, you're a lifesaver @LasseV.Karlsen !! Thanks so much. That fixed the issue. If you create an answer, I'll vote for it ! :) – Ahmad Jul 17 '20 at 09:09

1 Answers1

6

It is a shell issue : as stated in the comments, you are using cmd.exe, in which ^ is an escape character
(see for example this link on ss64.com : Escape characters at the Windows command line).

Your input string :

c7c70076c435^..cbd0d94ca312

gets turned into :

c7c70076c435..cbd0d94ca312

and the first commit gets skipped.


If you stick with cmd.exe : use ~1 (as @LasseVKarlsen suggested) or ^^ instead of a plain ^ :

git cherry-pick c7c70076c435^^..cbd0d94ca312
LeGEC
  • 46,477
  • 5
  • 57
  • 104