git undo deleted files
Solution 1
I need to revert the delete command and then perform git rm command. How to revert to the latest code?
Simply do a (since you haven't committed anything):
cd /root/of/your/repo
git checkout HEAD -- .
That will restore the working tree to the index.
(A git reset --hard
should work too, but isn't needed here)
But you could also register those deletion to the index directly:
git add -A .
See "What's the difference between git add .
and git add -u
?"
Solution 2
If you have staged the deletion, first unstage it:
git reset HEAD path/to/deleted/file
Then restore the file:
git checkout path/to/deleted/file
Solution 3
Revert a git delete on your local machine
You're wanting to undo of git rm or rm followed by git add:
Restore the file in the index:
git reset -- <file>
after that check out from index
git checkout -- <file>
for example:
git reset -- src/main/java/de/foo/bar.java
git checkout -- src/main/java/de/foo/bar.java
Pradeep
Updated on February 20, 2022Comments
-
Pradeep about 2 years
I am new to git. I have checkout files from remote. I had to delete few files from the git repo. Instead of doing
git rm
command, I issued unixrm -rf folder
command. I need to revert the delete command and then performgit rm
command. How to revert to the latest code?Note: I have not yet committed the staged files.The out out of
git status
is the list of files deleted in the below format:# deleted: i18n/angular-locale_sl.js # deleted: i18n/angular-locale_in.js
-
John Jiang almost 3 yearsWhy doesn't "git checkout origin/master" do the same thing? At least the latter can remind me to stash the change before checking out or something.
-
VonC almost 3 years@JohnJiang The actual command these days would be
git restore -- .
(see stackoverflow.com/a/57066072/6309): the goal is to restore HEAD, which might have changed since the last fetch oforigin/master