5

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
CaribouCode
  • 13,998
  • 28
  • 102
  • 174
  • 1
    Possible duplicate of [Cannot push to GitHub - keeps saying need merge](https://stackoverflow.com/questions/10298291/cannot-push-to-github-keeps-saying-need-merge) – CodeCaster Jun 11 '18 at 11:00
  • Please read [ask] and show what you have tried. Paste that error into your favorite web search engine and read the results. – CodeCaster Jun 11 '18 at 11:01
  • @CodeCaster if you read my question you'll probably see the section where I say what I already tried... – CaribouCode Jun 11 '18 at 11:21
  • You tried one thing and it resulted in an error. I meant what you have tried to resolve that error. – CodeCaster Jun 11 '18 at 11:23
  • @CodeCaster The question you've suggested as a duplicate is not the same problem I'm having. My question wasn't how to resolve the error I'm getting at this stage, it was "I want to just overwrite everything in master with develop" and how do I do that. On searching around I see dozens of different suggestions on how to do this - I'm not finding one clear answer that seems to work. It's frustrating me that there are people like you on SO that waste everyone's time down voting and trying to close questions (especially with incorrect duplicate tickets) and useless comments like Google it. – CaribouCode Jun 11 '18 at 11:28
  • If you want to push a rewritten branch, you need to force push. It's not my fault you don't read the `-f` (or `--force`) parameter in the command mentioned in the duplicate. All answers you've received interpreted your question the same as I and suggest the same as in the duplicate. – CodeCaster Jun 11 '18 at 11:50

3 Answers3

11

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.

Enrico Campidoglio
  • 56,676
  • 12
  • 126
  • 154
1

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

Community
  • 1
  • 1
Kent Munthe Caspersen
  • 5,918
  • 1
  • 35
  • 34
1

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

Seyed Ali Roshan
  • 1,476
  • 1
  • 18
  • 37