git 使用 revert 多次回滚到旧提交
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4716051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rollback to an old commit using revert multiple times
提问by David
I want to rollback to a commit using git revert so I can preserve the history. Is there an easier way to do this then to run git revert for every commit hash I want to revert? Is there a quicker way to revert from a specific commit all the way up to another commit hash?
我想使用 git revert 回滚到提交,以便我可以保留历史记录。有没有更简单的方法来做到这一点,然后为我想要恢复的每个提交哈希运行 git revert ?有没有更快的方法从一个特定的提交一直恢复到另一个提交哈希?
On the man page they state that you can do something like this git revert -n master~5..master~2
but when I try to do that I get this error: fatal: Cannot find 'master~5..master~2'
在手册页上,他们声明您可以执行以下操作,git revert -n master~5..master~2
但是当我尝试这样做时,出现此错误:fatal: Cannot find 'master~5..master~2'
回答by Mark Longair
git revert
should take a single commit, so that error is complaining that you're giving it a list of revisions where it's expecting one. (Update: thanks to Jefromifor pointing out that since 1.7.2, git revert
actually can take multiple commits.)I'll suggest three possible courses of action below:
git revert
应该进行一次提交,因此该错误会抱怨您正在向它提供期望的修订列表。 (更新:感谢Jefromi指出,从 1.7.2 开始,git revert
实际上可以进行多次提交。)我将建议以下三种可能的行动方案:
Creating multiple revert commits
创建多个还原提交
I'm not sure if you can do this in one command, but if you know that db0bc
is the last good commit (i.e. you want to revert every commit after that) and the history is linear, you could do:
我不确定您是否可以在一个命令中执行此操作,但是如果您知道这db0bc
是最后一次好的提交(即您想在此之后还原每次提交)并且历史记录是线性的,您可以这样做:
for s in $(git rev-list --reverse db0bc..)
do
git revert --no-edit $s
done
Going straight back to the old state, but still on one branch
直接回到旧状态,但仍然在一个分支上
On the other hand, if you just care about the history being there, you could always make the next commit be the state of the source tree back at the last good commit db0bc
without introducing revert commits - you'll still have the history that was introduced between then and your new commit, but without lots of revert commits in between. Also this won't have trouble if the history is non-linear:
另一方面,如果您只关心那里的历史记录,您总是可以使下一次提交成为上次良好提交时源树的状态,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
Create a new branch for the old commits
为旧提交创建一个新分支
If you're the only person working on the project, or you haven't pushed master yet, or you know that resetting your master branch won't cause problems, you could take an alternative tack, and create a new branch at your current position and then reset master back, as described nicely on github's "undoing" page. In short you could do:
如果你是该项目唯一的工作人员,或者你还没有推送 master,或者你知道重置你的 master 分支不会导致问题,你可以采取另一种方式,并在当前创建一个新分支位置,然后重置 master 回来,如在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
The diagrams on that github page make this nice and clear, I think.
我认为,github 页面上的图表使这一切变得清晰明了。
回答by Uwe Kleine-K?nig
IMHO the most natural approach is to do (assuming you have a clean working copy):
恕我直言,最自然的方法是(假设你有一个干净的工作副本):
git reset --hard $lastnicecommit;
git merge -s ours @{1}
git reset --hard $lastnicecommit;
git merge -s ours @{1}
and then git commit --amend
to add some description. (On newer versions of git git-merge
already lets you specify a description.)
然后git commit --amend
添加一些描述。(在较新版本的 git 上git-merge
已经允许您指定描述。)
(@{1}
is just referring to the commit your branch pointed to before the reset.)
(@{1}
只是指您的分支在重置之前指向的提交。)
This makes a fast-forward change and so documents the history completely. Still the changes introduced in the commits since $lastnicecommit
don't introduce any changes.
这会产生快进的变化,从而完整地记录历史。仍然是提交中引入的更改,因为$lastnicecommit
没有引入任何更改。