Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error: src refspec main does not match any error: failed to push some refs to <url>

Tags:

git

github

  1. To get practice with Github, I created a new directory on my computer that I wanted to push to Github.
  2. I added a .csv file and nothing else.
  3. I created a new repo on Github without initializing a README.
  4. I cd'd into the directory then used the following commands in Terminal:

git init

git add file1.csv

git commit -m "First commit"

git remote add origin <Github url from Quick Setup page>

git push -u origin main

And I got the following errors:

error: src refspec main does not match any

error: failed to push some refs to <url>

I searched for a solution and I came across this: git error: failed to push some refs to remote The answer selected says:

If the GitHub repo has seen new commits pushed to it, while you were working locally, I would advise using:

git pull --rebase origin master

git push origin master

What I don't understand is, why did this happen with a new directory on my computer and a new repo? No commits were made to the repo on Github so why should I have to git pull? I even tried doing this with a new empty directory and new empty repo (again) and I got the same result.

like image 511
IamWarmduscher Avatar asked Oct 30 '20 01:10

IamWarmduscher


People also ask

How do you fix SRC Refspec main does not match any?

The solution to this error is to either create a local and remote master branch that you can push the commit to or to push the commit to an existing branch – maybe main . These commands will create a master branch locally. And by pushing to origin master , the master branch will also be created remotely.

How do I push master branch to remote repository?

Push a new Git branch to a remote repo Clone the remote Git repo locally. Create a new branch with the branch, switch or checkout commands. Perform a git push with the –set-upstream option to set the remote repo for the new branch. Continue to perform Git commits locally on the new branch.

What is the git push command?

The git push command is used to upload local repository content to a remote repository. Pushing is how you transfer commits from your local repository to a remote repo. It's the counterpart to git fetch , but whereas fetching imports commits to local branches, pushing exports commits to remote branches.

What is git push origin?

Git Push Origin pushes all the branches to the main branch. Git Push Origin Master pushes your master branch to the origin. Command for this: git push origin.


1 Answers

This is an unpleasant result of the master vs main controversy.

Your local GIT client created a default branch called master (when you initialized the repo with git init), but the remote repository on GitHub has no master - instead the default branch is called main.


Solution A - if you want to name the branch master

Run git push -u origin master instead of git push -u origin main

Or Solution B - if you want to name the branch main

Run git checkout -B main before git push -u origin main

like image 109
Petr Hejda Avatar answered Oct 20 '22 07:10

Petr Hejda