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

441,987

Solution 1

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.

Solution 2

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.

Solution 3

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

Solution 4

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

Solution 5

Force to push

git push -f origin master

Share:
441,987

Related videos on Youtube

thanos
Author by

thanos

Updated on July 30, 2022

Comments

  • thanos
    thanos almost 2 years

    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 about 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 committing, 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.

    • Scott Stensland
      Scott Stensland about 3 years
      this same error message is shown when you issue git push when currently in dir from another repo .... the git message should get updated to reflect this especially since its tone sounds so authoritative one might be convinced otherwise
  • thanos
    thanos almost 10 years
    Wow...I never knew that. But it makes a lot of sense now. The master branch was also incorrect, so your answer clears my whole question. I'm still a bit new to git. Thanks a lot for telling me the difference between those two!
  • Derek Foulk
    Derek Foulk almost 9 years
    Best option for me was git pull --rebase.
  • Spaideri
    Spaideri about 7 years
    Using force push (-f) flag is very dangerous and it should never be part of your regular work flow
  • Melebius
    Melebius about 6 years
    Downvoted since I’m missing some warning in this answer.
  • Melebius
    Melebius about 6 years
    Why do you need to use git fetch and git merge again manually after running git pull which contains them?
  • Azarsa
    Azarsa almost 6 years
    Ooh! This force the repository to rewrite itself.
  • Marcelo
    Marcelo almost 6 years
    I was doing git pull origin develop into my local develop branch, but now, just doing git pull it works fine to me, I dunno why.
  • Himanshu
    Himanshu almost 6 years
    because by default if your local branch is synched with remote branch and you are checked out in that branch you don't need to specify a branch just git pull is enough
  • maxshuty
    maxshuty over 4 years
    Yeah this saved me since I didn't need anything in the existing repo, but definitely a scary command
  • simon
    simon over 4 years
    I had the same error with github and I fixed it with this command, @theeastcoastwest why are you saying this is dangerous? what is your reason"
  • Chris
    Chris about 4 years
    This should probably come with a warning.
  • Gásten
    Gásten about 4 years
    @simon this is dangerous because it ignores the work that is in remote and it forces your changes onto the repo. So if you don't want to mess up your team's work, DO NOT force push.
  • Sotiris Koukios-Panopoulos
    Sotiris Koukios-Panopoulos about 4 years
    I'm not sure this answer to a 5year old question provides any additional value, plus It does not provide a solution to the specific problem of the OP. Since you're a new contributor, please take a look at a guide on how to answer questions: stackoverflow.com/help/how-to-answer
  • Akshaya Natarajan
    Akshaya Natarajan almost 4 years
    Hi Eduardo! This worked for me. But can you explain why it works? What does this command do exactly?
  • eric
    eric almost 4 years
    Raise your hand if you did this and feel guilty but would do it again.
  • VishalParkash
    VishalParkash almost 4 years
    This command helped me to escape the situation, but I will never use this command. ;) Thank you by the way. upvoted
  • Sabito 錆兎 stands with Ukraine
    Sabito 錆兎 stands with Ukraine over 3 years
    @Donal Please consider editing in a warning in this answer. git push -force is evil
  • Nico Haase
    Nico Haase over 3 years
    Even if this surely will work, would you mind explaining it? Could there possibly be any danger in doing this? If yes, explain that even further, like: "Folks, be sure that you will loose commits using this"
  • Nico Haase
    Nico Haase over 3 years
    Please explain further why adding a readme file should lead to the given error message
  • Nico Haase
    Nico Haase over 3 years
    Even if this surely will work, would you mind explaining it? Could there possibly be any danger in doing this? If yes, explain that even further, like: "Folks, be sure that you will loose commits using this"
  • Aris
    Aris over 3 years
    this is the only solution when you want to initially push a repository, but you get errors. should be done only once though.
  • Ahnaaf Al Rafee
    Ahnaaf Al Rafee about 3 years
    git pull <remote> master:dev What is dev here ?
  • Ahnaaf Al Rafee
    Ahnaaf Al Rafee about 3 years
    git pull helps
  • Harlin
    Harlin about 3 years
    Agree with Spaideri's comment (because this is not a normal process especially when working on a repo with others AND because warnings provided by others in this very comment list) but occasionally on a brand new repo init, this can be helpful.
  • IgorGanapolsky
    IgorGanapolsky about 3 years
    I get an error: error: src refspec master does not match any error: failed to push some refs to
  • IgorGanapolsky
    IgorGanapolsky about 3 years
    Error: fatal: refusing to merge unrelated histories
  • Donovan P
    Donovan P almost 3 years
    Nothing wrong with using -f when initializing a newly created repository. Just make sure you know what you do when using -f on the existing repository.
  • Lasse V. Karlsen
    Lasse V. Karlsen almost 3 years
    To moderator: I got pinged by Stack Overflow and automatically assumed this was my answer. I do not know why that happened. Please ignore my comment and my flag.
  • Miguel Gonzalez
    Miguel Gonzalez over 2 years
    True but dangerous
  • Usama Tahir
    Usama Tahir over 2 years
    it will remove all the old commits.
  • Liso
    Liso over 2 years
    Sad, I lost my 162 commit by this. I don't know much about git as I only edit them from my browser, I tried git command line and this happen :(
  • Nghien Nghien
    Nghien Nghien about 2 years
    After add manually file README.md on site GitHub. I also can't push project from device to Git like normally. Your anwser helped me so much!!!! Thanks bro <3