Git Reset and Revert et al.

Git reset rewrites the git history to remove commits, stages, and even working directory changes. It should not be used on a public branch such as master.

There are three types of reset, soft, mixed, and hard. Soft resets the repository but leaves the staging are and working directory intact. Mixed, the default, resets the repository and the staging area. Hard resets all three and is the most destructive of the operations. We can reset by specific number of commits.

git reset --soft <SHA1>
git reset <SHA1>
git reset --hard <SHA1>
git reset HEAD~5

Git revert is a non-destructive way to undo specific commit commit in the git history. It writes a new commit that is the opposite of the reverted one. Because it is non-destructive, it is safe to use on public branches. When using revert, git will allow you to enter a commit message. There may be conflicts to resolve if we revert a commit before the last one.

git revert HEAD

Git can amend the very last commit in the case you made a typo or want to change the author information. This is a destructive operation as it creates a new commit and allows the old one to be garbage collected.

git commit --amend -m "New message for the last commit"
git commit --amend --author="John Doe <john.doe@example.com>"

 

Leave a Reply

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