Recover Files Git Deleted (reset --hard deleted files)

Viewed 112

Mistakes were made, yes.

My storage folder of my application was committed to my repo and when I tried to sync up a new update that I committed to my repo and sent to my server I ran git reset --hard && git clean -df.

Little did I know that any files in my storage folder that weren't in my previous commit (which had the storage folder committed WITH files) - would be deleted.

forge@onaxim-crm:~/api.crm.onaxim.com.au$ git reset --hard && git clean -df
Checking out files: 100% (1049/1049), done.
HEAD is now at 6776b4d v2.3updates
Removing storage/customers/1/
Removing storage/customers/1017/
Removing storage/customers/1032/
Removing storage/customers/1039/
Removing storage/customers/1044/
Removing storage/customers/1058/
Removing storage/customers/1084/
Removing storage/customers/1086/
Removing storage/customers/1094/
Removing storage/customers/1095/
Removing storage/customers/1101/
Removing storage/customers/1102/
Removing storage/customers/1106/
Removing storage/customers/1109/
Removing storage/customers/1111/
Removing storage/customers/1112/
Removing storage/customers/1113/
Removing storage/customers/1114/
Removing storage/customers/1115/
Removing storage/customers/1116/
Removing storage/customers/1117/

I check the directory and the field have been removed so is there any way to recover them?

I read something about maybe using git fsck --lost-found?

1 Answers

A git reset --hard does not move HEAD to a different commit, so if sync involved a pull, you might still see your content in the previous commit (the one before pull) with, for testing:

git restore @~ -- .

My storage folder of my application was committed to my repo

If by "committed", you mean only "git add" (but no actual git commit), then yes, a git fsck --cache --no-reflogs --lost-found --unreachable HEAD is needed to start checking out content of deleted staged files.

The OP Tim Rowley does mention in the comments:

About half of the /storage/customer/xxxx folders are actually in the GitHub repo online from a previous commit.
The other half has never been committed or pushed as they have only been on the server. Thus, I must have accidentally made a push from my server with the first half - and the second half was new. I need the second half back.
The second half never interacted with Git, s

In that case, only file recovery tools might help restoring those files.

Related