gitrebasesquash

How to reorder commits to make a squash in git?


When doing interactive rebase for making squash and merge some commits into one I realized that when commits are not correlative this can not be done properly, this is an example suppossing we have 5 commits and we want to commit 2 with 4 and 5:

rebase -i HEAD~5

pick hash1 commit message 1
pick hash2 commit message 2
pick hash3 commit message 3
squash? hash4 commit message 4
squash? hash5 commit message 5

The problem is that squash applies to previous commit and in this example 3rd commit is on the way.

Is there a way to alter commit order to easily make the wanted squash?

Note: This is just an example, the question applies to N commits when they are unordered.


Solution

  • Disregarding conflicts, you can simply reorder the lines in your editor:

    pick oid1 commit summary 1
    pick oid2 commit summary 2
    squash oid4 commit summary 4
    squash oid5 commit summary 5
    pick oid3 commit summary 3