Idan
Idan

Reputation: 5735

"Updates were rejected because the remote contains work that you do not have locally." after creating a new repository on 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.

Upvotes: 274

Views: 463508

Answers (11)

palerdot
palerdot

Reputation: 7642

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 main 

// or optionally, 'git pull origin main --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 main

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.

Upvotes: 433

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 main

Where -f stands for "force".

Upvotes: 167

andrewdotn
andrewdotn

Reputation: 34803

This can also happen if you have a shallow clone. In that case it is technically always true that “the remote contains work that you do not have locally,” because you have a limited set of commits while the remote likely has the complete history. If the local repo only has a single new commit—which can happen on CI systems—this causes problems as there are no commits in common and git will be unable to push.

One solution can be to perform a less-shallow clone.

For Github Actions, you can set fetch-depth: 0 to pull “all history for all tags and branches.” For large repos you might want to tune that, e.g., by instead running git fetch --shallow-since=<date> after the shallow clone, but for small repos it should be fine.

steps:
  - uses: actions/checkout@v3
    with:
      fetch-depth: 0

Upvotes: 6

Lance Olana
Lance Olana

Reputation: 241

Simply run this commands:

git pull git add . git commit -m 'Your comment' git push

Upvotes: -2

blueblob26
blueblob26

Reputation: 102

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.

Upvotes: 3

Ashwini
Ashwini

Reputation: 851

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

Upvotes: 0

eQ19
eQ19

Reputation: 10691

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

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

Upvotes: 16

ptsivakumar
ptsivakumar

Reputation: 437

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

Upvotes: 0

Jakub Koralewski
Jakub Koralewski

Reputation: 693

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

Upvotes: 2

Darshan Jain
Darshan Jain

Reputation: 838

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.

Upvotes: 1

Mahyar
Mahyar

Reputation: 921

If this is your first push

just change the

git push <repo name> master

change it like this!

git push -f <repo name> master

Upvotes: 19

Related Questions