'github how to fix error: refname refs/heads/new_branch not found

I'm really new to GitHub.

I made my first project and want to add it to GitHub. But when I try to add my project to GitHub I'm getting the following error and I couldn't find a way to fix it.

git branch -M main

error: refname refs/heads/new_branch not found

fatal: Branch rename failed

Any help will be appreciated.



Solution 1:[1]

I solved this thanks to this guide,

Apparently and I do not know why the steps given by the Github guide are not in the correct order and if I am wrong that someone please explain to me.

I solved it with these steps:

    git init
    git remote add origin [email protected]:<YOUR_REPOSITORY_ADDRESS>
    git add .
    git commit -m "first commit" 
    git branch -M main
    git push -u origin main

steps that the guithub guide gives that for me was a mistake.

echo "# socialdata" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin https://github.com/<YOUR_REPOSITORY_ADDRESS>
git push -u origin main

Solution 2:[2]

add the files in the repository to track

git add .

commit at least once before pushing it to the origin

git commit -m "<your commit message>"

now try renaming the branch

git branch -M main

you can further add origin if not already added

git remote add origin https://github.com/<YOUR_REPOSITORY_ADDRESS>

and push it to github

git push -u origin main

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 JORGE IVAN GUEVARA PEREZ
Solution 2 M2Kishore