0

I cannot recover my accidently deleted files after running git rm --cached . I have tried to git reset hard and several other ways but nothing helps

[ec2-user@ip-10-0-0-190 cgsignlab]$ git rm --cached .
fatal: not removing '.' recursively without -r
You have new mail in /var/spool/mail/ec2-user
[ec2-user@ip-10-0-0-190 cgsignlab]$ git rm --cached . -r
error: 'vendor/apix/log' has staged content different from both the file     and the HEAD
(use -f to force removal)
error: 'vendor/aws/aws-sdk-php' has staged content different from both the     file and the HEAD
(use -f to force removal)
error: 'vendor/guzzlehttp/guzzle' has staged content different from both the file and the HEAD
(use -f to force removal)
error: 'vendor/guzzlehttp/promises' has staged content different from both     the file and the HEAD
(use -f to force removal)
error: 'vendor/guzzlehttp/psr7' has staged content different from both the file and the HEAD
(use -f to force removal) 
error: 'vendor/nategood/httpful' has staged content different from both the    file and the HEAD
(use -f to force removal)
error: 'vendor/nicolab/php-ftp-client' has staged content different from both the file and the HEAD
(use -f to force removal)
error: 'vendor/psr/http-message' has staged content different from both      the file and the HEAD 
(use -f to force removal)
error: 'vendor/psr/log' has staged content different from both the file and the HEAD
(use -f to force removal)
[ec2-user@ip-10-0-0-190 cgsignlab]$ git reset HEAD .
fatal: ambiguous argument 'HEAD': unknown revision or path not in the  working   tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'


[ec2-user@ip-10-0-0-190 cgsignlab]$ git status
# HEAD detached at 04e2948
nothing to commit, working directory clean
You have mail in /var/spool/mail/ec2-user
[ec2-user@ip-10-0-0-190 cgsignlab]$ 

these are the files I need to restore any help greatly appreciated

# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#   new file:   art.php
#   new file:   ceil.php
#   new file:   composer.json
#   new file:   composer.lock
#   new file:   metaupdate.php
#   new file:   movetoreview.php
#   new file:   orders.php
#   new file:   shipments.php
#   new file:   shipments/tracking.csv
#   new file:   src/CG/Aws/S3/Client.php
#   new file:   src/CG/OrderDesk/RestApi.php
#   new file:   src/CG/Orders/Order.php
#   new file:   src/CG/Orders/OrderDetails.php
#   new file:   src/CG/Orders/RestApi.php
#   new file:   src/CG/Orders/ShipMethods.php
#   new file:   src/CG/PitchPrint/RestApi.php
#   new file:   src/CG/Shipments/TrackingAdapter.php
#   new file:   src/CG/SignLab/ImageFormatter.php
#   new file:   src/CG/SignLab/Products.php
#   new file:   src/CG/SignLab/Utils.php

need more commentary so this does not think it's all code but need to post the relevant logs

[ec2-user@ip-10-0-0-190 cgsignlab]$ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#   new file:   art.php
#   new file:   ceil.php
#   new file:   composer.json
#   new file:   composer.lock
#   new file:   metaupdate.php
#   new file:   movetoreview.php
#   new file:   orders.php
#   new file:   shipments.php
#   new file:   shipments/tracking.csv
#   new file:   src/CG/Aws/S3/Client.php
#   new file:   src/CG/OrderDesk/RestApi.php
#   new file:   src/CG/Orders/Order.php
#   new file:   src/CG/Orders/OrderDetails.php
#   new file:   src/CG/Orders/RestApi.php
#   new file:   src/CG/Orders/ShipMethods.php
#   new file:   src/CG/PitchPrint/RestApi.php
#   new file:   src/CG/Shipments/TrackingAdapter.php
#   new file:   src/CG/SignLab/ImageFormatter.php
#   new file:   src/CG/SignLab/Products.php
#   new file:   src/CG/SignLab/Utils.php
#   new file:   vendor/apix/log
#   new file:   vendor/autoload.php
#   new file:   vendor/automattic/woocommerce/.editorconfig
#   new file:   vendor/automattic/woocommerce/.gitignore

#   new file:   vendor/mtdowling/jmespath.php/tests/compliance/wildcard.json
#   new file:   vendor/nategood/httpful
#   new file:   vendor/nicolab/php-ftp-client
#   new file:   vendor/psr/http-message
#   new file:   vendor/psr/log
#
[ec2-user@ip-10-0-0-190 cgsignlab]$ git reset HEAD 
user1930591
  • 307
  • 5
  • 22

1 Answers1

2

git fsck could be helpful here:

git fsck --unreachable

This should output list of hashes (potentially much more than the list that you've deleted). Then use those hashes to obtain their content with:

git cat-file -p <hash>

Unfortunately you wouldn't get file name, only its content.

Marcin Pietraszek
  • 3,134
  • 1
  • 19
  • 31
  • I think this should help – user1930591 Sep 12 '18 at 22:11
  • is there a easy way to just restore all of them? what about folders? – user1930591 Sep 12 '18 at 22:13
  • I tried using git fsck --lost-found but it put them all in a directory with a bunch of hash names does no good there where several folders that where deleed and trying to figure out where they all go seems like a nightmare will beeasier just cloning from another similar project and updating what I need sucks this happened and there was no easy way to recover I still don't know why it did deleted them and did not unstage them like it said it would do very frustrating – user1930591 Sep 12 '18 at 22:38
  • so far this is the most helpful answer but it still does not solve my problem – user1930591 Sep 12 '18 at 22:42
  • @marcin You just saved me 3 days of rewriting (accidentally) deleted code. Thank you so much!! – roronoa Aug 13 '20 at 13:46
  • @roronoa glad to hear that it helped :) – Marcin Pietraszek Aug 20 '20 at 06:32