如何用另一个分支完全替换master及其所有提交?

时间:2017-01-23 14:55:28

标签: git

我一直试图用另一个分支替换repo的master分支,但我尝试的每一种方式,它都保留了在另一个分支中不提交的提交。例如,另一个分支有121个提交,但是在命令之后(两个集合都在下面),master显示216个提交!

尝试#1

#Replacing master with otherBranch branch
git checkout otherBranch
git merge -s ours master
git checkout master
git merge otherBranch 
git push

尝试#2

git checkout otherBranch
git push git@github.com:remoteRepo :master

#Also tried
git checkout otherBranch
git push git@github.com:remoteRepo +otherBranch:master

这两个结果都是相同的结果。

那么如何用另一个分支完全替换master呢?

1 个答案:

答案 0 :(得分:6)

首先备份你的主分支:

git branch master_backup master

然后执行以下操作以清除当前的主人并使otherBranch成为您的新主人:

git checkout master # switch to master branch
git reset --hard otherBranch # dangerous command: make master point to where otherBranch is. You will loose work that was on master branch and not on otherBranch
git push -f origin master # push this new master branch to origin; thereby crushing (-f) whatever was on master branch on origin