Tuesday, September 23, 2014

GIT: remove last (few) commits

You should commit often, however sometimes I do a NEW commit instead making an amend to previous, that is especially important when working with tools like Gerit (online code review) when you can have only 1 commit, sometimes for a very long time.
This recipe works only if you did NOT PUSH.

Remove recent X (~1) commits without destroying your local changes:

git log
git reset --soft HEAD~1

at this time you can squash your multiple commits and commit with --amend flag.

$ git status
$ git add --all
$ git commit --amend