[英]Git: How do I remove a branch that has already been committed, pushed, and merged into master?
I have a branch that has been committed, pushed, and merged into master. 我有一个已提交,推送并合并到master的分支。 It did not pass QA.
它没有通过质量检查。 We need to pull that branch out of master for a release.
我们需要将该分支从master撤出以进行发布。 How do I pull that branch out of master.
我该如何将该分支从主服务器中拉出。 Assume the branch name is "to-be-removed"
假设分支名称为“将被删除”
You should have a history like this: 您应该具有这样的历史记录:
A--B--C---D--E <-- Master
\ /
Z--Y--X <-- Your feature
And you want to remove the commit D from the master branch. 并且您想从master分支中删除提交D。 You only need to revert this commit.
您只需要还原此提交。 There is a git command to this:
对此有一个git命令:
git revert -m 1 [sha_of_D]
where -m
mean the parent number of the commit. 其中
-m
表示提交的父代号。
It will create a new commit in master undoing the changes done by your feature branch. 它将在master中创建一个新的提交,以撤消功能分支所做的更改。 For more information, you can go to the main source .
有关更多信息,您可以转到主要资源 。
If, on the other hand, you want to really get rid of everything you've done since then, there are two possibilities. 另一方面,如果您想真正摆脱之后的所有工作,则有两种可能性。 One, if you haven't published any of these commits, simply reset:
一个,如果您尚未发布任何这些提交,只需重置:
# 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
On the other hand, if you've published the work, you probably don't want to reset the branch, since that's effectively rewriting history. 另一方面,如果您发布了作品,则可能不想重置分支,因为这实际上是在重写历史记录。 In that case, you could indeed revert the commits.
在这种情况下,您确实可以还原提交。 With git, revert has a very specific meaning: create a commit with the reverse patch to cancel it out.
使用git时,还原具有非常特殊的含义:使用反向修补程序创建提交以将其取消。 This way you don't rewrite any history.
这样,您就不会重写任何历史记录。
# 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.