Is it possible to revert unsaved files after git checkout. or git reset –hard?

Question:

By mistake, I deleted the changes that I did not have time to save in the commit . Is there any way to restore the changes to the files?

Erased in one of the following ways:

  • git checkout .
  • git reset --hard
  • Just deleted files from disk

(in fact, if there was no commit, the way of deleting does not affect the possibility of recovery in any way)

Answer:

How to prevent

To prevent this from happening to you again, undo the changes in a safe way:

git stash save

This command will undo all changes in the work area and in the index (relative to HEAD ) and store them in stash. Then it can be used or removed.

# использовать
git stash apply

# удалить
git stash drop

How to recover

First, check the history and temp files of your text editor or development environment. Many of them make their own backups on the fly in case of unforeseen circumstances.

If the modified version of the files was first added (indexed, git add ), then like this:

git fsck --lost-found

Each found object will be uploaded to the .git/lost-found/other/ folder in the same form in which it was (i.e. not a blob), but with sha1 corresponding blob instead of a name. Thus, the contents of the files can be restored, the names cannot.

If the files weren't even indexed and git checkout . – then there is no way to recover by means of Git.

Scroll to Top