Apoleytaa
Apoleytaa

Reputation: 97

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.

Upvotes: 8

Views: 11776

Answers (4)

VonC
VonC

Reputation: 1324248

git branch -M is for renaming a branch.

If makes sense if you have a current master branch that you want to rename as main.

Make sure that your new local project has:

  • at least one commit (Nikolaos Kakouros mentions in the comments it is not required anymore, at least with Git 2.37+)
  • done on a master branch
  • pushed with git push -u origin master

Upvotes: 22

Long
Long

Reputation: 1793

The problem was because you did not have any commit. In other words, you must add some files using git add . and git commit -m "first commit" before trying to git branch -M main.

Upvotes: 0

M2Kishore
M2Kishore

Reputation: 3100

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

Upvotes: 2

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

Upvotes: 8

Related Questions