繁体   English   中英

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

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

我错误地在git仓库中rm -rf一个目录。 更改未提交,我想还原此更改并返回到我的上一个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
#       [....]

由于文件被删除,所以我可以执行git checkout -- <file>所以我也可以执行git checkout --但这是行不通的。

因此,我采取了一条捷径:隐藏变更

$  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

现在一切正常。

我的印象是隐藏是一种蛮力的方法。 是否可以checkout当前分支(整个分支而不给出任何文件)而放弃任何更改?

在短期内,您可以使用git stash dropgit stash drop多余的条目。 将来,您可以使用git checkout HEAD -- dir获取git checkout HEAD -- dir的head commit版本。

暂无
暂无

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

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