From This other answer on the same thread
Based on Chris Johnsen's answer:
I added this line to the [alias]
section of my git config file (~/.gitconfig
):
squash = "!f(){ git reset --soft HEAD~${1} && git commit --edit -m\"$(git log --format=%B --reverse HEAD..HEAD@{1})\"; };f"
Usage:
git squash N
... Which automatically squashes together the last N
commits, inclusive.
My previous solution was this `[alias]`:
squash = "!f(){ git rebase -i HEAD~${1}; }; f"
... which has the same usage, but requires you to edit the "git-rebase-todo" file (and change pick
to squash
).
@jordanst3wart please note that
--am
might throwerror: pathspec 'first-word-of-commit-msg' did not match any file(s) known to git
(well, at least it does for me 🤷) so-am
is the way to go 😉