Notes:

  • For COMMIT below, use HEAD~3 for 3 commits back, or use a hash for a particular commit.
  • If you’ve already pushed code, you’ll have to force push to overwrite with git push --force.

Reset approach

$ git reset --soft COMMIT
$ git commit

That will add a commit with all your changes.

Use this if you want to replace that existing commit, including its changes and everything you did after:

$ git commit --amend

More advanced approach with a script with validation here on StackOverflow.

Rebase approach

This requires more manual work.

$ git rebase -i COMMIT

Then mark each commit as s for squash.

Save and close.

Merge a branch

$ git merge --squash BRANCH

See SO thread for using this approach in a more advanced way.