Git src refspec master does not match any Solution

Last Updated On Thursday 11th Aug 2022

Before you can push your changes from a remote Git repository, you need to add a file as a commit. You may get the src refspec master does not match any error if you create a new repository but forget to add a file.

This guide will explain what the error is and why it was raised. This guide will show you how to fix this error on your computer.

src refspec master does not match any

The repository does not have a commit history when it is first created. You must first commit if you wish to push a change into a Git repository.

This is the workflow to push a change into a repository:

Modify a file , Move the file to the staging area and Creating a commit

Once you’ve created a commit you can push it on a remote server. If you fail to follow the third step, Git will throw an error when you try to push your code on a remote server. Git won’t know what changes are needed to the remote repository.

We’re going to create a Git repository for a new HTML project. To start, let’s create the directory structure for our project:

	mkdir cj
cd cj
	
	git init
	

After creating a directory called cj, we moved into it. Once we have the folder created, we can start a Git repository.

This command creates hidden folder .git that contains configuration data for our repository. Next, create your first project file. This file will be called index.html. Then, we’ll add the following contents.

	<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    Hello World
</body>
</html>
	

We have a index.html file in our repository.

Our remote repository is hosted at GitHub. This will allow us to keep track of the project via the GitHub platform. We must create a remote reference to the GitHub repository in order to connect our local repository with the GitHub repository.

	git remote add origin https://github.com/codejagd/cj
	

This command will tell Git where your commits should be when you push them to a remote repository. We can now add the modified file to our project.

	git add
	

The staging area now contains our index.html file. We can now display the file in our remote repository by pushing it to the origin repository.

	git push -u origin master
	
	error: src refspec master does not match any
	

An error is returned.

Solution

The git add command doesn’t create a commit. The git add command moves files to the staging space. The staging area can be removed and added to whenever you like.

This error is common when you attempt to push changes into a Git ref prior to creating your first commit to your remote repository or local repository. Create an initial commit before you push your code to the remote repository.

	git commit \-m "feat: Init !! Woohoo"
	

This will create a record at the current time of the repository, which will reflect all changes made to the staging area. Let’s now push our code. The code has been successfully pushed to the remote repository.

To solve this error, create a commit using the git commit command and then try to push your changes to the remote repository. Now you have the knowledge you need to fix this error like a professional coder!

error: src refspec main does not match any
error: src refspec main does not match any
src refspec main does not match any
src refspec master does not match any
error src refspec master does not match any