繁体   English   中英

如何还原master的合并分支?

[英]How to revert back the merged branch from master?

我正在使用BitBucket,已经将分支推送到master并与master合并了,现在我需要从master还原推送的分支。

就我而言:我必须还原最近的五个提交,并且需要返回上一个代码。如何删除或还原提交?

注意:目前我没有任何分支,因为已经与master合并并删除了该分支。我只有一个master分支。

是否可以再次还原合并的分支?

了解您的历史记录将对您大有帮助。 我将以Dungeon Crawl的历史为例,并向您展示如何撤消合并提交:

假设当前它看起来像这样:

$ git log --graph --online --decorate -n 5
*   94e1b85 (HEAD, master) Merge branch 'next'
|\  
| * 9312efc Don't generate Lehudib's crystal spear the unrand.
| * 518382a Make Sticks to Snakes only work on missiles
| * 6e8ccac Let monsters pick up Elf:$ loot
* | 9ca6e02 Distinct manual tiles
* | a16bb39 Restore trunk changelog entries removed by 673ecc60.
* | 1d9921b Touch the 0.13 changelog header with the tentative release date.

您会发现git log --oneline --graph --decorate在这里很有用。 --graph将帮助您查看分支和合并,-- --oneline使其简短,-- --decorate将注释分支和标记名称到提交中。 (这是一个非常有用的命令,我为该名称的自定义版本提供了别名。)

看到94e1b85提交了吗? 这是一个合并提交。 假设我们要撤消它。 让我们备份一些东西,以防万一:

# Save master, just in case:
$ git checkout master
$ git branch old-master

首先,让我们还原旧分支。 我将其称为other-branch 或者:

$ git branch other-branch master^2

这意味着“在提交时创建一个名为other-branch的新分支,该other-branchmaster的第二个父( 2 )父( ^ )。您也可以通过哈希将其命名,您可以在git log命令中看到它” ve以上。

$ git branch other-branch 9312efc

历史记录现在应如下所示:

$ git log --graph --online --decorate -n 5
*   94e1b85 (HEAD, master) Merge branch 'next'
|\  
| * 9312efc (other-branch) Don't generate Lehudib's crystal spear the unrand.
| * 518382a Make Sticks to Snakes only work on missiles
| * 6e8ccac Let monsters pick up Elf:$ loot
* | 9ca6e02 Distinct manual tiles
* | a16bb39 Restore trunk changelog entries removed by 673ecc60.
* | 1d9921b Touch the 0.13 changelog header with the tentative release date.

现在,更改master点:

$ git checkout master $$ git reset --hard HEAD^

(您也可以在此处用提交哈希替换HEAD^ 。在^之后有一个隐式1

检查您的工作:

$ git log --oneline --graph -n 120 --decorate old-master
*   94e1b85 (old-master) Merge branch 'next'
|\  
| * 9312efc (other-branch) Don't generate Lehudib's crystal spear the unrand.
| * 518382a Make Sticks to Snakes only work on missiles
| * 6e8ccac Let monsters pick up Elf:$ loot
* | 9ca6e02 (HEAD, master) Distinct manual tiles
* | a16bb39 Restore trunk changelog entries removed by 673ecc60.
* | 1d9921b Touch the 0.13 changelog header with the tentative release date.

一旦您将事情放到想要的状态,将其推送到BitBucket,您很可能需要git push --force origin master 请注意:这将强制将Bitbucket上的master设置为您使用上述设置所设置的任何值。 另外:我们正在有效地重写历史记录。 如果其他任何人都基于我们撤消的合并提交进行了更改,则将给他们带来麻烦。 (他们需要将他们的提交重新建立在我们新近重写的历史记录上。)

好的,因此您将需要进行强制推动以解决此问题。 我不会还原合并,这会给您带来问题。 请注意,强制推送将意味着您正在重写历史记录,因此您需要让其他开发人员知道您正在执行此操作。

1./ reset master back to the master side parent commit
2./ recover your private branch, so you don't lose your commits.
3./ force push master.

该图有望使其清楚。 在此处输入图片说明

暂无
暂无

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

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