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

Github

Github Problem Overview


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.

Github Solutions


Solution 1 - Github

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.

Solution 2 - Github

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 commit".

Solution 3 - Github

If this is your first push

just change the

git push <repo name> master

change it like this!

git push -f <repo name> master

Solution 4 - 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

Solution 5 - Github

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'.

Solution 6 - Github

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.

Solution 7 - Github

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.

Solution 8 - Github

If you are using Visual S2019, Create a new local branch as shown in following, and then push the changes to the repo.

VS2019 local branch

Solution 9 - Github

The issue is because the local is not up-to-date with the master branch that is why we are supposed to pull the code before pushing it to the git

git add .
git commit -m 'Comments to be added'
git pull origin master
git push origin master

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionIdanView Question on Stackoverflow
Solution 1 - GithubpalerdotView Answer on Stackoverflow
Solution 2 - GithubKartikey Kumar SrivastavaView Answer on Stackoverflow
Solution 3 - GithubMahyarView Answer on Stackoverflow
Solution 4 - GithubeQ19View Answer on Stackoverflow
Solution 5 - GithubkoralView Answer on Stackoverflow
Solution 6 - Githubblueblob26View Answer on Stackoverflow
Solution 7 - GithubDarshan JainView Answer on Stackoverflow
Solution 8 - GithubptsivakumarView Answer on Stackoverflow
Solution 9 - GithubAshwiniView Answer on Stackoverflow