Overwrite everything in master with another branch in git

后端 未结 4 1908
迷失自我
迷失自我 2021-01-11 10:20

I have a very out of date master branch in a git repository.

All of my work has been done in another branch.

What is the best way to merge the

相关标签:
4条回答
  • 2021-01-11 10:47

    If you are fine with losing the history of your master branch you just let master point to the head of your current branch (your don't "overwrite" master - the branch - per se):

    git checkout yourotherbranch
    git branch -D master
    git checkout -b master
    

    Of course if you have a remote clone, you'll then have to

    git push -f origin master 
    

    Nota bene: this specifically applies to replacing the whole of your master branch and throwing the old master away as the title suggests. Otherwise, you should be fine with git merge master --strategy=ours.

    0 讨论(0)
  • 2021-01-11 10:49

    Lots of these questions are answered with the git merge strategy=ours solution, but I think it misses the point of the question. I interpret the question as "i didn't follow good practice and basically the master is useless, and i want nothing from the master". common for solo projects.

    git push -f origin branch_ive_been_working_for_months:master fully replaces your remote master with your remote branch. then, just git pull origin master after locally checking out master is the solution that directly answers question IMO.

    0 讨论(0)
  • 2021-01-11 10:52

    A complete overwrite isn't merging the other content, it's abandoning it.

    git checkout -B master anotherbranch
    

    This has the advantage over a delete-and-recreate of retaining your branch settings and reflogs.

    If there's some administrative requirement to retain worthless commits in your history, follow that with git merge -s ours master@{1}. I prefer this sequence because it generates an unusual merge message, which will alert scanning --oneline logs that the merge is unusual.

    0 讨论(0)
  • 2021-01-11 10:59

    Let's assume your work is in a branch called dev:

    git checkout dev 
    git merge --strategy=ours master
    git checkout master 
    git merge dev
    

    The merge option --strategy=ours is meant to be used to supersede old development history of side branches (quote).

    0 讨论(0)
提交回复
热议问题