How to fix git error:src refspec origin does not match any


This post talks about how to fix an git error.

error: src refspec master does not match any error: failed to push some refs to ‘url.git’

Possible causes for this error for below usercases

  • Create and commit changes to remote repository
  • Push existing repository from command line

Here are steps and commands for create and clone and commit repository

error: src refspec master does not match any error: failed to push some refs to ‘url.git’

There are two usecases success and error flow

Error Flow

Let’s see how we can reproduce this error.

These are not correct way of add commit and pushing changes, but given an example use case how we can reproduce this error.

  • I have a local project created angular-crud-mock-api in b:\\githubwork directory.

  • Create an empty repository in github.com my repository url.

https://github.com/intkiran/angular-mock-api-json.git
  • Now want to commit angular-curd-mock-api code changes into Repository url existing local application created angular-crud-mock-api which is not sync with github repository.
  • Let’s see how to add this changes to remote repository
:\githubwork\angular-crud-mock-api>git remote add origin https://github.com/intkiran/angular-mock-api-json.git
  • Issue git push command and throws an error
B:\githubwork\angular-crud-mock-api>git push -u origin master
error: src refspec master does not match any
error: failed to push some refs to 'https://github.com/intkiran/angular-mock-api-json.git'

That means, we have to add first files or directory before push changes

  • Add the files and directories using below command git add .
B:\githubwork\angular-crud-mock-api>git add .
warning: LF will be replaced by CRLF in angular-crud-mock-api/.browserslistrc.
The file will have its original line endings in your working directory

This adds the changes to local repository in git.

Now, Let’s try to push changes to remote repository using git push command.

  • Next push changes using git push command
B:\githubwork\angular-crud-mock-api>git push -u origin master
error: src refspec master does not match any
error: failed to push some refs to 'https://github.com/intkiran/angular-mock-api-json.git'

This error throws an error and you need to use commit command before push command

  • commit changes Here is an commit changes to local repository.
B:\githubwork\angular-crud-mock-api>git commit -m "Initial changes"
[master (root-commit) 96c6c0c] Initial changes
 29 files changed, 30724 insertions(+)

Success Flow

Here are sequence of commands you need to run to avoid the error

git remote add origin https://github.com/intkiran/angular-mock-api-json.git
git add .
git commit -m "Initial changes" .
git push -u origin master

Here is the output of push changes

B:\githubwork\angular-crud-mock-api>git push -u origin master
Enumerating objects: 38, done.
Counting objects: 100% (38/38), done.
Delta compression using up to 4 threads
Compressing objects: 100% (34/34), done.
Writing objects: 100% (38/38), 260.09 KiB | 5.20 MiB/s, done.
Total 38 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), done.
To https://github.com/intkiran/angular-mock-api-json.git
 * [new branch]      master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

Solutions for this error

When you are creating new repo or push an existing repository, You have to carefully check below things to avoid this error

  • You forgot to add the files before pushing changes to master or branch

  • Missing or skipping git add . or git commit these command throws an error

Here are steps for adding the files or directories

git add .
git add  --all
  • git commit message enclosed in double quotes instead of single quotes

Valid

git commit -m "initial changes"

Invalid

git commit -m 'initial commit'
  • Please check branch name with push command

Usually we will push changes using below command

git push -U origin `branchame`

push command push changes from local repo to remote repository branchname Please make sure that branchname exists.

By default ‘master ' is default branch

Here is a correct command

git push -U origin master

Possible reasons

  • Branch name not found
  • Branch name spelling mistake or case insensitive

How to create a new repo to avoid this error

git init git add . git commit -m ‘message’ git push -u origin master

How to clone existing repo

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.