简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM