git add + git reset硬删除工作副本文件-撤消? [英] git add + git reset hard deleted working copy file - undo?
问题描述
我用git add添加了一个目录,并意识到其中包含错误的文件,所以我使用git reset-很难返回到最新的提交,但是我不知道git也会删除那些添加的文件在工作副本中.
I have added a directory with git add and realized that there are wrong files in it, so I used git reset --hard to go back to the latest commit but I didn't know that git would also delete those added files in the working copy.
有人知道我该怎么做吗?
Does anyone know how I undo this?
推荐答案
git add
文件时,该文件将添加到对象数据库和索引中.运行git reset
更新了索引和工作目录,但没有从对象数据库中删除那些文件.它们保留为无根(悬挂")对象.您可以运行git文件系统检查器git fsck
来查看哪些项目悬空了".您应该会看到如下报告:
When you git add
a file, it is added to the object database and to the index. Running git reset
updated the index and the working directory, but it did not remove those files from the object database. They remain as unrooted ("dangling") objects. You can run the git filesystem checker, git fsck
to see what items are "dangling". You should see a report like:
% git fsck
dangling blob 1ff0c423042b46cb1d617b81efb715defbe8054d
dangling blob 1bc915c5cb7185a9438de28a7b1a7dfe8c01ee7f
dangling blob a8c86221b400b836010567cc3593db6e96c1a83a
dangling blob 46ff0854663aeb2182b9838c8da68e33ac23bc1e
dangling blob 21a96a98ed84d45866e1de6e266fd3a61a4ae9dc
... etc ...
如果您还碰巧有一棵悬垂的树,则可以将文件名还原为Blob ID映射.如果您有一些悬空的树:
If you also happen to have a dangling tree, you may be able to restore the filename to blob ID mappings. If you have some dangling tree:
dangling tree 57623d711c18d819831f24a9456f0660e9dbe596
然后您可以通过运行git ls-tree
命令来显示树内容:
Then you can display the tree contents by running the git ls-tree
command:
% git ls-tree 57623d711c18d819831f24a9456f0660e9dbe596
100644 blob 21a96a98ed84d45866e1de6e266fd3a61a4ae9dc file1.txt
100644 blob 46ff0854663aeb2182b9838c8da68e33ac23bc1e file2.txt
这表示Blob 46ff0854663aeb2182b9838c8da68e33ac23bc1e
最初是作为file1.txt
添加的.但是,很可能您没有将树添加到对象数据库中,因此您需要检查每个文件以确定要保存在何处.
This indicates that blob 46ff0854663aeb2182b9838c8da68e33ac23bc1e
was added originally as file1.txt
. However, it is most likely that you did not get trees added to the object database and you will need to examine each file to determine where you wish to save it.
您可以使用git cat-file
命令获取Blob的内容.例如,将其保存到recovered.bak
:
You can get the blob's contents using the git cat-file
command. For example, to save it to recovered.bak
:
% git cat-file blob 46ff0854663aeb2182b9838c8da68e33ac23bc1e > recovered.bak
这篇关于git add + git reset硬删除工作副本文件-撤消?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!