git squash

1. Add a global “squash” alias from bash

git config --global alias.squash '!f(){ git reset --soft HEAD~${1} && git commit --edit -m"$(git log --format=%B --reverse HEAD..HEAD@{1})"; };f'

2. ~/.gitconfig should now contain this alias:

[alias]
    squash = "!f(){ git reset --soft HEAD~${1} && git commit --edit -m\"$(git log --format=%B --reverse HEAD..HEAD@{1})\"; };f"

3. Usage

git squash N

… Which automatically squashes together the last N commits, inclusive.

Note: The resultant commit message is a combination of all the squashed commits, in order. If you are unhappy with that, you can always git commit –amend to modify it manually. (Or, edit the alias to match your tastes.)

About Tang

A mobile developer, work in Stockholm, Sweden
This entry was posted in git, Mac and tagged . Bookmark the permalink.

Leave a comment