Discarding local changes

To discard local changes (i.e. changes in our working directory) from a file that is already been tracked, we run

git restore file_name.ext

Same as before, Git will take the version of this file that is in the next environment. Since we are on the working directory now, the next environment is the staging area.


Note that file3.txt was not restored to its previous state. This is because this file is new, so there is no previous state. Going back to the "previous version" of this file means deleting it. To do this we run

git clean -fd
Discarding local changes 1

The -fd switch is necessary because by default Git will not allow us to delete un-tracked files (since they can not be recovered). The f switch is mandatory here and it means force. So we are forcing Git to go ahead with the removal. The d switch is necessary for deleting entire un-tracked sub-directories.