Git – revert or amend last commit

Since we moved from SVN to git in HeiaHeia I had to revert or amend changes I accidentally committed or committed and pushed to git repository. This is not the most common operation, so I have to browse the documentation every time I do that. This is more of a memo to myself, which hopefully will be useful to others too.

Committed some changes, didn’t push them, and need to amend the commit:

git commit --amend -a -m "Commit message"

Committed some changes, pushed them, and need to amend the commit, do the revert operation instead, since someone might’ve already used your changes.

Committed some changes, didn’t push them, and need to undo the commit:

git reset --hard HEAD^

This will just toss away the last commit completely.

Commited some changes, pushed them, and need to undo the commit:

git revert HEAD

This will automatically create a new commit, reverting the changes from the previous


Comments

2 responses to “Git – revert or amend last commit”

  1. […] Continue reading here: Ivan Kuznetsov › Git – revert or amend last commit […]

  2. It was useful to me too, thanks

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.