Git Undo

Git is the most popular version control tool. Easy to learn but hard to master it. This blog is to explain how to cancel your work if there is something wrong with your change no matter where the change you made.

Concept

git is more complex than other version control tool because it introduces the stage area. There are four area in git world: Working area, Stage, Local Repository and Remote Repository.

Check the diff

if the change is still in working area and has not yet been added into stage

1
$ git diff

if the change has been added and not yet been committed

1
$ git --cached

if the change has been committed and not yet been push to remote

1
$ git diff master origin/master

Undo

if the change is still in working area and has not yet been added into stage

1
$ git checkout .

if the change has been added and not yet been committed

1
2
$ git reset
$ git checkout .

if the change has been committed and not yet been push to remote

1
$ git reset --hard origin/master

if the change has been push to remote

1
2
$ git reset --hard HEAD^
$ git push -f