ChatGPT解决这个技术问题 Extra ChatGPT

Git - undoing git rm [duplicate]

This question already has answers here: Closed 9 years ago.

Possible Duplicate: How to revert a “git rm -r .”?

Git SOS here. I worked 10 hours on a project without committing (I know, I know) and then I git added too many files, so I tried using git rm and accidentally deleted EVERYTHING. Is there hope for me? :(((


H
Hauleth

If you already commited changes, then:

git reset (--hard) HEAD~1

If not then:

git reset
git ls-files -d -z | xargs -0 git checkout --

Would git reset --hard HEAD work in the second case?
Will, but it will also discard all your changes.
You're a freaking lifesaver!
To restore deleted files the last part should look like git ls-files -d and thet it will recover only deleted files.
git ls-files -d -z | xargs -0 git checkout -- is the way to go for the generic case with filename spaces (jennyandlih.com/using-git-ls-files-input-xargs)