Updates were rejected because the remote contains work that you do not have locally
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
Related videos on Youtube
thanos
Updated on July 30, 2022Comments
-
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 tomaster
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 thepull
). 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 thedev
branch.-
Scott Stensland about 3 yearsthis 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 almost 10 yearsWow...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 almost 9 yearsBest option for me was
git pull --rebase
. -
Spaideri about 7 yearsUsing force push (-f) flag is very dangerous and it should never be part of your regular work flow
-
Melebius about 6 yearsDownvoted since I’m missing some warning in this answer.
-
Melebius about 6 yearsWhy do you need to use
git fetch
andgit merge
again manually after runninggit pull
which contains them? -
Azarsa almost 6 yearsOoh! This force the repository to rewrite itself.
-
Marcelo almost 6 yearsI was doing
git pull origin develop
into my local develop branch, but now, just doinggit pull
it works fine to me, I dunno why. -
Himanshu almost 6 yearsbecause 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 over 4 yearsYeah this saved me since I didn't need anything in the existing repo, but definitely a scary command
-
simon over 4 yearsI 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 about 4 yearsThis should probably come with a warning.
-
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 about 4 yearsI'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 almost 4 yearsHi Eduardo! This worked for me. But can you explain why it works? What does this command do exactly?
-
eric almost 4 yearsRaise your hand if you did this and feel guilty but would do it again.
-
VishalParkash almost 4 yearsThis command helped me to escape the situation, but I will never use this command. ;) Thank you by the way. upvoted
-
Sabito 錆兎 stands with Ukraine over 3 years@Donal Please consider editing in a warning in this answer. git push -force is evil
-
Nico Haase over 3 yearsEven 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 over 3 yearsPlease explain further why adding a readme file should lead to the given error message
-
Nico Haase over 3 yearsEven 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 over 3 yearsthis 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 about 3 years
git pull <remote> master:dev
What isdev
here ? -
Ahnaaf Al Rafee about 3 years
git pull
helps -
Harlin about 3 yearsAgree 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 about 3 yearsI get an error: error: src refspec master does not match any error: failed to push some refs to
-
IgorGanapolsky about 3 yearsError: fatal: refusing to merge unrelated histories
-
Donovan P almost 3 yearsNothing 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 almost 3 yearsTo 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 over 2 yearsTrue but dangerous
-
Usama Tahir over 2 yearsit will remove all the old commits.
-
Liso over 2 yearsSad, 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 about 2 yearsAfter 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