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

Viewed 2657

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

2 Answers

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.

You can rename your current branch to main.

git branch -M main

After that you can push code using new branch name main

git push -u origin main
Related