簡體   English   中英

Git 在任何提交之前恢復已刪除的文件

[英]Git recover deleted files before any commit

我剛剛開始了一個已經有一些文件的新存儲庫,然后我做了git add . 但在那之后我做了git rm -r -f ./並刪除了所有文件。 有沒有辦法讓他們回來? 在此之前我沒有做任何提交。

您可能能夠使用git fsckgit show檢索已刪除文件的內容。 請參閱以下示例 - 我們創建一個新存儲庫,創建一個新文件,將其添加到索引中,然后運行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)

我們的FILE現在丟失了:

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

讓我們嘗試使用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

無法撤消“rm -r -f”命令。 如果您不提交更改,切勿使用 rm -f 命令。 沒有 -f 選項 git rm 檢查未提交的文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM