您可以使用交互式 rebase 恢复到之前的提交。假设您想将 HEAD 恢复为 HEAD~5(之前提交了 5 次),您可以选择 HEAD~5 的父级,即 HEAD~6,然后在其上应用 HEAD~5。
git rebase -i HEAD~6
HEAD~6 到 HEAD 之间的每次提交都会被重写。说 HEAD~5 SHA1 是 ABC & HEAD~6 SHA1 是 XYZ。当前的 HEAD 是 PQR。我们需要从下面的编辑中删除其他提交,因为我们不想在 HEAD~6 上重写它们。我们只想在 HEAD~6 之上应用 HEAD~5。我们应该在这个文件中至少有一个条目,否则 rebase 将中止。所以,我们在 HEAD~6 之上应用 HEAD~5。
edit ABC reverting to state before merge
# Rebase XYZ..PQR onto XYZ
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
现在,发出以下命令来修改历史记录并输入提交信息。
git commit --amend
现在,发出以下命令以继续并退出编辑器
git commit --continue
阅读更多rewriting git history