Make the current Git branch a master branch

前端 未结 14 2140
悲&欢浪女
悲&欢浪女 2020-11-22 05:29

I have a repository in Git. I made a branch, then did some changes both to the master and to the branch.

Then, tens of commits later, I realized the branch is in muc

14条回答
  •  温柔的废话
    2020-11-22 06:05

    From what I understand, you can branch the current branch into an existing branch. In essence, this will overwrite master with whatever you have in the current branch:

    git branch -f master HEAD
    

    Once you've done that, you can normally push your local master branch, possibly requiring the force parameter here as well:

    git push -f origin master
    

    No merges, no long commands. Simply branch and push— but, yes, this will rewrite history of the master branch, so if you work in a team you have got to know what you're doing.




    Alternatively, I found that you can push any branch to the any remote branch, so:

    # This will force push the current branch to the remote master
    git push -f origin HEAD:master
    
    # Switch current branch to master
    git checkout master
    
    # Reset the local master branch to what's on the remote
    git reset --hard origin/master
    

提交回复
热议问题