git 从 GitHub.com 上的受保护分支恢复合并提交
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42548836/
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 a merge commit from a protected branch on GitHub.com
提问by nak
We have protected our develop branch on GitHub so that nobody downstream can push their commit directly. The commits need to go through specific feature branch and get merged through a pull request.
我们在 GitHub 上保护了我们的开发分支,这样下游的任何人都不能直接推送他们的提交。提交需要通过特定的功能分支并通过拉取请求合并。
There came a scenario where a feature branch is merged into the develop branch (after proper review and changes) and we are required to revert it later (maybe due to changes in requirements). If I try to revert the merge commit downstream, it will not allow me to push, since the branch is protected. I remember GitHub providing revert button when we merge the branch. But somehow I am not able to see (or find) the button now. We needed to revert the commit on priority so we removed the protection from the develop branch for the time being and pushed the revert commit (ugliest hack).
出现了一个场景,功能分支被合并到开发分支(经过适当的和更改后),我们需要稍后将其还原(可能是由于需求的变化)。如果我尝试将合并提交还原到下游,它将不允许我推送,因为该分支是受保护的。我记得当我们合并分支时,GitHub 提供了恢复按钮。但不知何故,我现在无法看到(或找到)按钮。我们需要优先恢复提交,因此我们暂时从开发分支中删除了保护并推送了恢复提交(最丑陋的黑客)。
Are there any other better alternative for reverting a commit from protected branch? Maybe I am missing or misunderstood some GitHub features.
还有其他更好的替代方法可以从受保护的分支恢复提交吗?也许我遗漏或误解了某些 GitHub 功能。
One more scenario is, what if I have deleted the branch from GitHub after I have merged, how would I revert it then?
另一种情况是,如果我在合并后从 GitHub 中删除了分支,那我将如何恢复它?
采纳答案by rink.attendant.6
Reverting on GitHub
在 GitHub 上恢复
You don't need to restore (undelete) branches on GitHub to revert merge commits resulting from pull requests. For example:
您不需要在 GitHub 上恢复(取消删除)分支来恢复由拉取请求产生的合并提交。例如:
Non-revertible pull request
不可恢复的拉取请求
Sometimes the revert button doesn't appear. From GitHub Help on reverting a pull request:
有时不会出现还原按钮。从 GitHub 帮助恢复拉取请求:
Note: You may need to use Git to manually revert the individual commits if:
- Reverting the pull request causes merge conflicts
- The original pull request was not originally merged on GitHub (for example, using a fast-forward merge on the command line)
注意:如果出现以下情况,您可能需要使用 Git 手动还原单个提交:
- 恢复拉取请求会导致合并冲突
- 原始拉取请求最初没有在 GitHub 上合并(例如,在命令行上使用快进合并)
It took me a while to find an example, but if the head branch wasn't merged into the base branch using the big green button on GitHub then it can't be reverted on GitHub:
我花了一段时间才找到一个例子,但是如果没有使用 GitHub 上的绿色大按钮将头部分支合并到基本分支中,那么它就无法在 GitHub 上恢复:
git revert
git revert
Locally on the command line, you can use the git revert
command to revert changes.
在命令行本地,您可以使用该git revert
命令来恢复更改。
This will work on both your protected branch and a downstream branch. git revert
creates a new commit ahead of the current HEAD so you don't need to force push, and if from a downstream branch, you can manually create a pull request for the reverted changes.
这将适用于您的受保护分支和下游分支。git revert
在当前 HEAD 之前创建一个新提交,因此您无需强制推送,如果来自下游分支,您可以手动为已恢复的更改创建拉取请求。
Reverting a merge commit is slightly more complicated than reverting a single-parent commit, so I'd suggest taking a look at this questionfor more information, as it's something I've never done before.
恢复合并提交比恢复单父提交稍微复杂一些,所以我建议查看这个问题以获取更多信息,因为这是我以前从未做过的事情。
If people aren't comfortable using the command line, I think SourceTreehas an item on the context menu to revert a commit but I don't know how it handles merge commits. There might be similar options in other GUI applications.
如果人们不习惯使用命令行,我认为SourceTree在上下文菜单上有一个项目可以恢复提交,但我不知道它如何处理合并提交。其他 GUI 应用程序中可能有类似的选项。
Hope this helps!
希望这可以帮助!
回答by Vinod Kumar
The most easiest option is to perform the following
最简单的选择是执行以下操作
git revert -m 1 "last_commit_id"