如何用另一个分支替换一个分支的历史?

时间:2018-07-11 15:08:09

标签: git branch git-rewrite-history git-p4

我有从Perforce迁移到Git的存储库。每次我在每个存储库上执行一个git p4 rebase(其中p4是git-p4的别名),以便从Perfoce到Git进行新的更改。除了该注定的存储库(称为GitRepo)以外,此过程在所有存储库上都可以正常运行。

由于某种原因,每次我将GitRepo与Perforce同步时,都会得到一个空的合并提交,如下所示:

enter image description here

在过去的两个月中,没有任何变化。所以历史现在看起来像这样:

enter image description here

注意提交4/6/2018和3/30/2018吗?不确定为什么会在2018年6月25日之后(可能是问题的根源),并且确实在历史上已经存在了它们。

长话短说,我已经检查了6/25/2018的提交并将历史推到新的分支Branch-A。

Branch-A完全符合我的需求,我该如何重写master的历史记录,成为Branch-A中的主人?

1 个答案:

答案 0 :(得分:1)

如果确实(如评论所示)独自在此存储库上工作,则可以利用-f命令的branch选项提供的可能性,并随意设置主分支在任何给定的提交上(或在文档中称为 committish ,这意味着可以用于指向给定提交的任何引用,例如分支,标签等)

# First you can, as a backup, set a branch where master is at the start of the process
git checkout master
git checkout -b backupMaster

# Okay, now we force (-f) the master branch on the same commit where BranchA is
git branch -f master BranchA

# Local is clear, now is time to update your remote with
# your rewritten version of repo history
git push -f origin master

现在master与BranchA在同一点,直到他们的历史为止,因为这是他们俩都指向的同一提交。而分支backupMaster在这里,以防万一您出于任何原因改变主意。

相关问题