我不小心说了git rm -r .
。我该如何从中恢复?
我没有答应。
我认为所有文件都被标记为删除,并且也从我的本地结帐中物理删除。
编辑: 我可以(如果我知道命令)恢复到最后一次提交。但如果我可以撤消 git rm -r .
,那就更好了。因为我不确定在最后一次提交之后和 git rm -r .
之前我做了什么。
git rm
提供 -f
,git 不会删除任何已暂存或未暂存更改的文件,因此 git reset; git checkout .
应该恢复所有内容。
git reset HEAD
应该这样做。如果您没有任何您关心的未提交更改,那么
git reset --hard HEAD
应该强制将所有内容重置为您的最后一次提交。如果您确实有未提交的更改,但第一个命令不起作用,则使用 git stash
保存未提交的更改:
git stash
git reset --hard HEAD
git stash pop
当我意识到我需要返回其中的一些文件时,我 git-rm'd 了一些文件并在下一次提交之前继续进行更改。如果需要,您可以简单地检查您错过/删除的单个文件,而不是存储和重置:
git checkout HEAD path/to/file path/to/another_file
这使您的其他未提交的更改完好无损,没有变通办法。
git rm
而不是整个递归 git rm -r
。对于完全递归删除,其他解决方案可能会更好,具体取决于删除的文件数量。
要重新获得一些单个文件或文件夹,可以使用以下
git reset -- path/to/file
git checkout -- path/to/file
这将首先重新创建 path/to/file
的索引条目并重新创建文件,就像它在上次提交中一样,即 HEAD
。
提示: 可以将提交哈希传递给两个命令,以从较旧的提交重新创建文件。有关详细信息,请参阅 git reset --help
和 git checkout --help
。
git rm
操作进行简单的外科手术“撤消”,而不会清除其他未提交的更改。
更新:
由于 git rm .
删除了工作结帐以及索引中的此目录和子目录中的所有文件,因此您需要撤消这些更改中的每一个:
git reset HEAD . # This undoes the index changes
git checkout . # This checks out files in this and child directories from the HEAD
这应该做你想要的。它不会影响您签出代码或索引的父文件夹。
旧答案不是:
reset HEAD
可以解决问题,并且不会删除您对文件所做的任何未提交的更改。
之后,您需要重复您已排队的任何 git add
命令。
git alias.co="checkout"
以便 git co
进行结帐。
如果您最终没有上述方法,您可以使用此处的建议检索数据:http://www.spinics.net/lists/git/msg62499.html
git prune -n
git cat-file -p <blob #>
git prune -n
后,我得到了很多行,那些行是tree
或blob
或commit
,我填写了所有blob
的哈希字符串,我试试使用第二个命令 git cat-file -p <blob #>
,我仍然找不到丢失的文件。我的情况很简单,我创建了一个名为 a.cpp
的新文件,并将这个文件添加到索引中,后来我使用 git rm -f
删除了这个文件,我看到 a.cpp
丢失了。
git rm -f
删除的文件,我不知道为什么之前的测试不起作用,谢谢!
撤消 git rm
git rm file # delete file & update index
git checkout HEAD file # restore file & index from HEAD
撤消 git rm -r
git rm -r dir # delete tracked files in dir & update index
git checkout HEAD dir # restore file & index from HEAD
撤消 git rm -rf
git rm -r dir # delete tracked files & delete uncommitted changes
not possible # `uncommitted changes` can not be restored.
Uncommitted changes
包括 not staged changes
、staged changes but not committed
。
git rm -rf
,因为这也可能会删除未跟踪或暂存的(仅)文件。
git rm -rf file
和 git rm -rf dir
不会删除任何未跟踪的文件。
如果您已提交并推送更改,则可以执行此操作以取回文件
// Replace 2 with the # of commits back before the file was deleted.
git checkout HEAD~2 path/to/file
已经有一些很好的答案,但我可能会建议一种很少使用的语法,它不仅效果很好,而且在你想要的方面非常明确(因此并不可怕或神秘)
git checkout <branch>@{"20 minutes ago"} <filename>
获取列表提交
git log --oneline
例如,稳定提交具有哈希:45ff319c360cd7bd5442c0fbbe14202d20ccdf81
git reset --hard 45ff319c360cd7bd5442c0fbbe14202d20ccdf81
git push -ff origin master
我也有同样的情况。就我而言,解决方案是:
git checkout -- .
对于 Git 2.23+(2019 年 8 月),恢复文件(和索引)的正确命令是使用... git restore
(不是 reset --hard
或 confusing git checkout
command)
那是:
git restore -s=HEAD --staged --worktree -- .
或其缩写形式:
git restore -s@ -SW -- .
如果您对未暂存(因此也未提交)更改的存储库执行命令 git rm -r --cached .
,则可以使用命令 git restore --staged .
撤消操作(取消暂存删除)
因此,简而言之,要撤消 git rm -r --cached .
,您只需运行 git restore --staged .
我遇到了完全相同的问题:清理我的文件夹、重新排列和移动文件。我输入: git rm 。并按回车;然后感觉我的肠子松了一点。幸运的是,我没有直接输入 git commit -m "" 。
但是,以下命令
git checkout .
恢复了一切,救了我的命。
不定期副业成功案例分享
git reset --hard HEAD
会破坏您在当前工作目录的父目录中所做的任何有用更改。