<div><noscript><img fifu-featured=" title="
Solved: "error: failed to push some refs to remote"
"/>

Solved: “error: failed to push some refs to remote”

In this article, we will see how to solve "error: failed to push some refs to remote" if you are also getting this error. Last night when I was trying to push my release branch changes to the remote master, I noticed that it was failing with the "error: failed to push some refs to remote".  While I wasn’t expecting this error but this is very common error that can occur due to few reasons. So you need to check the error and then try to fix it accordingly. Here I will explain you the most common reason for this error which even I have encountered and how to solve it in very simple steps.

Solved: "error: failed to push some refs to remote"

Solved: “error: failed to push some refs to remote”

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

To give you insight about my local branches, I am currently having two branches – develop and release-2.0.1 as you can see below. After adding all the changes to my release-2.0.1 branch, I am trying to push it to the remote master.

cyberithub@ubuntu:~$ git branch
  develop 
* release-2.0.1

From the release-2.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 any reasons but for me it occurred because I was pushing the changes to master branch from a source branch which did not had any reference to the remote master. 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'

Verify the current branch by using git branch command as shown below.

cyberithub@ubuntu:~$ git branch
  develop
  release-2.0.1
* master

Then I had to check for any 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-2.0.1 branch using git pull origin release-2.0.1 command as shown below.

cyberithub@ubuntu:~$ git pull origin release-2.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-2.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 changes to local master, I again tried to push these 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 worked successfully. So this is how I solved my error. But it may be possible that same solution does not work for you. There could be a chance that you are still getting "error: failed to push some refs to remote" 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. Check more about git push command.

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: failed to push some refs to remote" too. Please let me know your feedback on the comment box.

Cyber IT Hub