git 将推送的分支恢复为具体的提交
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10330970/
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
Revert pushed branch to a concrete commit
提问by Carlos Campderrós
I have merged a dev branch (with constant, sometimes unstable changes) to our master branch (where we store the released, stable code). I want to restore the master branch to the state it was before like the merge with the dev branch had never happened (and that when in the future we merge the dev branch all changes that we will discard now will be merged "again").
我已经将一个 dev 分支(具有持续的,有时是不稳定的更改)合并到我们的 master 分支(我们存储已发布的稳定代码)。我想将 master 分支恢复到之前的状态,就像与 dev 分支的合并从未发生过一样(并且将来我们合并 dev 分支时,我们现在将丢弃的所有更改都将“再次”合并)。
This is the current status of the master branch and I want it to have the 'professional-1.1.2' commit/tag at the HEAD.
这是主分支的当前状态,我希望它在 HEAD 处具有“professional-1.1.2”提交/标记。
I tried:
我试过:
$ git revert -n professional-1.1.2..HEAD
fatal: Commit 9167e846a387c793edbc089c7ab6bd9eb8260456 is a merge but no -m option was given.
$ git revert -n -m 1 professional-1.1.2..HEAD
fatal: Mainline was specified but commit 380169097f35d07466640bc0db1b639278cd25fa is not a merge.
$ git revert -n -m 2 professional-1.1.2..HEAD
fatal: Mainline was specified but commit 380169097f35d07466640bc0db1b639278cd25fa is not a merge.
After a bit of research I think that the better option is do a git reset --hard professional-1.1.2
and git push --force
as the answer to Git: How to ignore fast forward and revert origin [branch] to earlier commit?or reverting push'd git commit. Other developers are in the same office, and they should never commit anything to master (as neither should I, but... yeah, we don't have permissions per branch), so it's not a big problem to tell them and do any action required.
经过一些研究,我认为更好的选择是做一个git reset --hard professional-1.1.2
和git push --force
作为Git的答案:How to ignore fast forward and revert origin [branch] to early commit? 或恢复推送的 git commit。其他开发人员在同一个办公室,他们永远不应该向 master 提交任何内容(我也不应该,但是......是的,我们没有每个分支的权限),所以告诉他们并做任何事情都不是什么大问题需要采取的行动。
So in the end the question is: git revert something
or git reset --hard <TAG>
&& git push --force
? If git revert
, which commandline should I use?
所以最后的问题是:git revert something
还是git reset --hard <TAG>
&& git push --force
?如果git revert
,我应该使用哪个命令行?
采纳答案by ?imon Tóth
The -m number
option specifies which of the parents you want to revert to (since a merge has multiple parents).
该-m number
选项指定要恢复到哪个父级(因为合并有多个父级)。
So you want git revert -m 1 HEAD
or git revert -m 1 SHA_OF_MERGE_COMMIT
(assuming you did git checkout master; git merge devel;
)
所以你想要git revert -m 1 HEAD
或git revert -m 1 SHA_OF_MERGE_COMMIT
(假设你做了git checkout master; git merge devel;
)
回答by Mark Longair
If you just want to make the state of master
exactly the same as professional-1.1.2
, while avoiding rewriting history and force-pushing, you can just create a new commit on top of master that represents the same state of the project as professional-1.1.2
. You can do that with the following steps:
如果你只是想让状态与master
完全一样professional-1.1.2
,同时避免重写历史和强制推送,你可以在 master 之上创建一个新的提交,代表与 相同的项目状态professional-1.1.2
。您可以通过以下步骤做到这一点:
# Check that "git status" is clean, since the steps that follow will throw
# way uncommitted changes:
git status
# Set the index (staging area) to be as it was at professional-1.1.2:
git read-tree professional-1.1.2
# Create a commit based on that index:
git commit -m "Reverting to the state at professional-1.1.2"
# 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
As an alternative, you can follow the steps suggested in this answer by Charles Bailey, which accomplishes the same thing, but is slightly more confusing, I think (even though the steps I've suggested involve the "plumbing" command git read-tree
).
作为替代方案,您可以按照Charles Bailey在此答案中建议的步骤进行操作,它可以完成相同的操作,但我认为稍微有些混乱(即使我建议的步骤涉及“管道”命令git read-tree
)。
回答by Sergey K.
If you are a daredevil (recovering from an upstream rebase could be necessary for all other committers)
如果你是一个勇敢的人(所有其他提交者可能需要从上游 rebase 中恢复)
git checkout yourbranch
git reset HEAD <commit-hash>
git push origin yourbranch -f