Home > Software design >  git: error: src refspec main does not match any
git: error: src refspec main does not match any

Time:12-01

I was trying to use git via VSCode, and because I didn't know how to use Git on VSCode, it messed up everything.
Then I tried to use CMD (Windows) as usual. I am learning, and I've never seen this kind of error before, so I don't know how to solve this.

But it's showing these errors when I try to push to the main branch:

error: src refspec main does not match any
error: failed to push some refs to 'myPathToRepo'

I tried:

  • I tried with another branch and it was working fine but I want to use the main branch
  • I even tried deleting the git folder and initiating it again

I've seen other posts on StackOverflow and mostly they say "first commit and then push because this error arises when you're pushing without committing".

After deleting the .git/ folder and restarting, it starts to show this same error

>git push
fatal: The current branch master has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin master

>git push -u origin main
error: src refspec main does not match any
error: failed to push some refs to 'myPathToRepo'

This is my first commit:

[master (root-commit) 061a06e] first commit

PS. I only have 'main' branch.

Commands that I ran:

git init
git remote add origin 'pathToMyRepo'
git add -A
git commit -m "first commit"
git push
git push -u origin main

Please help.

CodePudding user response:

You don't have a branch called main. You have a branch called master.

Either push with the correct name:

git push -u origin master

Or change your local branch name to main and use that:

git branch -m main
git push -u origin main

CodePudding user response:

Make sure to launch VSCode after having set up:

git config --global init.defaultbranch main

That way, your default local branch created will be indeed main, and not master, when you initialize a new repository, and make your first commit with VSCode.

  • Related