How can I combine 2 commits into 1 in git in a branch?

703 views Asked by At

I was working in branch "Feature".
So my git tree was:

                  master
A<--B<--C<--D<--F<--G
            |<--E
                Feature  

I wanted to get all the latest of master while working on Feature branch so I did:

git checkout Feature  
git rebase master  

During the rebase I got a merge conflict in one file and during resolving the conflict manually I did a mistake which I realized later and not before running git rebase --continue
So I ended up with a tree as follows:

                 master      
A<--B<--C<--D<--F<--G<--E  
                       Feature

Then I realized about my mistake that actually broke the build in E.
I corrected my mistake (not commited yet) and continued my work but I don't want to have 2 commits in Feature that will end up in master and the one of the commits is a bad commit.
Also I would like to end up with a single commit in master branch of my work in Feature.
So if I have:

                 master      
A<--B<--C<--D<--F<--G<--E<--H  
                           Feature   

How can I combine E and H into one commit so that when I do:

git checkout master  
git rebase feature   

I have only 1 commit in my master? Note: The branch Feature is local and not pushed in case it matters.

1

There are 1 answers

2
Sébastien Dawans On BEST ANSWER

You can use interactive rebase to combine two commits. You need to use the squash option on the second one.

Start with:

git rebase -i HEAD~2

in the editor, you will see 2 lines in this order:

pick E's SHA-1
pick H's SHA-1

replace it with

pick E's SHA-1
squash H's SHA-1

Then close the editor. Git will pick E, then combine H to it.

You will be prompted for a new, combined commit message. Close that editor when finished and the rebase will finish by itself.