2

Is there a single command in GIT that I can use to squash last n commits together ?

As of now how I do this is , invoke the command

git rebase -i HEAD~n

then prefix the commits which I want to squash with s or squash , I am curious to know if there is a single command which achieves the same purpose?

1 Answer 1

5

Manual but with constant time for any number of commits option

git reset --soft HEAD~X 
git commit 

If you know beforehand you want to do this you can always type

git commit --fixup HEAD

instead of git commit while you are working. Then if you have rebase.autosquash configured, or if you pass in --autosquash rebase will do all the squashing for you.

If you like to live dangerously you can create an alias like the following which will do the squash for you. Basically you manually call commit-tree with the tree from current head, and create a commit pointing to HEAD~X, where you pass in X as an argument to an alias called squashx or something. Creating a commit this way doesn't update your branch, so you need to force update your current branch via update-ref. This form recycles the commit message from HEAD.

git update-ref HEAD $(git log --format=%B -n 1 HEAD | git commit-tree -p HEAD~$1 HEAD^{tree} -F -)
1
  • Thats a lot of information Andrew , Thanks a lot :)
    – Ajay
    Commented Sep 26, 2014 at 6:25

Not the answer you're looking for? Browse other questions tagged or ask your own question.