James
James

Reputation: 521

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.

Upvotes: 5

Views: 5208

Answers (4)

Mehmet Recep Yildiz
Mehmet Recep Yildiz

Reputation: 2137

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.

Upvotes: 2

Himanshu Dhamija
Himanshu Dhamija

Reputation: 136

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

Upvotes: 7

Parth Sharma
Parth Sharma

Reputation: 105

git reset HEAD unstages all the staged files.

git checkout . discards all the unstaged changes.

Upvotes: 3

Marina Liu
Marina Liu

Reputation: 38106

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

git reset --hard HEAD

Upvotes: 2

Related Questions