17

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.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
naveen
  • 223
  • 1
  • 2
  • 11
  • https://stackoverflow.com/search?q=%5Bgit%5D+error%3A+src+refspec+main+does+not+match+any – phd Dec 01 '21 at 12:19

4 Answers4

17

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
1615903
  • 32,635
  • 12
  • 70
  • 99
  • after doing so it's showing this error. >To pathToMyRepo ! [rejected] main -> main (fetch first) error: failed to push some refs to 'pathToMyRepo' hint: Updates were rejected because the remote contains work that you do hint: not have locally. This is usually caused by another repository pushing hint: to the same ref. You may want to first integrate the remote changes hint: (e.g., 'git pull ...') before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details. – naveen Dec 01 '21 at 08:06
  • Well, did you do what the error message suggests? – 1615903 Dec 01 '21 at 09:52
  • I tried git pull. then it said "There is no tracking information for the current branch. Please specify which branch you want to rebase against." then I tried >git pull origin main, then It said "fatal: refusing to merge unrelated histories". Then I did "git pull origin main --allow-unrelated-histories" and this solved it. Then I did 'git push -u origin main and that's it, I can use it like normal now. I really need to focus on errors rather than just making it work so next time I won't have issues resolving them. But thanks, I'll select this as the answer to help others – naveen Dec 01 '21 at 12:05
12

just make sure you have SOMTHING in your folder to commit

  1. git init
  2. git remote add origin 'pathToYourRepo'
  3. git add .
  4. git commit -m "first commit"
  5. git push -u origin main
Sky
  • 291
  • 4
  • 5
9

In my case I just had forgot to commit a first time

The solution was to git commit -am "initial commit"

I wish the error message were more explicit

vinyll
  • 11,017
  • 2
  • 48
  • 37
2

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.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250