git undo deleted files
Asked Answered
V

5

14

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 
Vagrom answered 28/4, 2014 at 5:0 Comment(6)
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?Zymometer
possible duplicate of Restore a deleted file in a Git repoRef
Possible duplicate of How do you discard unstaged changes in git?Zymometer
@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.Zymometer
I have not yet committed the changes. The output of the 'git status' command is updated in the question.Vagrom
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.Zymometer
F
15

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

Fillin answered 28/4, 2014 at 5:35 Comment(2)
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.Vulvitis
@JohnJiang The actual command these days would be git restore -- . (see https://mcmap.net/q/12502/-how-to-reset-all-files-from-working-directory-but-not-from-staging-area): the goal is to restore HEAD, which might have changed since the last fetch of origin/masterFillin
B
14

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
Bubonocele answered 15/11, 2017 at 2:8 Comment(0)
L
8

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
Lemmon answered 2/5, 2019 at 7:57 Comment(0)
C
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.

Crammer answered 29/4, 2014 at 9:58 Comment(0)
L
-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.

Luttrell answered 19/2, 2022 at 11:31 Comment(1)
not a valid command as of git version 2.39.0 (very recent as of today)Koski

© 2022 - 2024 — McMap. All rights reserved.