Search code examples
gitgit-rebasegit-cherry-pick

git cherry-pick and rebase fails


I have the following commits in the history of my topic branch:

(git lola shortcut described here)

jochen@autolaptop4:~/projects/infoodle$ git lola
*   0c61616 (refs/stash) WIP on receipt_preview_sprint_to_finish: 3678770 progress
|\  
| * 332dc8a index on receipt_preview_sprint_to_finish: 3678770 progress
|/  
* 3678770 (HEAD -> receipt_preview_sprint_to_finish, origin/receipt_preview, receipt_preview, integration) progress
*   ed2ca95 Merge branch 'receipt_preview' of bitbucket.org:richinnz/infoodle-web into receipt_preview
|\  
| * 0743777 preview stuff
| * 03a2279 be able to ahndle both rebatable and non-rebatable
| * 1ff3d6c better sample number for preview of receipts
| * 0a0f3ed handle missing {tax receipt} replacement
| * 0ce35c9 remove language files, should not be in git
| * 5cc2b61 identify first key of transaction detail correctly to get data out of transaction record
| * def1132 sort out preview spinner
| * 5622f85 typo when pasting code from master
* | 30ef79c (origin/receipt_search, receipt_search) merge transactiontranslator back into receiptconfigurator
* | 367685c progress transferring sql into configurator
* |   84c71b1 Merge remote-tracking branch 'origin/receipt_search' into receipt_search
|\ \  
| * | 149e5f0 Progress on receipt screen search/ sort/ detail
* | | e927458 processing receiptstodo query into receiptList class, process SQL where parts into ReceiptConfigurator
* | | 80c7c06 list loaded from ajax complete
* | | 99b6ed8 only use global.min when not in debug mode
* | | bf15181 rename
* | | 43fd17a re-indent
* | | 57e38a0 re-indent
* | | c4e7588 save work
| |/  
|/|   
* | 867c918 remove confusing commented out stuff
* | fec8c04 text tweak in phpdoc
|/  
* 75a78ce fix mismatch of function parameter typing after merge

Basically I have accidentally merged origin/receipt_search and we had a change of plan in regards to getting this feature branch finished.

I want to start now with commit 75a78ce (at bottom) and apply

0743777 preview stuff
03a2279 be able to ahndle both rebatable and non-rebatable
1ff3d6c better sample number for preview of receipts
0a0f3ed handle missing {tax receipt} replacement
0ce35c9 remove language files, should not be in git
5cc2b61 identify first key of transaction detail correctly to get data out of transaction record
def1132 sort out preview spinner
867c918 remove confusing commented out stuff
fec8c04 text tweak in phpdoc

in reverse order onto a new branch.

1) I have tried git cherry-pick:

git checkout 75a78ce
git checkout -b receipt_preview_sprint_to_finish
git cherry-pick fec8c04..0743777

This fails when applying the second commit:

On branch receipt_preview_sprint_to_finish
You are currently cherry-picking commit 867c918.
  (fix conflicts and run "git cherry-pick --continue")
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

Unmerged paths:
  (use "git add <file>..." to mark resolution)

        both modified:   code/classes/class.receipting.php

I don't understand why there would be a conflict.

2) Then I tried rebase:

git branch -f integration 0743777
git rebase --onto receipt_preview_sprint_to_finish fec8c04~1 integration

    First, rewinding head to replay your work on top of it...
    Fast-forwarded integration to receipt_preview_sprint_to_finish.
    jochen@autolaptop4:~/projects/infoodle$ git log
    commit 3678770d92b2fd00797d2cda2875c090fc701a1e
    Author: Jochen Daum <[email protected]>
    Date:   Thu Mar 23 10:48:42 2017 +1300

        progress

    commit ed2ca95096690c4c419ef491ad65c3c5020120e5
    Merge: 30ef79c 0743777
    Author: Jochen Daum <[email protected]>
    Date:   Thu Mar 23 08:26:23 2017 +1300

        Merge branch 'receipt_preview' of bitbucket.org:richinnz/infoodle-web into receipt_preview

        # Conflicts:
        #   code/ajax/accountcode_functions.php
        #   code/ajax/subinclude/person.php
        #   code/styles/ennz/admin_donationreceipts.tpl.php
        #   code/styles/ennz/header.tpl

But these 2 commits I specifically did not want.

What am I doing wrong?


Solution

  • The problem with the git cherry-pick is simple:

    I want to start now with commit 75a78ce (at bottom) and apply [commits starting with, and including, fec8c04, so I ran]

    git cherry-pick fec8c04..0743777
    

    The notation X..Y in Git means "all commits reachable from Y, excluding all commits reachable from X". This excludes X itself. It's reminiscent of half-open intervals in maths, where [3..5] means 3, 4, and 5 but (3,5] means 4 and 5 only, or [3, 5) means 3 and 4 only. (These are written other ways, such as ]3, 5] in some notations, and commits are not really linear this way—we're actually doing set subtraction, rather than intervals—but the idea here is to act as a reminder that X..Y never includes commit X itself.)

    Hence, what you need is:

    git cherry-pick 75a78ce..0743777
    

    or:

    git cherry-pick fec8c04^..0743777
    

    to include commit fec8c04.

    Worthy of note as well is that this is not quite right:

    git checkout 75a78ce
    git branch -b receipt_preview_sprint_to_finish
    

    The second command should be git checkout -b, not git branch -b. But git branch -b would give you an error so I guess you actually used git checkout -b. :-)

    (The git rebase method is not going to work well as it will select far too many commits to copy into a linear sequence.)