这个问题已经得到了正确的回答,但我只想解决 op 问题的第一点——我不知道如何完成这个 reverting。如果您也像我一样陷入恢复状态并来到这里寻找答案。
它可以很简单git revert --continue
。但前提是您要还原文件中的一些更改。如果您陷入恢复状态,那很可能是因为您试图恢复创建文件的提交,并且该文件在任何后续提交中都被修改。
举个例子:
1c40cd3 added fileB
4e06828 added fileA
faf29dd (origin/main, origin/HEAD, main) Merged in feature5 (pull request #13)
6e735e3 (origin/feature5, feature5) updated file.txt
fb51fca added file.txt
744ba74 Initial commit
现在,如果您尝试 revert fb51fca (added file.txt)
,它将向您显示以下内容:
$ git revert fb51fca
CONFLICT (modify/delete): file.txt deleted in parent of fb51fca (added file.txt) and modified in HEAD. Version HEAD of file.txt left in tree.
error: could not revert fb51fca... added file.txt
hint: After resolving the conflicts, mark them with
hint: "git add/rm <pathspec>", then run
hint: "git revert --continue".
hint: You can instead skip this commit with "git revert --skip".
hint: To abort and get back to the state before "git revert",
hint: run "git revert --abort".
$ git status
On branch feature6
You are currently reverting commit fb51fca.
(fix conflicts and run "git revert --continue")
(use "git revert --skip" to skip this patch)
(use "git revert --abort" to cancel the revert operation)
Unmerged paths:
(use "git restore --staged <file>..." to unstage)
(use "git add/rm <file>..." as appropriate to mark resolution)
deleted by them: file.txt
你可以清楚地看到file.txt deleted in fb51fca (added file.txt) and modified in HEAD. Version HEAD of file.txt left in tree.
。现在,你可以git add -A
做git commit ...
但是
revert 所做的是返回历史记录(在指定的提交中),删除在该提交中所做的更改。并做出新的承诺。但问题是,您创建了一个文件,现在 revert 正在删除它。最后,没有文件可以暂存,而且如您所知,git 不允许空目录或空提交。
但是有一个空提交的解决方法。正如瑞安建议的那样,你可以做到git commit -m "empty commit to escape revert" --allow-empty
。git revert 可能不使用此--allow-empty
标志,这可能是它无法执行提交卡在恢复状态的原因。
无论哪种方式,如果您陷入恢复状态,您可能做错了什么。试图还原一些没有意义的东西。