在 Git 中回滚
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/218023/
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
Rolling back in Git
提问by Gwyn Morfey
I have committed, and pushed, several patches: A1-->A2-->A3-->A4 (HEAD)
我已经提交并推送了几个补丁:A1-->A2-->A3-->A4 (HEAD)
Everyone's pulled these changesets into their local copy.
每个人都将这些变更集拉到他们的本地副本中。
Now we want to "roll back" to A2, and continue developing from there - essentially throwing away A3 and A4. What's the best way to do this?
现在我们要“回滚”到 A2,并从那里继续开发——基本上是扔掉 A3 和 A4。做到这一点的最佳方法是什么?
回答by Aristotle Pagaltzis
From the root directory of your working copy just do
从你的工作副本的根目录做
git checkout A2 -- .
git commit -m 'going back to A2'
Using git revertfor this purpose would be cumbersome, since you want to get rid of a whole series of commits and revertundoes them one at a time.
git revert为此目的使用会很麻烦,因为您想要摆脱一系列提交并一次revert撤消它们。
You do not want git reseteither. That will merely change your masterbranch pointer: you are left with no record of the mistaken direction. It is also a pain to coordinate: since the commit you changed masterto is not a child of the remote repository's masterbranch pointer, pushing will fail – unless you add -f(force) or delete the masterbranch in the remote repository first and recreate it by pushing. But then everyone who tries to pull will still have the old history in their local masterbranch, so once origin/masterdiverges, git pullwill try to perform a merge. This is not the end of the world: they can get out of this situation by doing git rebase --onto origin/master $old_origin_master_commit master(ie. rebase their local commits made on top of the old origin/masteronto the top of the new origin/master). But Git will not know to do this automatically so you have to coordinate with every collaborator. In short, don't do that.
你也不想要git reset。那只会改变你的master分支指针:你不会留下错误方向的记录。协调也是一件痛苦的事情:由于您更改的提交master不是远程存储库master分支指针的子项,因此推送将失败——除非您先在远程存储库中添加-f(强制)或删除master分支,然后通过推送重新创建它。但是每个尝试拉取的人仍然会在他们的本地master分支中保留旧的历史记录,因此一旦origin/master发散,git pull将尝试执行合并。这不是世界末日:他们可以通过这样做来摆脱这种情况git rebase --onto origin/master $old_origin_master_commit master(即,将他们在旧的基础origin/master上所做的本地提交重新定位到新的基础上origin/master)。但是 Git 不知道自动执行此操作,因此您必须与每个协作者协调。简而言之,不要那样做。
回答by Pat Notz
Throwing away those commits will likely have some negative effects on anyone who is pulling from your repository. As another option, you may want to consider creating an alternate development branch starting at A2:
丢弃这些提交可能会对从您的存储库中提取的任何人产生一些负面影响。作为另一种选择,您可能需要考虑创建一个从 A2 开始的替代开发分支:
A1-->A2-->A3-->A4 (master/HEAD)
\
-->B1-->B2 (new-master/HEAD)
Doing this is as simple as
这样做很简单
git branch new-master master~2
回答by freespace
You want git-revertand To keep A3 and A4 and record the fact you are reverting, use git-resetdepending on how you want to treat A3 and A4. To remove all trace of A3 and A4, use git-reset --hard.git-revert.
您想要git-revert并要保留 A3 和 A4 并记录您正在还原的事实,请使用git-reset取决于您想如何对待 A3 和 A4。要删除 A3 和 A4 的所有痕迹,请使用git-reset --hard. git-revert.
edit: Aristotle Pagaltzis's git-checkoutsolution is superior, though for small reverts I don't see a problem with git-revert. None the less, I ask future upvotes be given to Aristotle Pagaltzis's answer
编辑:亚里士多德 Pagaltzis 的git-checkout解决方案是优越的,但对于小的恢复,我没有看到git-revert. 尽管如此,我要求未来对亚里士多德帕加尔齐斯的回答给予支持
I found git magicto be a good resource for git.
我发现git magic是一个很好的 git 资源。

