Search code examples
gitgit-rebasegit-squash

Combine 2 commits that are NOT the last 2 commits on my branch


I have to following 4 commits:

#Commit4
#Commit3
#Commit2
#Commit1

After making commits 3 and 4 I realized my first 2 commits really should be a single commit like so:

#Commit4
#Commit3
#Commit1_and_commit2

Is an interactive rebase recommended for this or is there a better approach? Steps to follow would be great. Thanks.


Solution

  • Interactive rebase is far and away the easiest approach (though in fact it does nothing you couldn't do in some more difficult, manual way). You will need to say

    git rebase -i --root
    

    if commit 1 really is the very first commit. If not, you need to say

    git rebase -i parentOfCommit1
    

    You will then be shown a pick list of your four commits:

    pick
    pick
    pick
    pick
    

    You will change that to

    pick
    squash
    pick
    pick
    

    and you'll be all set.