Pablo Marin-Garcia
Pablo Marin-Garcia

Reputation: 4251

how to revert uncommited deleted files (deleted with plain rm) to the last git commit

By mistake I rm -rf a directory in my git repository. The changes are not commited and I wanted to revert this change and go back to my last git commit.

# On branch release-1
# Changes not staged for commit:
#   (use "git add/rm <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       deleted:    dir/file1
#       [....]

As the files were deleted I was nit able to do git checkout -- <file> so I did git checkout -- instead, but this did not work.

Therefore I took a shortcut: stashed the changes

$  git stash
Saved working directory and index state WIP on release-1: d2dbff3 removed the CVS $Id lines
Checking out files: 100% (394/394), done.
HEAD is now at d2dbff3 removed the CVS $Id lines

And now is all OK.

I have the impression that stashing is a bit of brute force approach. Is it possible to do a checkout of the current branch (the whole one without giving any file) discarding any change?

Upvotes: 1

Views: 182

Answers (1)

Adam Mihalcin
Adam Mihalcin

Reputation: 14458

In the short term, you can use git stash drop to get the superfluous entry out of your stash. In the future, you can use git checkout HEAD -- dir to get the head commit version of dir.

Upvotes: 3

Related Questions