Git - undoing git rm
78,449
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 --
Author by
user1436111
Updated on October 15, 2020Comments
-
user1436111 over 3 years
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? :(((
-
Tamás Szelei almost 11 yearsWould
git reset --hard HEAD
work in the second case? -
Hauleth almost 11 yearsWill, but it will also discard all your changes.
-
Tamás Szelei almost 11 yearsAh, ok. What exactly is the second line doing here? Is it strictly undoing the rm, or does it have a wider effect?
-
Hauleth almost 11 yearsIt remove all files in index and then checkout all modified files to HEAD.
-
Omar Wagih over 10 yearsYou're a freaking lifesaver!
-
Craig Brett over 9 yearsBe careful when using the second one, I tried using it to recover some accidentally rm'd files and lost my uncommitted changes. Noob mistake I guess.
-
Hauleth over 9 yearsTo restore deleted files the last part should look like
git ls-files -d
and thet it will recover only deleted files. -
Carles Jove i Buxeda about 9 yearsI lost all my uncommited changes after
git checkout -- $(git ls-files -m)
. I don't know if what other people are commenting here is right (usinggit ls-files -d
instead), but someone who knows should edit this answer -
Chris P almost 9 years
git checkout -- $(git ls-files -d)
This doesn't work if the filenames have spaces in them -
Chris P almost 9 years
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) -
timeyyy almost 8 yearsinstall trash and then
alias rm=trash
in.zshrc
or similar, now all deletes are undo able :) -
lucasfcosta almost 7 yearsYou just saved my whole day of work. I learned a lesson today.
-
kaiser over 3 yearsI have never ever before been so much in love as with your lines above.
-
Arthur over 2 yearsIs
xargs
only for linux? How can I do the same thing from the Windows command line?