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

时间:2014-02-19 18:46:21

标签: git merge undo

我有一个已经提交,推送和合并到master中的分支。它没有通过QA。我们需要从master中取出该分支以进行发布。如何将该分支拉出主分支。假设分支名称是“待删除”

2 个答案:

答案 0 :(得分:2)

你应该有这样的历史记录:

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

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

git revert -m 1 [sha_of_D]

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

它将在master中创建一个新的提交,撤消功能分支所做的更改。有关详细信息,请转到the main source

答案 1 :(得分:0)

根据this post

另一方面,如果你想真正摆脱自那时以来所做的一切,那么有两种可能性。一,如果您还没有发布任何这些提交,只需重置:

# 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,revert有一个非常具体的含义:使用反向补丁创建一个提交以取消它。这样您就不会重写任何历史记录。

# 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