Git恢复删除不应删除的文件

问题描述

我有三个提交,我尝试恢复到第一个提交。在这样做时,Git 删除了两个文件,我不知道为什么它们在我尝试恢复的第一次提交中。如果 Git 删除的两个文件在原始提交中,为什么要删除它们?

这是我使用的代码

git revert <commit id>

这是错误信息:

    Removing style.css
CONFLICT (modify/delete): pages/services.html deleted in (empty tree) and modified in HEAD. Version HEAD of pages/services.html left in tree.
Removing index.html
error: Could not revert 9b23173... inital commit
hint: after resolving the conflicts,mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'
hint: and commit the result with 'git commit'

解决方法

如果您想将项目的内容恢复到某个修订版同时保留以后的历史,可以这样做:

git checkout the-revision-id
git reset --soft the-branch-I-want-to-take-back
# at this point,all changes from the branch you want to take back and the revision you want to go to are in index,ready to be committed
git commit -m "taking everything back in time"
# if you like the result,move the pointer of the branch
git branch -f the-branch-I-want-to-take-back
git checkout the-branch-I-want-to-take-back