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

IamWarmduscher
  • 875
  • 2
  • 10
  • 27
  • This could happen because of a typo. Technically the error means the repo is empty or no commit is made. – Amin Gheibi Oct 30 '20 at 02: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 '20 at 02:11

3 Answers3

30

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

Petr Hejda
  • 40,554
  • 8
  • 72
  • 100
  • 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! – IamWarmduscher Oct 30 '20 at 02:28
  • @IamWarmduscher Ugh, do you have to do this every time?? – Kaleb Coberly Dec 07 '20 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 08 '20 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. – Kaleb Coberly Dec 08 '20 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` – Brian Wiley Dec 16 '20 at 03:31
  • @BrianWiley That's why you need to run it "**before**" (see my answer last line), not "**then**". Please doublecheck the question (with the original ordering) and the answer. – Petr Hejda Dec 16 '20 at 20:38
  • I'm confused I ran `git checkout -B main` before `git push -u origin main` – Brian Wiley Dec 16 '20 at 21:12
  • @BrianWiley In that case it's possible your remote branch is ahead of your local, which is a different issue. If it's the case, it's better explained in this post: https://stackoverflow.com/a/18328842/1693192 – Petr Hejda Dec 17 '20 at 01:09
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
Pirooz Jenabi
  • 440
  • 5
  • 7