Search code examples
gitsquash

How to squash the last N commits into a single commit in git?


I actually want to squash around 5-6 commits in a single commit. What is the best option I have in this case?


Solution

  • With the most recent commit checked out, do a soft reset to your desired parent commit. If the commits are linear, The parent of the last N commits is HEAD~N. For example, to combine the last five commits:

    git reset --soft HEAD~5
    

    Optionally, make sure you've got the right changes with git status or git diff.

    Then create the new commit:

    git commit -m 'five combined commits in one'