CaribouCode
CaribouCode

Reputation: 14398

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

Upvotes: 5

Views: 5199

Answers (3)

Enrico Campidoglio
Enrico Campidoglio

Reputation: 59923

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.

Upvotes: 13

Seyed Ali Roshan
Seyed Ali Roshan

Reputation: 1574

you can go to master by git checkout master and use this command:

git merge --strategy-option theirs develop

this will automatically accept the develop branch changes if there are any conflicts

this is an example for it

option2 - you can also go to develop branch and remove master and then recreate master from develop like this:

git checkout develop
git branch -d master
git push -f :master
git checkout -b master

remember if you use something like gitlab, you must first turn the remote repository Protected Branches off for master

Upvotes: 1

Kent Munthe Caspersen
Kent Munthe Caspersen

Reputation: 6918

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

Upvotes: 1

Related Questions