Search code examples
gitbranchsquash

Git squash commits with merged branch


I am working on master branch, and had created a fix branch which merged back to master after job done:

  C ---.      | fix
 /      \
A -- B -- D   | master

By git log I can see:

commit 1992d90ff860b7f965d1cf24a199e2575f7761a9
Merge: 3ef8294 d2d8796
Date:   Fri May 30 13:07:02 2014 +0800

    Merge branch 'fix' -- D

commit d2d8796bc12a85b9948eee8d63fcbbf3b89d4aeb
Date:   Thu May 29 10:57:13 2014 +0800

    C

commit 3ef8294ce739f5f7524084b0b604132e40cf5fd9
Date:   Fri May 16 14:35:11 2014 +0800

    B

commit bfc45852466de4c3dcedc0ea24d8506312004e40
Date:   Fri May 16 10:31:39 2014 +0800

    A

Now I would like to squash commits of C,D into one commit:

A -- B -- E    | master

Usually I squash commits by rebase:

git rebase -i commit_of_B

But it is not working in this case, git complains:

error: could not apply d2d8796... C

Is there any way to do this?


Solution

  • Well, just found a way by cherry-pick with option -m:

    git checkout -b work
    git reset --hard HEAD^
    git cherry-pick -m 1 1992d90ff
    

    Now the branch is like:

    A -- B -- D    | master