Search code examples
gitgit-rebase

How to remove commits with rebase


I have the following commit tree :

D         (HEAD, origin/develop, develop)
| \
|  C
B  |
| /
A         (origin/master, origin/HEAD, master)

I'd like to remove commits B and C from history and keep the changes made into them. So D source files will be the same after the operations. Only the git history will be changed.

I tried : git rebase -i A from D with the options :

pick B 
pick C

then I have a merge error :

warning: Cannot merge binary files: MyFile.bin (HEAD vs. C... )

Automatic cherry-pick failed.  After resolving the conflicts,
mark the corrected paths with 'git add <paths>', and
run 'git rebase --continue'
Could not apply C... 

So I correct my conflicts, make a git rebase --continue and I get :

E           (HEAD, develop)
| D         (origin/develop)
| |\
|/ /
|  C
B  |
| /
A         (origin/master, origin/HEAD, master)

Whereas I'd like to have :

E  (or D, the sources files are the same in D & E)
|
A

I don't understand why B is still in the way from A to E. What should I do now ? I'm a little confused


Solution

  • Based on comments, I have done this :

    git rebase -i A
    
    pick B
    squash C
    

    Then I've corrected the conflicts and ran the git rebase --continue

    After that the tree looked like this :

    E         (HEAD, develop)
    | D       (origin/develop)
    | |\
    | | C
    | |/
    |/|
    | B
    |/
    A         (origin/master, origin/HEAD, master)
    

    To get the origin/develop to point on E, I've made a git push --force. After that, I had exactly what I wanted :

    E         (HEAD, origin/develop, develop)
    A         (origin/master, origin/HEAD, master)