Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

github

I created a new repo, cloned it, added files to the directory, added them with add -A, committed changes, and when I try to push using git push <repo name> master I get:

hint: Updates were rejected because the remote contains work that you do not have locally. This is usually caused by another repository pushing to the same ref. You may want to first merge the remote changes (e.g., hint: 'git pull') before pushing again.

This doesn't seem to make sense since it's a new repo and contains only a readme file.

like image 436
Idan Avatar asked Aug 20 '13 06:08

Idan


People also ask

How do you fix updates were rejected because the tag already exists in the remote?

Right-click the tag and choose to delete it (be sure to uncheck the Remove tag from all remotes checkbox). Choose the Fetch option (Fetch and store all tags locally does not have to be enabled). You should now have that tag that was just deleted back, and attempting to Push will no longer show that error message.

Were rejected because the remote contains work that you do hint not have locally this is usually caused by another repository pushing?

hint: Updates were rejected because the remote contains work that you do not have locally. This is usually caused by another repository pushing to the same ref. You may want to first merge the remote changes (e.g., hint: 'git pull') before pushing again.

How do you fix updates were rejected because a pushed branch tip is behind its remote?

Integrate the remote changes (e.g. hint: 'git pull ...') before pushing again. See the 'Note about fast-forwards' in 'git push --help' for details. Updates were rejected because the tip of your current branch is behind its remote counterpart. Integrate the remote changes (e.g. 'git pull ...') before pushing again.


7 Answers

This happens if you initialized a new github repo with README and/or LICENSE file

git remote add origin [//your github url]

//pull those changes

git pull origin master 

// or optionally, 'git pull origin master --allow-unrelated-histories' if you have initialized repo in github and also committed locally

//now, push your work to your new repo

git push origin master

Now you will be able to push your repository to github. Basically, you have to merge those new initialized files with your work. git pull fetches and merges for you. You can also fetch and merge if that suits you.

like image 140
palerdot Avatar answered Sep 23 '22 09:09

palerdot


The error possibly comes because of the different structure of the code that you are committing and that present on GitHub. It creates conflicts which can be solved by

git pull

Merge conflicts resolving:

git push

If you confirm that your new code is all fine you can use:

git push -f origin master

Where -f stands for "force".

like image 31
Kartikey Kumar Srivastava Avatar answered Sep 24 '22 09:09

Kartikey Kumar Srivastava


If this is your first push

just change the

git push <repo name> master

change it like this!

git push -f <repo name> master
like image 28
Mahyar Avatar answered Sep 24 '22 09:09

Mahyar


You may refer to: How to deal with "refusing to merge unrelated histories" error:

$ git pull --allow-unrelated-histories
$ git push -f origin master
like image 41
Chetabahana Avatar answered Sep 25 '22 09:09

Chetabahana


The supplied answers didn't work for me.

I had an empty repo on GitHub with only the LICENSE file and a single commit locally. What worked was:

$ git fetch
$ git merge --allow-unrelated-histories
Merge made by the 'recursive' strategy.
 LICENSE | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
 create mode 100644 LICENSE

Also before merge you may want to:

$ git branch --set-upstream-to origin/master
Branch 'master' set up to track remote branch 'master' from 'origin'.
like image 35
koral Avatar answered Sep 23 '22 09:09

koral


I manually edited a couple files directly in the repo after I had pushed the last commit and got the same error as a result.

Instead of git push origin master use git push -f origin master to force an update.

Would only recommend doing this though if you know exactly what was changed in the repo and you're 100% confident you want the local commit to replace everything. Mine was a hobby project that no one else is working on and no other commits had been made from any other devices aside from the manual changes.

like image 36
blueblob26 Avatar answered Sep 24 '22 09:09

blueblob26


I followed these steps:

Pull the master:

git pull origin master

This will sync your local repo with the Github repo. Add your new file and then:

git add .

Commit the changes:

git commit -m "adding new file  Xyz"

Finally, push the origin master:

git push origin master

Refresh your Github repo, you will see the newly added files.

like image 45
Darshan Jain Avatar answered Sep 23 '22 09:09

Darshan Jain