264

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.

pkamb
  • 33,281
  • 23
  • 160
  • 191
Idan
  • 5,365
  • 5
  • 24
  • 28

11 Answers11

421

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.

palerdot
  • 7,416
  • 5
  • 41
  • 47
162

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

Ryan M
  • 18,333
  • 31
  • 67
  • 74
  • 25
    'git push -f origin master' -this helps – Saurabh Jul 03 '17 at 15:31
  • 8
    git push -f overwrites the remote history with your local history, be careful when using it. Especially on public repositories. – Andre Oct 24 '17 at 10:58
  • 2
    `updates-were-rejected` also happens if you made changes in github repo in the remote, for example: made some changes in readme file using github gui. And then tried to push your new work to github, it will show this message saying that the changes you made in remote but it is not present locally. – Deke Apr 28 '18 at 00:31
19

If this is your first push

just change the

git push <repo name> master

change it like this!

git push -f <repo name> master
Mahyar
  • 901
  • 9
  • 13
  • 1
    This discards files like initially created the in the origin. Rather use a `git pull` before to get remote files and merge your commit then. Just like the accepted answer states – Yuna Jul 16 '18 at 08:16
  • @jayjaybricksoft Thank you for your comment. It is the first push, so replacing the origin files is Ok. – Mahyar Jul 17 '18 at 08:47
16

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

$ git pull --allow-unrelated-histories
$ git push -f origin master
pkamb
  • 33,281
  • 23
  • 160
  • 191
eQ19
  • 9,880
  • 3
  • 65
  • 77
4

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
andrewdotn
  • 32,721
  • 10
  • 101
  • 130
3

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.

blueblob26
  • 75
  • 9
2

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'.
koral
  • 525
  • 7
  • 23
1

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.

Darshan Jain
  • 781
  • 9
  • 19
0

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

pkamb
  • 33,281
  • 23
  • 160
  • 191
ptsivakumar
  • 437
  • 6
  • 4
0

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
Ashwini
  • 771
  • 10
  • 5
-1

Simply run this commands:

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

Lance Olana
  • 154
  • 1
  • 4