gitversion-controlmergecommitbitbucket

Error "Updates were rejected because the remote contains work that you do not have locally"


I'm working on a team with a few developers using Git on Bitbucket. We are all working on a dev branch, not pushing to master until a release.

One of the developers committed incorrect code that overwrote my own by accident, and now I am trying to push the correct code back to the repository. I have been reading about this error for a few days now, and I can't push to the repository any more, because I am getting the following error:

 ! [rejected]        master -> dev (fetch first)
error: failed to push some refs to 'https://myusername@bitbucket.org/repo_user/repo_name.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

I follow the instructions and pull, but then I receive a merge conflict. After entering a message for the merge conflict, my local code is now the incorrect code that the other developer uploaded by accident (as expected from the pull). So I replace the incorrect code with the backup I copied before committing, and when I try to push again, I get the same error.

How can I solve this issue?

These are the commands I run in order to commit:

git pull remotename master:dev
git add --all
git commit -m "some message"
git pull remotename master:dev
git push remotename master:dev

I would have thought that if I kept this order, I would not receive merge conflicts. I guess I was wrong.

I have looked for a few hours on Google and Stack Overflow, and followed different instructions, but I still can't do a Git push to the dev branch.


Solution

  • git pull <remote> master:dev will fetch the remote/master branch and merge it into your local/dev branch.

    git pull <remote> dev will fetch the remote/dev branch, and merge it into your current branch.

    I think you said the conflicting commit is on remote/dev, so that is the branch you probably intended to fetch and merge.

    In that case, you weren't actually merging the conflict into your local branch, which is sort of weird since you said you saw the incorrect code in your working copy. You might want to check what is going on in remote/master.