0

I have been trying to push to a new git repository having not used git for a while. I am on a different computer to the one I normally use as I have changed jobs, and have never made a successful commit from this computer before. It is a windows 7 PC and I am using git bash.

I first set up my SSH key and copied the public key into my github settings. Next, I did git remote add origin <url>to link a newly created empty repository on my organisations github repo to a folder on my computer that already had the code in it that I wanted to commit. Then I did:


git init
git remote add origin [email protected]:myorg/myproject.git
git commit -m "First commit"
git add .
git push -u origin master

Everything worked fine until the push, which generated this error:

error: src refspec master does not match any
error: failed to push some refs to '[email protected]:myorg/myproject.git'

After searching here on StackOverflow I found several posts explaining that in 2020 due to concerns around the language used, Github had changed the name of its default repository from "master" to "main". Most of the responses suggested that just editing the push line to: git push origin main however when I tried this or variations on that theme (e.g. git push origin HEAD:main) etc. it just gave me the same error as above.

I then thought it might work better if I cloned the empty repository onto my computer (which worked fine, so I guess there isn´t a problem with my .SSH key) and then copied my files into it (without the .gitfolder in case that was messed up). However when I got to the push step it just generated the same error as above (when using either "main" or "master").

It seems that even though Github is now referring to the default branch as "main", Git bash is still referring to the local folder on my computer as "master" - hence the "does not match any" error?

The version of Git Bash I am using is 2.26.0.1 which was installed on 23 March 2020. Is it just that my version of git needs to be updated (I can´t test that this evening as I don´t have admin rights)?

Note that if I do git show-ref nothing comes up - not sure what that means since the other posts implied that this should result in the name of the branch I am using. I can see that Git Bash is referring to my local branch as "master" though in the header.

I would be grateful for any insights as to why git push origin main just does not seem to be working and what I can do to fix this.

1 Answer 1

1

You should first git add . and only then git commit. Now your commit fails as there is no staged files, leading to no initial commit:

git init
git remote add origin [email protected]:myorg/myproject.git
git add .
git commit -m "First commit"
git push -u origin master
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for that catch, it worked - but what is curious now is that Github is now calling my main branch "master", not "main". I´m just happy that I can push code to it, but curious all the same.
Just check if your local branch name matches with the one in push command. You can track different name on remote than your local one.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.