简体   繁体   English

删除.git文件夹后恢复git中的删除文件

[英]Recover delete files in git after removing .git folder

I am trying to push to a repository from an existing directory so I added all the files using 我试图从现有目录推送到存储库,所以我使用添加了所有文件

git add -A

but then i realized that i should not add the node modules folder, so i did a 但是后来我意识到我不应该添加节点模块文件夹,所以我做了一个

git reset --hard

then I decided to add folders one by one and halfway through, i realized that i should have made the .gitignore file first so i decided to remove all the git relationships in my folder using 然后我决定一遍又一遍地添加文件夹,我意识到我应该先制作.gitignore文件,所以我决定使用以下方法删除文件夹中的所有git关系

rm -rf .git

after i re-initialized the current folder using 在我使用以下命令重新初始化当前文件夹后

git init

too many files are missing from the current directory. 当前目录中缺少太多文件。 I don't have any backup of the project. 我没有该项目的任何备份。 Any idea on how i can recover the files? 关于如何恢复文件的任何想法吗?

If you have no backups, and either your OS doesn't do "soft deletes" (trash can, recycle bin, whatever) or you deleted in a way that bypasses them, then there is no reliable way to recover the work. 如果您没有备份,并且操作系统没有执行“软删除”(垃圾桶,回收站等),或者以绕过备份的方式删除了这些备份,则没有可靠的方法来恢复工作。

The .git directory contains everything git knows about your files, other than untracked files or changes in the working tree. .git目录包含git知道的有关文件的所有信息,而不是未跟踪的文件或工作树中的更改。 Removing .git/ is almost never necessary and very dangerous, as this question unfortunately illustrates. 不幸的是,删除.git/几乎是没有必要的,而且非常危险。 Other commands you used are also more aggressive/dangerous than what you needed, and the odds are you lost the working files pretty early in the process. 您使用的其他命令也比您需要的命令更具攻击性/危险性,而且您很可能在此过程的早期丢失了工作文件。 Sorry the news isn't better. 对不起,这个消息并没有更好。

For future reference: 备查:

It is likely that the reset --hard is what deleted your working files. reset --hard可能是删除您的工作文件的原因。 You had done some adds and wanted to revert them, so a mixed reset (which is the default) would have been sufficient and would have left your working tree alone. 您已经做了一些添加,并且想要还原它们,所以mixed重置(这是默认设置)就足够了,并且会独自留下工作树。 A hard reset restores your working tree to the committed state (along with the index). 硬重置将您的工作树恢复到提交状态(以及索引)。

After the reset you started doing add s again. 重置后,您再次开始执行add I guess when you had to start over a second time, you were worried that you might have git accumulating unwanted state. 我猜想当您不得不重新开始时,您担心git可能会积累不需要的状态。 It wasn't. 不是。 Another mixed reset would have been fine. 另一个mixed重置本来可以。 You can always use git status to make sure the index doesn't contain anything unexpected (and clear it out if so). 您可以始终使用git status来确保索引中不包含任何意外内容(如果有的话,请清除该内容)。

The only enduring record is what's in the database, and since you hadn't committed anything you weren't affecting the database yet. 唯一持久的记录是数据库中的内容,并且由于您尚未提交任何内容,因此您尚未影响数据库。 Even if you had committed things you wanted to discard, as long as those things hadn't been shared with others there would be procedures for getting rid of them. 即使您犯了一些您想丢弃的东西,只要这些东西没有与其他人共享,就会有摆脱它们的程序。

Generally git is very good at safeguarding your data, but it can't protect what you haven't committed; 通常,git非常擅长保护您的数据,但无法保护您尚未提交的内容; so asking it to use commands that are destructive toward the work tree, or nuking the repository and metadata (by deleting .git/ ) should only be done if you're very sure of why you're doing them. 因此,仅当您非常确定为什么要使用它们时,才要求它使用对工作树具有破坏性的命令或对存储库和元数据进行破坏(通过删除.git/ )。

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

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