0

I just create a new repo on github, I set it with command git remote add origin htpps://XXX

And when I try to push ... with git push origin main ... I have an error :

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 integrate the remote changes (e.g., 'git pull ...') before pushing again. See the 'Note about fast-forwards' in 'git push --help' for details.

It is a new repo, doesn't make sense…
I try to push my first things in a new remote repo.

Obsidian
  • 3,719
  • 8
  • 17
  • 30

2 Answers2

-1

That error means that the repository already contains code.

To make sure your repository is the one you added, use:

git remote -v

If it's correct and you already have code you want to replace Github won't let you upload your code because there is already code and your commit would break the repository version line.

With --force you can ignore this warning and upload your code anyway.

git push origin main --force.

Juan Melnechuk
  • 461
  • 1
  • 8
  • 1
    Why is there already code if they just created a new repository on GitHub? – mkrieger1 Feb 20 '23 at 21:47
  • 1
    If there's already code here, `--force` will **overwrite** this code. Don't use it unless you really know what's happening there. – Obsidian Feb 20 '23 at 21:48
  • Exactly. You can check if your repo is correct using git remote -v as described in the comment. Then if it's correct you can use --force. – Juan Melnechuk Feb 20 '23 at 21:53
  • As the error message suggests, `git pull --rebase` first and check for conflicts. Resolve the conflicts by accepting coming or local and force push with `--force` to **overwrite** the entire git history. – Talkhak1313 Feb 21 '23 at 00:39
-1

If you are confident that there are no changes in remote branch that you need you can do git push -f to push your local changes to remote. But do it only if it's your repo and no-one else is working on that repo cause force push will delete the remote changes. There is a possibility that when you initialized the repo a README option was selected and your local branch may not have that since you did not pull. If you don't care about that git push -f will do it.

Marcia Moss
  • 189
  • 5