Pradeep
Pradeep

Reputation: 803

git undo deleted files

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 unix rm -rf folder command. I need to revert the delete command and then perform git 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 

Upvotes: 17

Views: 36423

Answers (5)

mockinterface
mockinterface

Reputation: 14860

To supplement the answer by @VonC,

I had to delete few files from the git repo. Instead of doing git rm command, I issued unix rm -rf folder command

The only difference between the two options is that git rm can also remove the files from your git index. However, because you never added them to the index to begin with, there is no particular reason to use git rm, and you don't need to undo the plain /bin/rm.

Upvotes: 0

audiracmichelle
audiracmichelle

Reputation: 124

I deleted a folder without committing the change. I was able to recover the folder by simply using git recover

git recover name_of_deleted_folder

I had to spell the full name, that is partial_name_of_deleted_folder* did not work.

Upvotes: -1

Sma Ma
Sma Ma

Reputation: 3685

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

Upvotes: 8

Damien
Damien

Reputation: 1528

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

Upvotes: 17

VonC
VonC

Reputation: 1323095

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?"

Upvotes: 15

Related Questions