12

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 
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Pradeep
  • 753
  • 7
  • 15
  • 25
  • 1
    If you've already deleted the folder, git should detect that already, you just need to stage the deletion. What's the output of `git status`? –  Apr 28 '14 at 05:01
  • possible duplicate of [Restore a deleted file in a Git repo](http://stackoverflow.com/questions/953481/restore-a-deleted-file-in-a-git-repo) – indivisible Apr 28 '14 at 05:02
  • Possible duplicate of [How do you discard unstaged changes in git?](http://stackoverflow.com/questions/52704/how-do-you-discard-unstaged-changes-in-git) –  Apr 28 '14 at 05:03
  • @mbs that's not a good duplicate, that question involves committing the deletion. The original poster to this question doesn't appear to have staged and committed the change yet. –  Apr 28 '14 at 05:04
  • I have not yet committed the changes. The output of the 'git status' command is updated in the question. – Pradeep Apr 28 '14 at 05:11
  • Yeah, see, git detects that you've deleted the file already, so you just need to stage the change, you don't need to revert and then delete it again. Just do `git rm -r i18n`, or `git add -A i18n`, or `git add --all i18n`. –  Apr 28 '14 at 05:19

5 Answers5

13

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

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Why 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. – John Jiang Jul 26 '21 at 23:39
  • @JohnJiang The actual command these days would be `git restore -- .` (see https://stackoverflow.com/a/57066072/6309): the goal is to restore HEAD, which might have changed since the last fetch of `origin/master` – VonC Jul 27 '21 at 06:15
8

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
Damien
  • 1,140
  • 15
  • 22
5

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
Sma Ma
  • 3,343
  • 2
  • 31
  • 39
0

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.

mockinterface
  • 14,452
  • 5
  • 28
  • 49
-1

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.

audiracmichelle
  • 124
  • 1
  • 4