Issue
I have created a Repository as the default main branch.
Step I tried to add to the repository:
- git config –global init.befaultBranch main
- git init -b main
- git add .
- git commit -m "commit to main branch"
- 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.
Answered By – VonC
This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0