Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overwrite master with develop

Some how in our project our master branch has completely broken. It thinks it's ahead by a few commits, and if we try merging develop in it shows huge amounts of conflicts. It's been broken for a long time now so a lot has happened in develop (too many conflicts to resolve).

I want to just overwrite everything in master with develop (so master basically equals develop). How do I do this?

I've tried deleting everything in master locally then running the following:

git checkout master
git reset --hard develop

Seems to give me all the correct files but when I try to push master I get this error:

Updates were rejected because the tip of your current branch is behind
like image 411
CaribouCode Avatar asked Dec 14 '22 16:12

CaribouCode


2 Answers

I want to just overwrite everything in master with develop (so master basically equals develop). How do I do this?

The --force option of git-branch will do exactly that:

git checkout develop
git branch -f master develop

Notice how you need to switch to a different branch before modifying master, since git-branch will refuse to change the current branch.

Alternatively, you can use the git-update-ref plumbing command:

git update-ref refs/heads/master refs/heads/develop

which allows you to modify any reference, regardless of where HEAD is currently pointing to. Just be aware that git-update-ref treats the arguments as file paths relative to the .git directory.

Finally, you need to use the -f --force option of git-push to rewrite the remote master branch as well:

git push -f origin master

From the documentation:

Usually, "git push" refuses to update a remote ref that is not an ancestor of the local ref used to overwrite it.

This option overrides this restriction if the current value of the remote ref is the expected value. "git push" fails otherwise.

like image 169
Enrico Campidoglio Avatar answered Dec 17 '22 23:12

Enrico Campidoglio


How are you pushing to master? You cannot do a regular push in this case.

Have you tried to force push with git push -f? Force pushing will override history on the remote, and should be done with caution.

-f
--force
Usually, the command refuses to update a remote ref that is not an ancestor of the local ref used to overwrite it. Also, when --force-with-lease option is used, the command refuses to update a remote ref whose current value does not match what is expected.

This flag disables these checks, and can cause the remote repository to lose commits; use it with care.

Note that --force applies to all the refs that are pushed, hence using it with push.default set to matching or with multiple push destinations configured with remote.*.push may overwrite refs other than the current branch (including local refs that are strictly behind their remote counterpart). To force a push to only one branch, use a + in front of the refspec to push (e.g git push origin +master to force a push to the master branch). See the ... section above for details.

Source: https://git-scm.com/docs/git-push

like image 21
Kent Munthe Caspersen Avatar answered Dec 17 '22 23:12

Kent Munthe Caspersen