Git examples: Resetting, Undoing and Reverting Changes

Last updated:
Table of Contents

Revert working directory to last commit

$ git reset --hard HEAD~1

Revert working directory to specific commit

You don't need to use the full commit hash, just the first 6 or 7 characters will do

$ git reset --hard <commit_hash>

That will make your working directory mirror what your Repository was like after commit <commit_hash>

Uncommit changes

All files will be exactly as they are, but the repository will be pointing to the previous commit

$ git reset --soft HEAD~1

Unstage changes

I.e. unadd added files:

$ git reset --soft HEAD

Add more changes to last commit

Just add new files and then run git commit --amend

To add file file1.txt to the previous commit:

$ git add file1.txt
$ git commit --amend

This will open up the previous commit message in case you want to edit it or keep the same message.

Dialogue & Discussion