git revert
命令只接受一个提交,所以该错误是在抱怨您给了它一个修订版本列表而它期望得到一个。 (更新:感谢Jefromi指出,自1.7.2版本以来,git revert
实际上可以接受多个提交。) 下面我将建议三种可能的操作:
创建多个还原提交
我不确定是否可以在一条命令中完成这个动作,但如果您知道db0bc
是最后一个良好的提交(即您想要还原它之后的每个提交),并且历史记录是线性的,您可以执行以下操作:
for s in $(git rev-list --reverse db0bc..)
do
git revert --no-edit $s
done
回到旧状态,但仍在一个分支上
如果你只关心历史记录的存在,那么你可以让下一次提交成为源代码树返回到上一个好的提交db0bc
的状态,而不引入还原提交 - 你仍然拥有介于此之间和您的新提交之间引入的历史记录,但是没有大量的还原提交。此外,如果历史记录是非线性的,则不会出现问题:
# Check that "git status" is clean:
git status
# Set the index (staging area) to be as it was at commit db0bc:
git read-tree db0bc
# Create a commit based on that index:
git commit -m "Going back to the state at commit db0bc"
# Your working tree will still be as it was when you started, so
# you'll want to reset that to the new commit:
git reset --hard
为旧提交创建一个新的分支
如果您是该项目中唯一的贡献者,或者您还没有推送主分支,或者您知道重置主分支不会引起问题,那么您可以采取另一种方法,在当前位置创建一个新分支,然后将主分支重置,具体操作方法在github的“撤消”页面上介绍得非常好。简单地说,您可以执行以下操作:
# Make sure you're on master:
git checkout master
# Check that "git status" is clean, since later you'll be using "git reset --hard":
git status
# Create a new branch based on your current HEAD:
git branch unwanted-work
# Reset master back to the last good commit:
git reset --hard db0bc
我认为那个 GitHub 页面上的图表非常清晰易懂。