如何删除已经推送到原点的合并分支
How can I remove merged branch which is already push to origin
例如,我有 1 个缺陷问题,开发人员创建了新分支,并在分支名称 "fix_#1_backyard_data_displayed" 中修复了它。然后,我将它合并到分支 "beta" 中进行测试。过了一会儿,另一个测试人员将 "fix_2" 和 "fix_3" 到 "fix_20" 的另一个分支合并到 "beta" 并进行测试。在那之后,我发现我的第一个合并 "fix_#1_backyard_data_displayed" 分支有问题,使我们的应用程序不稳定。
现在,如何在不干扰 "fix_2" 和 "fix_3" 到 "fix_20" 的情况下删除已经推送到原点的合并分支?
简答
在 beta
分支中:
- 找到与合并对应的提交的 SHA1。
- 运行
git revert <sha1> -m 1
有一个警告:Git 仍然会认为这个特性分支被合并到 beta
分支中,要再次合并这个分支你需要(再次)恢复提交git revert
合并前引入。
更长的版本
Git 中撤消提交的命令是 revert
。对于合并提交,您需要使用 -m
开关指定 "mainline" 分支。在你的情况下,它可能应该是 1:
Usually you cannot revert a merge because you do not know which side of the merge should be considered the
mainline. This option specifies the parent number (starting from 1) of the mainline and allows revert to
reverse the change relative to the specified parent.
Reverting a merge commit declares that you will never want the tree changes brought in by the merge. As a
result, later merges will only bring in tree changes introduced by commits that are not ancestors of the
previously reverted merge. This may or may not be what you want.
See the revert-a-faulty-merge How-To[1] for more details.
(来源:git help revert
)
下面的 sh
脚本对此进行了演示。第一个 revert
命令恢复合并 fix1
分支的提交,在本例中它是倒数第二个提交,即 HEAD^
。还原后,在再次合并 fix1
分支之前,需要使用另一个 revert
命令撤消还原:
#!/bin/sh
git init test
cd test
touch a
git add a
git commit -m a
git checkout -b fix1
touch b
git add b
git commit -m b
git checkout -b fix2 master
touch c
git add c
git commit -m c
git checkout master
git merge --no-ff --no-edit fix1
git merge --no-ff --no-edit fix2
git revert --no-edit HEAD^ -m 1
git checkout fix1
echo "fix b" > b
git add b
git commit -m bb
git checkout master
# git merge fix1 # this will give an error!
# revert changes introduced by revert before merging
git revert --no-edit HEAD
git merge --no-ff --no-edit fix1
我在这里找到了解决方案:
branch_to_revert="fix1"; git log --all --pretty=format:"%H %s" | grep -i "Merge branch '$branch_to_revert'" | grep -v "Revert" | awk '{print }' | xargs git revert -m 1
例如,我有 1 个缺陷问题,开发人员创建了新分支,并在分支名称 "fix_#1_backyard_data_displayed" 中修复了它。然后,我将它合并到分支 "beta" 中进行测试。过了一会儿,另一个测试人员将 "fix_2" 和 "fix_3" 到 "fix_20" 的另一个分支合并到 "beta" 并进行测试。在那之后,我发现我的第一个合并 "fix_#1_backyard_data_displayed" 分支有问题,使我们的应用程序不稳定。
现在,如何在不干扰 "fix_2" 和 "fix_3" 到 "fix_20" 的情况下删除已经推送到原点的合并分支?
简答
在 beta
分支中:
- 找到与合并对应的提交的 SHA1。
- 运行
git revert <sha1> -m 1
有一个警告:Git 仍然会认为这个特性分支被合并到 beta
分支中,要再次合并这个分支你需要(再次)恢复提交git revert
合并前引入。
更长的版本
Git 中撤消提交的命令是 revert
。对于合并提交,您需要使用 -m
开关指定 "mainline" 分支。在你的情况下,它可能应该是 1:
Usually you cannot revert a merge because you do not know which side of the merge should be considered the mainline. This option specifies the parent number (starting from 1) of the mainline and allows revert to reverse the change relative to the specified parent.
Reverting a merge commit declares that you will never want the tree changes brought in by the merge. As a result, later merges will only bring in tree changes introduced by commits that are not ancestors of the previously reverted merge. This may or may not be what you want.
See the revert-a-faulty-merge How-To[1] for more details.
(来源:git help revert
)
下面的 sh
脚本对此进行了演示。第一个 revert
命令恢复合并 fix1
分支的提交,在本例中它是倒数第二个提交,即 HEAD^
。还原后,在再次合并 fix1
分支之前,需要使用另一个 revert
命令撤消还原:
#!/bin/sh
git init test
cd test
touch a
git add a
git commit -m a
git checkout -b fix1
touch b
git add b
git commit -m b
git checkout -b fix2 master
touch c
git add c
git commit -m c
git checkout master
git merge --no-ff --no-edit fix1
git merge --no-ff --no-edit fix2
git revert --no-edit HEAD^ -m 1
git checkout fix1
echo "fix b" > b
git add b
git commit -m bb
git checkout master
# git merge fix1 # this will give an error!
# revert changes introduced by revert before merging
git revert --no-edit HEAD
git merge --no-ff --no-edit fix1
我在这里找到了解决方案:
branch_to_revert="fix1"; git log --all --pretty=format:"%H %s" | grep -i "Merge branch '$branch_to_revert'" | grep -v "Revert" | awk '{print }' | xargs git revert -m 1