[git] git: 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 repo. I have been reading on this error for a few days now, I can't push to the repo anymore because I am getting the following error:

 ! [rejected]        master -> dev (fetch first)
error: failed to push some refs to 'https://[email protected]/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 commiting, and when I try to push again, I get the same error.

It is really frustrating, I really want to help out my team and contribute, but I can't because of this error. Does anyone know how to solve this issue? I would very much appreciate any help.

These are the commands I run in order to commit, if it helps anyone out:

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. Thanks again

Update: I should add that I have looked for a few hours on Google and stackoverflow, and followed different instructions, but I still can't push to the dev branch.

This question is related to git version-control merge commit bitbucket

The answer is


I had the same problem. It happened that I have created .Readme file on the repository without pulling it first.

You may want to delete .Readme file or pull it before pushing.


Force to push

git push -f origin master


I have done below steps. finally it's working fine.

Steps

1) git init

2) git status (for checking status)

3) git add . (add all the change file (.))

4) git commit -m "<pass your comment>"

5) git remote add origin "<pass your project clone url>"

6) git pull --allow-unrelated-histories "<pass your project clone url>" master

7) git push -u "<pass your project clone url>" master


I had this error and it was because there was an update on the server but SourceTree was not showing any updates available (possibly because I was offline when it last checked). So I did a refresh in source tree and now it shows 2 items to push instead of 1 item.

So be sure to press refresh or pull if you get this error and then try again.


The error possibly comes because of the different structure of the code that you are committing and that present on GitHub. You may refer to: How to deal with "refusing to merge unrelated histories" error:

$ git pull --allow-unrelated-histories
$ git push -f origin master

You need to input:

$ git pull
$ git fetch 
$ git merge

If you use a git push origin master --force, you will have a big problem.


You can try this: git pull origin master --rebase


The best option for me and it works and simple

git pull --rebase

then

git push

best of luck


You can override any checks that git does by using "force push". Use this command in terminal

git push -f origin master

However, you will potentially ignore the existing work that is in remote - you are effectively rewriting the remote's history to be exactly like your local copy.


This is how I solved this issue:

  1. git pull origin master
  2. git push origin master

This usually happens when your remote branch is not updated. And after this if you get an error like "Please enter a commit message" Refer to this ( For me xiaohu Wang answer worked :) )


This usually happens when the repo contains some items that are not there locally. So in order to push our changes, in this case we need to integrate the remote changes and then push.

So create a pull from remote

git pull origin master

Then push changes to that remote

git push origin master

I fixed it, I'm not exactly sure what I did. I tried simply pushing and pulling using:

git pull <remote> dev instead of git pull <remote> master:dev

Hope this helps out someone if they are having the same issue.


Well actually github is much simpler than we think and absolutely it happens whenever we try to push even after we explicitly inserted some files in our git repository so, in order to fix the issue simply try..

: git pull

and then..

: git push

Note: if you accidently stuck in vim editor after pulling your repository than don't worry just close vim editor and try push :)


git pull --rebase origin master

git push origin master


git push -f origin master

Warning git push -f origin master

  • forcefully pushes on existing repository and also delete previous repositories so if you don`t need previous versions than this might be helpful

It happens when we are trying to push to remote repository but has created a new file on remote which has not been pulled yet, let say Readme. In that case as the error says

git rejects the update

as we have not taken updated remote in our local environment. So Take pull first from remote

git pull

It will update your local repository and add a new Readme file. Then Push updated changes to remote

git push origin master

you can use

git pull --rebase <your_reponame> <your_branch>

this will help incase you have some changes not yet registered on your local repo. especially README.md


Examples related to git

Does the target directory for a git clone have to match the repo name? Git fatal: protocol 'https' is not supported Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) git clone: Authentication failed for <URL> destination path already exists and is not an empty directory SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443 GitLab remote: HTTP Basic: Access denied and fatal Authentication How can I switch to another branch in git? VS 2017 Git Local Commit DB.lock error on every commit How to remove an unpushed outgoing commit in Visual Studio?

Examples related to version-control

How can I switch to another branch in git? Do I commit the package-lock.json file created by npm 5? Project vs Repository in GitHub Remove a modified file from pull request Git push: "fatal 'origin' does not appear to be a git repository - fatal Could not read from remote repository." Git: How to squash all commits on branch git: updates were rejected because the remote contains work that you do not have locally Sourcetree - undo unpushed commits Cannot checkout, file is unmerged Git diff between current branch and master but not including unmerged master commits

Examples related to merge

Pandas Merging 101 Python: pandas merge multiple dataframes Git merge with force overwrite Merge two dataframes by index Visual Studio Code how to resolve merge conflicts with git? merge one local branch into another local branch Merging dataframes on index with pandas Git merge is not possible because I have unmerged files Git merge develop into feature branch outputs "Already up-to-date" while it's not How merge two objects array in angularjs?

Examples related to commit

git: updates were rejected because the remote contains work that you do not have locally How to add multiple files to Git at the same time Why Git is not allowing me to commit even after configuration? Git commit in terminal opens VIM, but can't get back to terminal How to configure Git post commit hook GIT commit as different user without email / or only email Editing the git commit message in GitHub How to amend a commit without changing commit message (reusing the previous one)? Temporarily switch working copy to a specific Git commit git - Your branch is ahead of 'origin/master' by 1 commit

Examples related to bitbucket

How to markdown nested list items in Bitbucket? Your configuration specifies to merge with the <branch name> from the remote, but no such ref was fetched.? Bitbucket git credentials if signed up with Google What I can do to resolve "1 commit behind master"? Bitbucket fails to authenticate on git pull Change remote repository credentials (authentication) on Intellij IDEA 14 git: updates were rejected because the remote contains work that you do not have locally How do I push a local repo to Bitbucket using SourceTree without creating a repo on bitbucket first? Clone private git repo with dockerfile How to move git repository with all branches from bitbucket to github?