繁体   English   中英

Git:如何删除已经提交,推送并合并到master的分支?

[英]Git: How do I remove a branch that has already been committed, pushed, and merged into master?

我有一个已提交,推送并合并到master的分支。 它没有通过质量检查。 我们需要将该分支从master撤出以进行发布。 我该如何将该分支从主服务器中拉出。 假设分支名称为“将被删除”

您应该具有这样的历史记录:

A--B--C---D--E      <-- Master
 \       /
  Z--Y--X          <-- Your feature

并且您想从master分支中删除提交D。 您只需要还原此提交。 对此有一个git命令:

git revert -m 1 [sha_of_D]

其中-m表示提交的父代号。

它将在master中创建一个新的提交,以撤消功能分支所做的更改。 有关更多信息,您可以转到主要资源

根据这篇文章

另一方面,如果您想真正摆脱之后的所有工作,则有两种可能性。 一个,如果您尚未发布任何这些提交,只需重置:

# This will destroy any local modifications.
# Don't do it if you have uncommitted work you want to keep.
git reset --hard 0d1d7fc32

# Alternatively, if there's work to keep:
git stash
git reset --hard 0d1d7fc32
git stash pop
# This saves the modifications, then reapplies that patch after resetting.
# You could get merge conflicts, if you've modified things which were
# changed since the commit you reset to

另一方面,如果您发布了作品,则可能不想重置分支,因为这实际上是在重写历史记录。 在这种情况下,您确实可以还原提交。 使用git时,还原具有非常特殊的含义:使用反向修补程序创建提交以将其取消。 这样,您就不会重写任何历史记录。

# This will create three separate revert commits:
git revert 0766c053 25eee4ca a867b4af

# It also takes ranges. This will revert the last two commits:
git revert HEAD~2..HEAD

# To get just one, you could use `rebase -i` to squash them afterwards
# Or, you could do it manually (be sure to do this at top level of the repo)
# get your index and work tree into the desired state, without changing HEAD:
git checkout 0d1d7fc32 .
# and then commit
git commit    # be sure and write a good message describing what you just did

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM