Search code examples
gitcommitsquash

How to squash a lot of commits automatically?


I have a lot of commits that I want to squash together into one commit. Of course I may replace pick with squash for every commit, but I have a hundreds commits. Is there a way to do this automatically?


Solution

  • If you have a sequence of commits

    ... - C1 - C2 - C3 - C4 - C5 <- HEAD
    

    and you want to squash C2 to C5 into a single commit, you can reset your branch to C1 while keeping the state of your working directory and staging area, ans then commit again:

    git reset --soft C1
    git commit
    

    This will require you to re-enter a commit message. You can of course use git log before resetting and copy the parts of the commit messages you want to keep.

    If you want to squash a feature branch into a single commit ontop of the master branch, another option is to use the --squash option to git merge.