In Git, how can I recover a staged file that was reverted prior to committing?

If you had anything staged to git, you probably should be able to get that back. (If you just changed working copy, you wouldn’t be able to restore it.)

First of all: Backup your repository and working copy before going ahead. (Make sure to backup the .git directory.) Also avoid closing the terminal where this happened, and/or rebooting — if all fails, you have a chance to find stuff in history / memory. Do not run git gc.

Next, try:

git fsck --lost-found

It will print something like:

Checking object directories: 100% (256/256), done.
Checking objects: 100% (30165/30165), done.
dangling blob 8f72c7d79f964b8279da93ca8c05bd685e892756
dangling commit 4993502a6394491190d3f4d6fb3d1e14019c2e9b

Since you lost staged files and did not do a commit, you’re interested in dangling blob entries.

Run git show <sha> for each one — some of them should be your files.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)