'Git how to revert staged /unstaged modified file

In Git(2.16). Suppose, I modified a single file and did not stage yet I would like to revert the file back to original? Also, how can I accomplish this on a staged file? Thanks.



Solution 1:[1]

git reset HEAD fileName will unstage the file in staging directory, but the changes will still be present in your working directory. Now, if you want the original copy of the file, you can use -

git checkout fileName

Solution 2:[2]

You can use below command to revert modified files (both for staged and unstaged) as original:

git reset --hard HEAD

Solution 3:[3]

git reset HEAD unstages all the staged files.

git checkout . discards all the unstaged changes.

Solution 4:[4]

For undoing changes in the unstaged state for all files

$ git restore . 

For undoing changes in the unstaged state for a single file

$ git restore chapter1.txt

For unstaging the changes in staged state for all files

$ git reset
$ git reset head

For unstaging the changes in staged state for a single

    $ git reset songs.txt

For undoing changes in the unstaged & staged state for all files

$ git reset --hard
$ git checkout head .

For undoing changes in the unstaged & staged state for a single file

    $ git checkout head chapter1.txt

There are lots of other ways of accomplishing these tasks too.

But, these causes confusions.

I prefer this structure and commands.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Himanshu Dhamija
Solution 2 Marina Liu
Solution 3
Solution 4