You have changed some files and added some new ones to your project and want to undo that work to go back to the state of the last commit. But git has no 'undo' command. In this article, you learn how to combine git reset and git clean to mimic an undo command.

Git undo. Discard changes and remove untracked files.

Execute 'git status'. Your terminal should show something like this:

$ git status
On branch master
Changes not staged for commit:
  modified:   shop/admin.py
  deleted:    shop/views2.py

Untracked files:
  shop/visitor.py

There are two type of changes:

Discard changes and remove new files (undo)

To undo all work since the last commit, perform these two commands:

git reset --hard
git clean -fd

Execute git status again. Your terminal should show something like this:

$ git status
On branch master
nothing to commit, working tree clean

And all is good again.

Written by Loek van den Ouweland on 2022-07-16.
Questions regarding this artice? You can send them to the address below.