【问题标题】:How do I uncommit last merge on a protected branch?如何取消提交受保护分支上的最后一次合并?
【发布时间】:2021-08-15 02:02:26
【问题描述】:

如何取消提交受保护分支上的最后一次合并?

git reset --hard HEAD~1

git push -f origin master

在注意到最后一次合并在生产环境中存在 UI 错误后,我执行了以下操作,但在本地环境中没有。

$ git push -f origin master
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
remote: error: GH006: Protected branch update failed for refs/heads/master.
remote: error: Cannot force-push to this protected branch
To https://github.com/Ivory/Web.git
 ! [remote rejected]     master -> master (protected branch hook declined)
error: failed to push some refs to 'https://github.com/Ivory/Web.git'

但是,我无法执行强制推送,有没有办法取消提交更改?

【问题讨论】:

  • 恢复提交(使用git revert)可以工作,因为它会创建一个新的提交来恢复更改。但是,我不知道它如何处理合并提交。
  • 要么暂时取消对分支的保护,要么推送一个提交以恢复合并所做的更改。
  • 如何推送可以快速还原更改而无需手动执行的提交?
  • “快”是什么意思? “手动”是什么意思?

标签: git git-branch git-commit


【解决方案1】:

有不同级别的受保护分支。我不知道在您的情况下,保护是否需要 PR 合并到 master,或者您是否可以推送新提交,但不能强制推送。除非您可以让某人暂时禁用保护或让您暂时强制推送,否则您将需要恢复合并:

git fetch # update your branches
git checkout master 
git reset --hard @{u} # make your copy of master look like origin/master
git log # copy the merge commit ID that you want to revert (press q to exit)
git revert -m 1 <commit-id-to-revert>
git log # View the log message of your new commit and verify that it is correct
# verify your code looks correct (obviously)
git push

如果推送有效,那么您的“保护”只是防止强制推送。如果仍然失败,则创建一个新分支并将其 PR 到 master

git checkout -b fixup-bad-merge # or whatever you want to call this branch
git push
# now create your PR into master and do what needs to be done to complete it

【讨论】:

    【解决方案2】:

    正如其他用户在 cmets 中所说,您有两种选择:

    1. 取消保护您的分支,我想这不是一个选项,否则您不会发布问题
    2. git revert合并分支带来的变化

    为了简单起见(目前),这可能是您需要运行的命令:

    git revert -m 2 HEAD

    其中HEAD 引用合并提交,-m 2 仅表示您要还原由第二个父项引入的更改。但是,在还原合并提交时,您应该注意

    还原合并提交声明您永远不会想要树 合并带来的变化。结果,以后的合并只会 引入由非祖先的提交引入的树更改 先前还原的合并。这可能是也可能不是您想要的。

    来自git revert documentation

    基本上,在您恢复合并提交后,您只是撤消更改,但历史记录仍将来自“恢复的分支”的提交视为主线的祖先。因此,如果将来您想再次合并该分支,即使在发现错误的同一分支中修补了新提交后,您也不会看到属于合并提交的还原父级的提交带来的更改。这种情况下的解决方案是git revert 恢复的提交。

    我知道,没有一些图片听起来有点复杂,Linus Torvalds 解释得比我好很多here

    【讨论】:

      猜你喜欢
      • 2017-07-21
      • 2012-08-31
      • 2019-09-26
      • 2017-06-19
      • 1970-01-01
      • 2021-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多