avatarTeri Radichel

Summarize

error: src refspec master does not match any.

Similar to a prior git error when trying to push files but not exactly

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

⚙️ Check out my series on Automating Cybersecurity Metrics | Code.

🔒 Related Stories: Bugs | AWS Security | CloudFormation

💻 Free Content on Jobs in Cybersecurity | ✉️ Sign up for the Email List

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

At first I thought this error was the same one I got in this blog, but it’s not.

I created a new repository using a script I wrote here:

I added a .gitignore file and tried to push it and got this error:

The current branch master has no upstream branch. To push the current branch and set the remote as upstream, use

git push --set-upstream origin master

Then I ran the suggested command and got this error:

error: src refspec master does not match any.

I got a clue to pull from this page — I needed to pull the code:

But when I pulled the code I still got another error.

What fixed it was when I ran a pull from main and then I ran the command recommended by git and then it worked. Like this:

git pull origin master
git push -set-upstream origin master

Well that worked when I navigated to my repository and ran the commands manually but when I automated the deployment it failed because I was tyring to create a new file before fixing the alignment to origin. This worked:

I did a pull to get the readme file before adding the new file and I got aligned with the correct origin. Then I create add, and push the file in the normal way. That way I can avoid rebase, merge, etc.

Update next day: Spoke to soon.

This is so frustrating. Git behavior is oddly incosistent. I deleted my local repository and my github repository. I redeployed it with the exact same code, credentials and everything. Today I get this:

Why do I need a global? Why won’t it work for this repository?

The interesting thing is that I’m using the GitHub cli for part of the code and git for the other. It’s asking me for credentials twice because GitHub CLI uses that auth login option I wrote about here:

The other code is just using regular git credentials which I have configured as cached:

So I thought maybe I could just use the GH CLI, but no. There’s no commit, push, or pull. But there’s a sync. Maybe that will work?

Nope. It just fails and kills my script prior to even attempting to create or push the .gitignore file as I am doing here:

I tried adding the force option to do a hard reset. Nope. Fails. No error. Swallowing errors is bad!

Now what?!

Ok first of all, I realized that I had inadvertently deleted the line of code where I change to the repository directory. I added that back in but it still failed.

I thought mabye with gh repo sync I could skip these two lines.

Nope. I put them back in and it worked.

Note that between each run, I deleted the repository directory and the respository on GitHub so I could start from scratch.

Proof that it worked from start to finish. I hope this still works tomorrow.

Code:

The one annoying thing is that I have to login twice because the CLI and git itself use different mechanisms for authentication. I hope that the GitHub CLI will add the cache mechanism used by git itself soon and this will work if I only enter the credentials once. #githubwishlist

But wait! There’s more! Let’s check the GitHub repo to make sure it’s right and…no .gitnore file. I verified it is in my local directory. I did a git push and pull. That worked fine. I found the problem.

In GitHub two branches exist. This was not my intention. I was just following the directions in the error message and some examples I found. For now, I don’t want the complexity of a new branch.

I’m still wasting time on this right now… I have tried a number of options including this:

git branch --set-upstream-to origin/master

And this:

git config --global --add --bool push.autoSetupRemote true

And this:

git config --global  pull.ff true

And so far none of it works. I also got more swallowed errors on different commands, which is not nice at all.

I ran this:

git ls-remote origin

And I can see that a branch named “main” exists:

xxxxxxxxxxxxxxxx HEAD
xxxxxxxxxxxxxxxx refs/heads/main

That makes sense. In GitHub I see this:

Maybe I typed master instead of main by mistake. I only want to reference main. Shouldn’t that work?

So I set these two lines to “main” and it works without error.

Except that I can see it’s still creating a master branch??

Seriously?

So I think what happened is when GitHub decided to go PC and change master to main (which I’m fine with except or this bug) they missed some code along the way that still references master. Argh.

I’m changing this to a BUG THAT BITES.

So how do I fix this……………..

I must have to force some code somewhere to use main instead, and I need to get main to be the default.

Finally figured a few thigns out. This documentation recommends using fetch. I have no idea if that is required and I’m not going to go back and test more things right now since it’s working — and this is not what I need to be doing right now!

The other thing I read was to add -u to the set-upstread command. I also added a command to set the branch upstream. Not ure if that is all necessary.

  git push -u --set-upstream origin main
  git branch --set-upstream-to origin/main

The main thing I needed to do… which I have not found recommended in ANY documentation ANYWHERE, is simply checkout the main branch on inital creation. I’m wondering if this step alone would have solved my whole problem.

git checkout main

Git documentation could really be clearer, and I think the fact that it uses master by default is a remnant of past code and a bug.

This code ran without error at some point.

But no. I deleted the repository and directory and ran the code again and go this error:

change dir GitHub
Enter github access token:
✓ Created repository xxxx on GitHubxxxxx
✓ Synced the "main" branch from xxxxxx to local repository
Username for 'https://github.com': xxxxxx
Password for XXX: 
From https://github.com/xxxxxxx
 * branch            main       -> FETCH_HEAD
branch 'main' set up to track 'origin/main'.
Everything up-to-date
fatal: No commit on branch 'master' yet.

What.

FINALLLLLLLLLLYYY.

Something is defaulting to main the way I created this repository. At some point, GitHub changed master to main. In order to get this reference or whatever it is to main run:

git fetch --all

If you don’t have the all on there I think you don’t get one of the references you need. Fetch initially confused me because I thought it was getting the files in the repository. It’s getting references you need to switch branches and so on. The way I set up this directory it somehow did not inclue main, I believe, and needed to be added.

I also had to sync down the readme I added before adding and pushing to avoid mass confusion. Hence I checkout the repository. After fetching. Otherwise you have conflicting changes in the repository you don’t have in your local and git gripes and that one is a real pain to fix. I usually delete things locally and start over.

The other problem I had was that you cannot run a push before a commit. I kept seeing this command everywhere so I added it but got errors because I added it before the commit.

git push -u --set-upstream orign main

Umm. Is it not obvious and easy to provide a better error message in this case? Like, “Yo, commit first dude.” Some error about refspec or other nonsense doesn’t really help.

I read that command above is deprecated for the branch version but could not get it working.

Here’s the code that just worked after deleting the repository and the local directory. Here’s crossing fingers it still works tomorrow.

🤞

I should get paid way more for the time I put into this blog. There are bug bounties for security bugs but not for just plain bugs that waste a lot of people’s time. Unfortunate.

More about GitHub and Git security:

Follow for updates.

Teri Radichel | © 2nd Sight Lab 2023

About Teri Radichel:
~~~~~~~~~~~~~~~~~~~~
⭐️ Author: Cybersecurity Books
⭐️ Presentations: Presentations by Teri Radichel
⭐️ Recognition: SANS Award, AWS Security Hero, IANS Faculty
⭐️ Certifications: SANS ~ GSE 240
⭐️ Education: BA Business, Master of Software Engineering, Master of Infosec
⭐️ Company: Penetration Tests, Assessments, Phone Consulting ~ 2nd Sight Lab
Need Help With Cybersecurity, Cloud, or Application Security?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
🔒 Request a penetration test or security assessment
🔒 Schedule a consulting call
🔒 Cybersecurity Speaker for Presentation
Follow for more stories like this:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
❤️ Sign Up my Medium Email List
❤️ Twitter: @teriradichel
❤️ LinkedIn: https://www.linkedin.com/in/teriradichel
❤️ Mastodon: @teriradichel@infosec.exchange
❤️ Facebook: 2nd Sight Lab
❤️ YouTube: @2ndsightlab
Error Message
Git
Refspec
Master
Does Not Match Any
Recommended from ReadMedium