I accidentally did a
git rm filename.txt -f
I removed all my files thinking it would only remove them from the commit, now need to recover them. Is there a way or are they lost for good?
I accidentally did a
git rm filename.txt -f
I removed all my files thinking it would only remove them from the commit, now need to recover them. Is there a way or are they lost for good?
The title of your question seems to indicate no commit was done on your repo.
In that case :
filename.txt
weren't staged (e.g: weren't present in the last git add filename.txt
that your ran), git
doesn't have a copy of that file, you would have to look at other tools to get that file back from disk (several IDEs keep a local history of your files, or you can run disk recovery tools)git add filename.txt
), however, is still present somewhere in git
's databaseSee for example this answer for one possible way to get back some data from git's storage :
#!/bin/sh
git fsck --no-reflogs --unreachable |\
grep blob |\
cut -d' ' -f3 |\
sed -e 's|^\(..\)\(.*\)|.git/objects/\1/\2|' |\
xargs ls -l -t 2> /dev/null
This should give you a list of hashes, and one of these hashes should be the content of your file. The list will be sorted in "most recent first" oerder, so with some luck your last file will be listed among the first ones.
You can run git show {hash}
to view the content of the file behind a hash, and git show {hash} > back-from-the-dead.txt
to restore it to a file on disk.
Try
git reset head ~
If you staged your commit you can recover it, otherwise it's probably dead forever.