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

Don*_*mmy 1 git

我一直在尝试master用另一个分支替换repo的分支,但我尝试的每一种方式,它都保留了不在另一个分支中的提交.例如,另一个分支有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
Run Code Online (Sandbox Code Playgroud)

尝试#2

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

#Also tried
git checkout otherBranch
git push git@github.com:remoteRepo +otherBranch:master
Run Code Online (Sandbox Code Playgroud)

两者都以相同的结果结束.

那么如何用其他分支完全替换master呢?

Chr*_*aes 6

首先备份你的主分支:

git branch master_backup master
Run Code Online (Sandbox Code Playgroud)

然后执行以下操作以清除当前的master并将otherBranch设为您的新master:

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
Run Code Online (Sandbox Code Playgroud)