简体   繁体   中英

Overwrite everything in master with another branch in git

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 two?

I don't care about anything in master , it is way out of date.

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 .

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 ).

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM