I've just started a new repository that had some files already, then I did git add .
but after that i did git rm -r -f ./
and that deleted all the files. Is there any way to get them back? I didn't do any commit before that.
Asked
Active
Viewed 390 times
-3

Arkadiusz Drabczyk
- 11,227
- 2
- 25
- 38

Davi Silva
- 73
- 5
-
https://stackoverflow.com/questions/47748090/how-to-recover-files-from-missing-tree-and-dangling-blobs – eftshift0 Mar 12 '20 at 15:06
-
Nope. No commit = no history. Sorry for your loss – LLSv2.0 Mar 12 '20 at 15:06
-
Ok then, thank you. – Davi Silva Mar 12 '20 at 15:07
-
Does this answer your question? [How to recover files from missing tree and dangling blobs?](https://stackoverflow.com/questions/47748090/how-to-recover-files-from-missing-tree-and-dangling-blobs) – MrTux Mar 12 '20 at 17:47
-
Does this answer your question? [All staged, but uncommitted, files deleted after issuing: git reset --hard HEAD](https://stackoverflow.com/questions/11621424/all-staged-but-uncommitted-files-deleted-after-issuing-git-reset-hard-head) – phd Mar 12 '20 at 17:47
-
https://stackoverflow.com/search?q=%5Bgit%5D+recover+uncommitted+files – phd Mar 12 '20 at 17:47
2 Answers
0
You may to be able to retrieve contents of the deleted files with git fsck
and git show
. See the following example - we create a new
repository, create a new file, add it to the index, then run 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:
$ 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 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

Arkadiusz Drabczyk
- 11,227
- 2
- 25
- 38
-
It's okay I didn't lost much and already redid the work really quick. (My keyboard is actually on fire right now). But thank you for trying to help. – Davi Silva Mar 12 '20 at 16:39
-2
There is no way to undo "rm -r -f" command. Never use rm -f command if you don't commits the changes. without -f option git rm check uncommitted files.

Anar Rzayev
- 350
- 2
- 9
-
1This is not correct. If the object had been added to index, it's already in git's object DB.... as dangling objects, at the very least. – eftshift0 Mar 12 '20 at 15:07
-
Is there any way to repair deleted files after git rm -r -f command? – Anar Rzayev Mar 12 '20 at 15:10
-
1Well.... sure: `git reset --hard` just to get back to the last revision :-). But I guess you mean to get the new files or modified files? You don't actually repair them. Once you identify the object that you care about, you can run `git cat-file -p object-id > some-file.txt`.... and hell, I hope we are talking about a handful of files and not dozens over dozens. – eftshift0 Mar 12 '20 at 15:12