Cyberithub

Solved: "error: src refspec master does not match any" when using git push

Advertisements

In this article, we will see how to solve "error: src refspec master does not match any" if you are also getting this error during git push operation. Last night when I was trying to push my release branch changes to the master, I noticed that it was failing with the "error: src refspec master does not match any".  While I  wasn't expecting this error to come but this is something which can occur at any time if you miss something. So at this time I decided to write an article about this so that it will help you guys also in case you are also getting this error.

But before proceeding towards the solution, let me tell you this error can be solved in multiple ways depending on your scenario and the branching strategy that you follow. Here I will explain you the best method which you can use even in a production or in a critical system with full confidence. This is fully tested and generally works fine in all Git based source control including Bitbucket.

Solved: "error: src refspec master does not match any" when using git push

Solved: "error: src refspec master does not match any" when using git push

Also Read: How to Create and Work on your Own Bitbucket Feature Branch

To explain the scenario in my system, I am currently having two local branches - develop and release/1.0.1 as you can see below. Here after adding all the changes to my release/1.0.1 branch, I am trying to push it to the remote master.

cyberithub@ubuntu:~$ git branch
  develop 
* release/1.0.1

From the release/1.0.1 branch, I tried to push the changes using git push -u origin master command then suddenly I noticed that it is failing with below error.

cyberithub@ubuntu:~$ git push -u origin master
error: src refspec master does not match any
error: failed to push some refs to 'https://app.cyberithub.com/bitbucket/scm/application/example-app.git'

While the above error could occur due to many reasons but for me it occurs because I was pushing the changes to master branch from a source branch which did not had any reference to the remote master branch. So to fix the above error, first I had to create and switch to new local master branch using git checkout -b master command as shown below.

cyberithub@ubuntu:~$ git checkout -b master
Switched to a new branch 'master'

You can verify the current branch by using git branch command as shown below.

cyberithub@ubuntu:~$ git branch
  develop
  release/1.0.1
* master

Here first I needed to pull all the changes from remote master branch using git pull origin master command as shown below.

cyberithub@ubuntu:~$ git pull origin master
warning: Pulling without specifying how to reconcile divergent branches is
discouraged. You can squelch this message by running one of the following
commands sometime before your next pull:

git config pull.rebase false # merge (the default strategy)
git config pull.rebase true # rebase
git config pull.ff only # fast-forward only

You can replace "git config" with "git config --global" to set a default
preference for all repositories. You can also pass --rebase, --no-rebase,
or --ff-only on the command line to override the configured default per
invocation.

From https://app.cyberithub.com/bitbucket/scm/application/example-app.git
* branch          master     -> FETCH_HEAD
Already up to date!
Merge made by the 'recursive' strategy.

Once the branch is in sync with remote master, I pulled all the files from my release branch using git pull origin release/1.0.1 command as shown below.

cyberithub@ubuntu:~$ git pull origin release/1.0.1
warning: Pulling without specifying how to reconcile divergent branches is
discouraged. You can squelch this message by running one of the following
commands sometime before your next pull:

git config pull.rebase false # merge (the default strategy)
git config pull.rebase true # rebase
git config pull.ff only # fast-forward only

You can replace "git config" with "git config --global" to set a default
preference for all repositories. You can also pass --rebase, --no-rebase,
or --ff-only on the command line to override the configured default per
invocation.

From https://app.cyberithub.com/bitbucket/scm/application/example-app.git
* branch           release/1.0.1 -> FETCH_HEAD
Merge made by the 'recursive' strategy.
 config/test.yaml                     | 2 +-
 config/hello.yaml                    | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)

After merging all the release changes to master, I again tried to push the changes to remote master branch by using git push -u origin master command as shown below.

cyberithub@ubuntu:~$ git push -u origin master
Enumerating objects: 16, done.
Counting objects: 100% (16/16), done.
Compressing objects: 100% (1/1), done.
Writing objects: 100% (2/2), 565 bytes | 565.00 KiB/s, done.
Total 2 (delta 1), reused 0(delta 0), pack-reused 0
remote: Checking connectivity: 2, done.
remote:
remote: Create pull request for master:
remote:   https://app.cyberithub.com/bitbucket/scm/projects/EXAMPLE/repos/example-app/pull-requests?create&sourceBranch=refs/heads/master
remote: 
To https://app.cyberithub.com/bitbucket/scm/application/example-app.git
   768ca03..9acb8ca  master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

As you can see from the above output, this time git push to remote master branch worked successfully. So this is how I solved my error. But this is not always the case with everyone. You might be getting "error: src refspec master does not match any" due to some other reasons like you might not have done the initial commit inside the repository and without that you are trying to push the changes. In that case also you will get the same error. So to fix this kind of error you need to first do the initial commit in your local repo and then only push the changes using git push -u origin master command as shown below.

touch somefile
git add somefile
git commit -m "Initial Commit"
git push -u origin master

Hopefully the above solution should be enough to solve your "error: src refspec master does not match any" too. Please let me know your feedback on the comment box.

Leave a Comment