gitgithub

How to push code on my local machine to GitHub in the main branch


I have created a Repository as the default main branch.

Step I tried to add to the repository:

  1. git config --global init.befaultBranch main
  2. git init -b main
  3. git add .
  4. git commit -m "commit to main branch"
  5. git remote add origin 'my git url'

Now I have stuck to push to the origin

I have tried git push -u origin main but it gave me an error as:

! [rejected] main -> main (fetch first) error: failed to push some refs to 'my git url'

How to push the code from my local machine to GitHub in the main branch?

Any help and pointing to the documentation will be appreciated.

I tried to pull first and there is also an error on doing that:

$ git pull origin main

From my git url * branch
main -> FETCH_HEAD fatal: refusing to merge unrelated histories


Solution

  • That only happens because your remote repository was initialized not empty, but with some files (README.md for instance)

    That is why force pushing is a good option (in that case)

    git push -f -u origin main
    

    If you had created the repository empty, a regular push would have been enough.