简体   繁体   English

如何将未提交的已删除文件(用纯rm删除)还原到最后的git commit

[英]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. 我错误地在git仓库中rm -rf一个目录。 The changes are not commited and I wanted to revert this change and go back to my last git commit. 更改未提交,我想还原此更改并返回到我的上一个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. 由于文件被删除,所以我可以执行git checkout -- <file>所以我也可以执行git checkout --但这是行不通的。

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? 是否可以checkout当前分支(整个分支而不给出任何文件)而放弃任何更改?

In the short term, you can use git stash drop to get the superfluous entry out of your stash. 在短期内,您可以使用git stash dropgit stash drop多余的条目。 In the future, you can use git checkout HEAD -- dir to get the head commit version of dir. 将来,您可以使用git checkout HEAD -- dir获取git checkout HEAD -- dir的head commit版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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