简体   繁体   English

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

[英]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 . 有关更多信息,您可以转到主要资源

As per this post 根据这篇文章

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.

相关问题 我如何知道一个分支是否已经合并到 master 中? - How can I know if a branch has been already merged into master? 我在git的错误分支上创建了一个分支,并将旧分支合并到master中,如何从另一个分支中删除提交? - I created a branch off of the wrong branch in git and already merged the old branch into master, how do I remove the commits from the other branch? 如何检查master是否已合并到当前分支 - How do I check if master has been merged into current branch 我怎么知道分支是否已经在SVN / Mercurial / Git中合并? - How do I know if a branch has already been merged in SVN/Mercurial/Git? 已经与master合并的分支中的错误如何解决? - How to fix a bug in a branch after it has already been merged with master? 我可以安全地删除已合并到master的Git主题分支吗? - Can I safely remove my Git topic branch that has been merged into master? 从已推送的合并分支恢复特定提交 - Revert a specific commit from a merged branch that has already been pushed 如何获取所有提交到已经合并到master的分支的提交 - How to get all commits committed to a branch which is already merged to master 如果您已经将文件推送到远程分支,合并到开发分支并且它不是最新提交,如何从 git 中完全删除文件 - How to completely remove a file from git if you already pushed it to the remote branch, merged to the develop branch and it is not the latest commit 将 Master 合并到分支中,然后提交并将更改推送到分支。 如果没有强制推动,这怎么能撤消? - Merged Master into branch then committed and pushed changes to branch. How can this be undone without a force push?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM