[英]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.