Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save benduran/dd6292d60685a8f1d7be30ccd5943af3 to your computer and use it in GitHub Desktop.
Save benduran/dd6292d60685a8f1d7be30ccd5943af3 to your computer and use it in GitHub Desktop.
Git Squash alias, from amazingly clean explanation on stackoverflow

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).

From This answer

You can do this fairly easily without git rebase or git merge --squash.

If you want to write the new commit message from scratch, this suffices:

git reset --soft HEAD~3 &&
git commit

If you want to start editing the new commit message with a concatenation of the existing commit messages (i.e. similar to what a pick/squash/squash/…/squash git rebase -i instruction list would start you with), then you need to extract those messages and pass them to git commit:

git reset --soft HEAD~3 && 
git commit --edit -m"$(git log --format=%B --reverse HEAD..HEAD@{1})"

Both of those methods squash the last three commits into a single new commit in the same way. The soft reset just re-points HEAD to the last commit that you do not want to squash. Neither the index nor the working tree are touched by the soft reset, leaving the index in the desired state for your new commit (i.e. it already has all the changes from the commits that you are about to “throw away”).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment