如何从master恢复合并的分支?

时间:2014-03-07 04:20:24

标签: git github bitbucket

我正在使用BitBucket。我已经将一个分支推送到master并与master合并。现在我需要从master恢复推送的分支。如何做?

在我的情况下:我必须恢复最后五次提交,我需要返回上一段代码。如何删除或恢复提交?

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

可以再次恢复合并的分支吗?

2 个答案:

答案 0 :(得分:1)

了解您的历史记录会有很大帮助。我将以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的新分支,这是2的第二个(^)父(master)。您也可以通过哈希命名,您可以在我上面使用的git log命令中看到。

$ 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上的主人设置为使用上述设置的任何内容。 另外:我们正在有效地重写历史。如果其他人对我们撤消的合并提交进行了基础更改,那么我们将为他们带来问题。 (他们需要将他们的承诺改写为我们新重写的历史。)

答案 1 :(得分:0)

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

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.

这张图希望能说清楚。enter image description here