0

Every time I try to push to Github I get an error that says:

Dominics-Air:django dominic.$ git push -u origin master

To https://github.com/domtom1126/django.git

 ! [rejected]      master ->  master (fetch first)

error:  failed to push some refs to 'https://github.com/gomtom1126/django.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want tot first integrate the remote changes
hint: (g.h., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
Dominics-Air:django dominic.$

I have tried installing git on different paths and nothing works. I am using a MacBook Air running Mojave.

evolutionxbox
  • 3,932
  • 6
  • 34
  • 51
  • Possible duplicate of [Issue pushing new code in Github](https://stackoverflow.com/questions/20939648/issue-pushing-new-code-in-github) – phd Dec 11 '18 at 08:58
  • https://stackoverflow.com/search?q=%5Bgit%5D+Updates+were+rejected+because+the+remote+contains+work+that+you+do+not+have – phd Dec 11 '18 at 08:58
  • You didn't read the error message. It gives you a likely cause of the problem and what you can do to resolve it. – evolutionxbox Dec 11 '18 at 09:38

2 Answers2

3

Run:

>git fetch origin
>git rebase origin/master
>git push origin master

Here, first step is to fetch the code from Remote.

Second step is to rebase your code with remote's master branch. In this step you might face conflicts, and you need to resolve them.

Third, push to master branch.

ruddra
  • 50,746
  • 7
  • 78
  • 101
  • If you are rebasing, why force push? It adds unnecessary risk and should not be used. – stellarhopper Dec 11 '18 at 06:49
  • well, as [history changes](https://git-scm.com/book/en/v2/Git-Tools-Rewriting-History) after rebasing, when you try to push to the remote, it will reject it, because history does not match between local and remote. that is why you need to make force push – ruddra Dec 11 '18 at 06:59
  • "well, as history changes after rebasing," only if it's not a fast-forward. With public/shared branches, I'd avoid forced pushes at all costs. – Frank Osterfeld Dec 11 '18 at 07:11
  • @ruddra a force push is only necessary because of the rebase, unlike a merge commit or fast-forward. – evolutionxbox Dec 11 '18 at 09:39
  • Yes you are right. My bad(I mixed it up with fast-forwarding). Updated the answer. – ruddra Dec 11 '18 at 09:49
0

So it turns out if you already have committed locally to git, you should not initialize with a README.md through Github, but make one your self through git. It is stated that you should not make a README if have an exiting local repository, but since I didn't read this was the error that would appear.