0

I've created my project locally and initialized .git with branch "master" created by default. History:

> git init
> git add --all
> git commit -m "initial set up"
> git remote add origin https://github.com/me/APP-REPO.git
> git remote -v
> git push origin main
error: src refspec main does not match any
error: failed to push some refs to 
> git branch
* master
> git push -u origin main
error: src refspec main does not match any

But, repo created on Github has default branch "main". How can push directly to "main"? Why I can't see that branch locally?

Thank you in advance!

NatalieLes
  • 153
  • 9
  • 2
    In your local repository you don't have a branch called "main", only "master", as far as I understood. – mkrieger1 Jan 11 '21 at 13:11
  • 1
    To make it easier you could've begun from `git init -b main` – Alexey S. Larionov Jan 11 '21 at 13:11
  • 1
    Since it seems that you did not do any work in your local repository, it would be easier, instead of creating a new empty repository and linking it with the existing one, to just clone the existing one. – mkrieger1 Jan 11 '21 at 13:13
  • 1
    Or, since you now already have the local repository, simply rename the local branch from "master" to "main": `git branch -m master main`. – mkrieger1 Jan 11 '21 at 13:15
  • @mkrieger1 thank you! tried `git branch -m master main` and then pushed, but got an error: "! [rejected] main -> main (fetch first) error: failed to push some refs to 'https://github.com/me/todo-app.git' 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.". Should I pull from `main`? – NatalieLes Jan 11 '21 at 14:02
  • Oh sorry, then my last advice wasn't the best. Do the one before instead: discard your local repository (assuming it is still more or less empty) and clone the one from GitHub. – mkrieger1 Jan 11 '21 at 14:19
  • Your second line: `git add --all` implies that you have changes on your machine that you want to add to the new repo you are creating. However, the repo you are connecting to also appears to have changes in it already. Do you intend to: 1) Create a new repo from the changes on your machine, 2) start with the changes already in the existing repo (then just clone it), 3) start with the changes already in the existing repo and add the changes on your machine to that. (Then save off your changes somewhere else, clone the repo, then drop your changes back in, commit, and push.) – TTT Jan 11 '21 at 15:59
  • thank you, I have the project on my local machine and just trying to push to Github. I've already deleted .git and old repo on github and trying to create everything from scratch. As you said, but... `git init -b main` gives error: unknown switch 'b' – NatalieLes Jan 11 '21 at 16:58
  • @NatalieLes Check your version: `git --version`. I think you need 2.28 or higher to use -b. – TTT Jan 11 '21 at 17:33
  • Yes, the `-b` option is pretty new. There are plenty of ways to work without it though. See [my long answer here](https://stackoverflow.com/a/65174592/1256452) to a related question. – torek Jan 11 '21 at 18:55

1 Answers1

2

Your first error occured here:

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

because you have not yet modified your branch name master to main with git branch -M main.

So, running git push -u origin master would've worked, but it would create a branch name called master in the remote repo. But then things can get a little annoying if you want your master to be default, so you should git branch -M main first if possible.

And reading the comment section, you seemed to have changed your branch name master to main and encountered another error when you ran git push -u origin main.

It seems to me that you had initialized a remote repository with README or some other stuff in it. So, normally you can do git pull --rebase <your-remote-repo-url> to fetch your remote change and rebase it to your local repo.

After doing all that, you should be able to git push -u origin main.

But if you are starting all over, you can simply start a new repo on GitHub without any initial files, copy the url, git branch -M main, git remote add origin <remote-url>, and git push -u origin main.

syohey
  • 156
  • 1
  • 1
  • 6