merge overwrite - How to replace master branch in Git, entirely, from another branch?
with local (5)
Make the current Git branch a master branch
I have two branches in my Git repository:
seotweaks(created originally from
seotweaks with the intention of quickly merging it back into
master. However, that was three months ago and the code in this branch is 13 versions ahead of
It has effectively become our working master branch as all the code in
master is more or less obsolete now.
Very bad practice I know, lesson learned.
Do you know how I can replace all of the contents of the
master branch with those in
I could just delete everything in
master and merge, but this does not feel like best practice.
What about using git branch -m to rename the master branch to another one, then rename seotweaks branch to master? Something like this:
git branch -m master old-master git branch -m seotweaks master git push -f origin master
This might remove commits in origin master, please check your origin master before running
git push -f origin master.
You should be able to use the "ours" merge strategy to overwrite master with seotweaks like this:
git checkout seotweaks git merge -s ours master git checkout master git merge seotweaks
The result should be your master is now essentially seotweaks.
-s ours is short for
From the docs about the 'ours' strategy:
This resolves any number of heads, but the resulting tree of the merge is always that of the current branch head, effectively ignoring all changes from all other branches. It is meant to be used to supersede old development history of side branches. Note that this is different from the -Xours option to the recursive merge strategy.
seotweaks was originally created as a branch from
master, merging it back in is a good idea. However if you are in a situation where one of your branches is not really a branch from
master or your history is so different that you just want to obliterate the
master branch in favor of the new branch that you've been doing the work on you can do this:
git push [-f] origin seotweaks:master
This is especially helpful if you are getting this error:
! [remote rejected] master (deletion of the current branch prohibited)
And you are not using GitHub and don't have access to the "Administration" tab to change the default branch for your remote repository. Furthermore, this won't cause down time or race conditions as you may encounter by deleting master:
git push origin :master
You can rename/remove master on remote, but this will be an issue if lots of people have based their work on the remote master branch and have pulled that branch in their local repo.
That might not be the case here since everyone seems to be working on branch '
In that case you can:
git remote --show may not work. (Make a
git remote show to check how your remote is declared within your local repo. I will assume '
(Regarding GitHub, house9 comments: "I had to do one additional step, click the '
Admin' button on GitHub and set the '
Default Branch' to something other than '
master', then put it back afterwards")
git branch -m master master-old # rename master on local git push origin :master # delete master on remote git push origin master-old # create master-old on remote git checkout -b master seotweaks # create a new local master on top of seotweaks git push origin master # create master on remote
- if other users try to pull while master is deleted on remote, their pulls will fail ("no such ref on remote")
- when master is recreated on remote, a pull will attempt to merge that new master on their local (now old) master: lots of conflicts. They actually need to
reset --hardtheir local master to the remote/master branch they will fetch, and forget about their current master.
You can do this is just 3 simple step that i used.
1) make new branch where you want to commit you recent update.
git branch <branch name>
2) Find Recent Commit Id for commit on new branch.
3) Copy that commit id note that Most Recent commit list take place on top. so you can find your commit. you also find this via message.
git cherry-pick d34bcef232f6c...
you can also provide some rang of commit id.
git cherry-pick d34bcef...86d2aec
Now your job done. If you picked correct id and correct branch then you will success. So before do this be careful. else another problem can occur.
Now you can push your code