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.
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.
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.
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.
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.
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".
If this is your first push
just change the
git push <repo name> master
change it like this!
git push -f <repo name> master
You may refer to: How to deal with "refusing to merge unrelated histories" error:
$ git pull --allow-unrelated-histories
$ git push -f origin master
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'.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With