17
  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.

2
  • This could happen because of a typo. Technically the error means the repo is empty or no commit is made. Oct 30, 2020 at 2:06
  • 1
    Your new repository with one new commit created one new branch name. Why do you believe that this new branch name is main? The default new branch name is actually master. You must take some particular action to change this. (A future Git release might have a different initial default name, but for now, it remains master.)
    – torek
    Oct 30, 2020 at 2:11

3 Answers 3

31

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

8
  • 1
    That's exactly what it was. I repeated the process all over again but this time I used git push -u origin master and it worked. Thank you! Oct 30, 2020 at 2:28
  • @IamWarmduscher Ugh, do you have to do this every time?? Dec 7, 2020 at 18:42
  • 1
    @KalebCoberly You don't. Just this one time when you're connecting your local repo to the origin.
    – Petr Hejda
    Dec 8, 2020 at 10:53
  • @IamWarmduscher, haha, thanks. That's what I meant. I guess it's too late now for GitHub to git with Git on this and save us all the trouble. Dec 8, 2020 at 23:33
  • When I run git checkout -B main and then git push -u origin main I get the error: failed to push some refs and Updates were rejected because the remote contains work that you do hint: not have locally Dec 16, 2020 at 3:31
2

I solved that problem using

git branch -M master main

There, you only rename the branch. That solved the problem you wrote

I learn about it with Git official documentation https://git-scm.com/docs/git-branch

1

Hello every one, first change to main branch :

git checkout main

After git merge to master :

git merge master

And final git push :

git push

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.