简体   繁体   English

恢复从未提交的已删除目录

[英]Restore deleted directory that was never committed

I accidentally added a local directory to my local Git repo. 我不小心将本地目录添加到本地Git存储库中。

git add dirName

It added the twenty or so files in that directory. 它在该目录中添加了大约二十个文件。 I did not want them added. 我不想添加他们。 They had never been added before, and I did not commit them. 以前从未添加过它们,我也没有提交它们。 Instead, I removed them with: 相反,我使用以下命令删除了它们:

git rm -rf dirName

This successfully removed them from the repo, but to my dismay that directory also appears to have been deleted from disk. 这成功地将它们从存储库中删除,但是令我沮丧的是,该目录似乎也已从磁盘中删除。 They are not in my recycle bin. 它们不在我的回收站中。

Is there any way to get those files back? 有什么办法找回那些文件吗? I have a backup, of course, but I had made quite a few changes to the files since the last backup. 当然,我有一个备份,但是自上次备份以来,我对文件进行了很多更改。

I don't think the accepted answer is entirely accurate. 我认为接受的答案并不完全正确。 Indeed git rm removes the files from the index, but they usually remain in Git's object database for a while. 确实git rm从索引中删除了文件,但是它们通常在Git的对象数据库中保留一段时间。 Unless you force garbage collection, that is. 除非您强制执行垃圾收集,否则就是这样。

Have you tried running git fsck to retrieve some dangling blobs or dangling trees ? 您是否尝试过运行git fsck来检索一些悬垂的斑点悬垂的树木

$ git init test
Initialized empty Git repository in D:/DOCUME~1/T012345/LOCALS~1/Temp/test/.git/

$ echo baz > foo/bar.txt

$ git add -Av
add 'foo/bar.txt'

$ git rm -rf *
rm 'foo/bar.txt'

$ git fsck
notice: HEAD points to an unborn branch (master)
Checking object directories: 100% (256/256), done.
notice: No default references
dangling blob 76018072e09c5d31c8c6e3113b8aa0fe625195ca

$ git cat-file -p 76018072e09c5d31c8c6e3113b8aa0fe625195ca
baz

no there isn't because 不,不是因为

git rm -rf dirName

is like 就好像

rm -rf dirName

which is also irreversible but it also removes the staging files so there is no way you can get the files back. 这也是不可逆的,但它也会删除暂存文件,因此您无法取回文件。 Next time use git rm --cached dirName which will only remove it from stage and not from your disc. 下次使用git rm --cached dirName只会将其从舞台中删除,而不是从光盘中删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM