【问题标题】:cannot recover files after deleted running git rm --cached删除运行 git rm --cached 后无法恢复文件
【发布时间】:2018-09-12 20:54:09
【问题描述】:

运行 git rm --cached 后,我无法恢复意外删除的文件。我尝试过 git reset 和其他几种方法,但没有任何帮助

[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]$ 

这些是我需要恢复的文件,非常感谢任何帮助

# 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

需要更多评论所以这不认为这是所有代码但需要发布相关日志

[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 

【问题讨论】:

  • 被删除的文件是否曾经提交给 git?
  • 它们在哪里被删除,然后在尝试修复时我通勤在新的 git innit 主目录上只有一次提交
  • 试试git checkout .。该命令应该将文件“恢复”到 git 中的任何内容。 如果您有未暂存的更改,请不要这样做
  • git 结帐。不恢复已删除的文件,我不敢相信没有警告说 git 将删除所有这些文件
  • 如果文件从未被 git 跟踪过,那么就无法使用 git 来恢复它们。您需要找到其他工具来执行此操作。

标签: git ubuntu


【解决方案1】:

git fsck 在这里可能会有所帮助:

git fsck --unreachable

这应该输出哈希列表(可能比您删除的列表多得多)。然后使用这些哈希来获取它们的内容:

git cat-file -p <hash>

很遗憾,您不会得到文件名,只有它的内容。

【讨论】:

  • 我认为这应该会有所帮助
  • 有没有一种简单的方法来恢复所有这些?文件夹呢?
  • 我尝试使用 git fsck --lost-found 但它把它们都放在一个带有一堆哈希名称的目录中并没有好处在那里有几个文件夹在哪里被删除并试图找出它们都在哪里go 似乎是一场噩梦,只是从另一个类似的项目中克隆并更新我需要的东西很糟糕,这发生了,没有简单的方法来恢复我仍然不知道为什么它确实删除了它们并且没有像它所说的那样取消暂存它们做的很沮丧
  • 到目前为止这是最有帮助的答案,但它仍然不能解决我的问题
  • @marcin 你刚刚为我节省了 3 天的重写(意外)删除代码的时间。非常感谢!!
猜你喜欢
  • 2014-05-02
  • 2017-12-16
  • 2017-05-29
  • 1970-01-01
  • 2017-03-06
  • 2023-03-16
  • 2012-08-24
  • 1970-01-01
  • 2015-05-16
相关资源
最近更新 更多