Search code examples
gitmergerebasesquash

Squashing latest commits into last merge commit


Here is my structure:

* - commit 2
|
* - commit 1
|
* - Merge commit (feature #1)
|\  
| * - Feature #1 commit 
|/
*

I messed up previously and now my history doesn't look as clean as I'd like.

I'd like to know if there is a simple way to squash 'commit 1' and 'commit 2' into 'Merge commit (feature #1)', so that I end up with something like this:

* - Merge commit (feature #1)
|\  
| * - Feature #1 commit 
|/
*

rebase -i doesn't seem to recognise the merge commit, and seems like it will squash everything into 'Feature #1 commit', which is not desirable.
Thanks


Solution

  • Make sure that you do not have anything staged (git added). Then just do

    git reset --soft HEAD~2       # go back across the two commits
    git commit --amend            # squash into the merge commit
    

    git reset --soft does not change what you have in the working directory nor what you have staged. Therefore, if you have git added content after you made "commit 2", then the git commit --amend would commit those staged content as well. For this reason, you have to make sure that you have nothing staged when you begin. git status should tell you whether that is the case.