简体   繁体   English

Git 在任何提交之前恢复已删除的文件

[英]Git recover deleted files before any commit

I've just started a new repository that had some files already, then I did git add .我刚刚开始了一个已经有一些文件的新存储库,然后我做了git add . but after that i did git rm -r -f ./ and that deleted all the files.但在那之后我做了git rm -r -f ./并删除了所有文件。 Is there any way to get them back?有没有办法让他们回来? I didn't do any commit before that.在此之前我没有做任何提交。

You may to be able to retrieve contents of the deleted files with git fsck and git show .您可能能够使用git fsckgit show检索已删除文件的内容。 See the following example - we create a new repository, create a new file, add it to the index, then run git rm :请参阅以下示例 - 我们创建一个新存储库,创建一个新文件,将其添加到索引中,然后运行git rm

$ git init
Initialized empty Git repository in /home/ja/so/git-rm/.git/
$ echo hello > FILE
$ git add FILE
$ git status
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

        new file:   FILE

$ git rm -r -f ./
rm 'FILE'
$ git status
On branch master

Initial commit

nothing to commit (create/copy files and use "git add" to track)

Our FILE is now lost:我们的FILE现在丢失了:

$ ls -Al
total 4
drwxr-xr-x 7 ja users 4096 Mar 12 16:17 .git

Let's try to retrieve its contents with git fsck and git show :让我们尝试使用git fsckgit show检索其内容:

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

There is no way to undo "rm -r -f" command.无法撤消“rm -r -f”命令。 Never use rm -f command if you don't commits the changes.如果您不提交更改,切勿使用 rm -f 命令。 without -f option git rm check uncommitted files.没有 -f 选项 git rm 检查未提交的文件。

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

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