src refspec xxx does not match any

2021-07-24

 | 

~2 min read

 | 

262 words

I ran into an interesting problem today when I bootstrapped a new project with TSDX.

The actual steps I took to create the problem seem pretty rare - which is likely why it’s the first time I’ve come across it, even after creating hundreds of git managed repos.

In this particular case, I wanted to immediately push my repo up to GitHub after initializing the project.

% git init
% git remote add origin git@github.com:stephencweiss/backlinks.git
git branch -M main
git push -u origin main
error: refname refs/heads/main not found
fatal: Branch rename failed
error: src refspec main does not match any
error: failed to push some refs to 'git@github.com:stephencweiss/backlinks.git'

The error message felt Greek to me, but it turns out what Git is telling me is that there are no commits, so there’s nothing to actually push.

The solution was simple:

  1. Create a commit
  2. Try again
% git commit
% git remote add origin git@github.com:stephencweiss/backlinks.git
git branch -M main
git push -u origin main
fatal: remote origin already exists.
Enumerating objects: 16, done.
Counting objects: 100% (16/16), done.
Delta compression using up to 16 threads
Compressing objects: 100% (12/12), done.
Writing objects: 100% (16/16), 145.17 KiB | 3.38 MiB/s, done.
Total 16 (delta 0), reused 0 (delta 0)
To github.com:stephencweiss/backlinks.git
 * [new branch]      main -> main
Branch 'main' set up to track remote branch 'main' from 'origin'.

Et Voilá! Sure, we have an error because it’s trying to set the origin when that succeeded on the first try, but overall, problem solved!



Hi there and thanks for reading! My name's Stephen. I live in Chicago with my wife, Kate, and dog, Finn. Want more? See about and get in touch!