I have tried to follow the solutions suggested in this post but it didnt work and I am still getting: src refspec master does not match any.
Here is what I did:
Followed this solution
// adding the file I created
$ git add .
$ git commit -m 'initial commit'
$ git push origin master
error: src refspec master does not match any.
When doing:
$ git push origin HEAD:master
b40ffdf..a0d1423 HEAD -> master // looks promising
// adding a remote
$ git remote add devstage -f <another git>
$ git merge devstage/master -s recursive -X ours
$ git push -u devstage master
error: src refspec master does not match any.
More information:
$ git branch
* origin
$ git show-ref
refs/heads/origin
refs/remotes/devstage/master
refs/remotes/origin/HEAD
refs/remotes/origin/devstage
refs/remotes/origin/master
refs/remotes/origin/origin
So I am definitely missing refs/heads/master but dont know how to create it.
Thanks
asked Jan 21, 2014 at 17:13
special0nespecial0ne
6,04317 gold badges66 silver badges107 bronze badges
4
This should help you
git init
git add .
git commit -m 'Initial Commit'
git push -u origin master
answered Nov 26, 2015 at 5:42
3
From git branch it appears that somehow your local branch name is «origin».
You can rename the branch with -mv flag, like this:
git branch -mv origin master
After this git branch should show master 
Just to make sure the name is indeed the only thing that went astray, you can run git log and look at the last few commits — and compare them to the last few commits on bitbucket website.
answered Jan 21, 2014 at 19:29
apprenticeDevapprenticeDev
8,0293 gold badges22 silver badges25 bronze badges
5
Try to do :
git push origin HEAD:master
answered Nov 29, 2017 at 14:53
1
i have same problem, to solve it, follow these steps
git init
git add .
git commit -m 'message'
git push -u origin master
after this, if you still having that error, follow these steps again
git add .
git commit -m 'message'
git push -u origin master
that worked for me and Hope it will help anyone
answered Mar 4, 2017 at 16:51
![]()
2
By just adding an empty commit will fix issue by using
$ git commit -m "empty commit" --allow-empty
$ git push
above. make empty commit without edit then push
answered Jan 12, 2019 at 10:40
![]()
Bourbia BrahimBourbia Brahim
14.3k4 gold badges38 silver badges52 bronze badges
0
Try following command:
git push origin HEAD:master
Git threw the below error when I tried simply git push. So clearly this is because Git matches the local and remote branch while pushing commits. This is the push.default behavior, you can find out more details here.
fatal: The upstream branch of your current branch does not match
the name of your current branch. To push to the upstream branch
on the remote, use
git push origin HEAD:<Branch_Name>
To push to the branch of the same name on the remote, use
git push origin <Branch_Name>
To choose either option permanently, see push.default in 'git help config'.
answered Apr 10, 2018 at 7:03
![]()
SaikatSaikat
13.9k20 gold badges102 silver badges122 bronze badges
0
I was having the SAME ERROR AGAIN AND AGAIN.
I added files in local repository and Trying the command
«git push origin master»
Showed Same Error
ALL I WAS MISSING I DID NOT COMMIT .
» git commit -m ‘message’ «
After Runnig this it worked
answered Mar 2, 2018 at 17:28
![]()
2
The clue is in the error
error: src refspec master does not match any.
Github’s recently changed its default branch to main.
Take a look here
On your local setup you could rename your local branch as shown below
git branch -m master main
or you could push from your master to main
git push origin master:main
answered Oct 23, 2021 at 20:17
pcodexpcodex
1,79215 silver badges16 bronze badges
Run the command git show-ref, the result refs/heads/YOURBRANCHNAME
If your branch is not there, then you need to switch the branch by
git checkout -b "YOURBRANCHNAME"
git show-ref, will now show your branch reference.
Now you can do the operations on your branch.
answered Jul 6, 2017 at 7:08
SonuSonu
7029 silver badges7 bronze badges
0
In my case the error was caused because I was typing
git push origin master
while I was on the develop branch
try:
git push origin branchname
Hope this helps somebody
answered Jun 17, 2017 at 10:47
![]()
The error demo:
007@WIN10-711082301 MINGW64 /d/1 (dev)
$ git add --all
007@WIN10-711082301 MINGW64 /d/1 (dev)
$ git status
On branch dev
Initial commit
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: index.html
new file: photo.jpg
new file: style.css
007@WIN10-711082301 MINGW64 /d/1 (dev)
$ git push origin dev
error: src refspec dev does not match any.
error: failed to push some refs to 'git@github.com:yourRepo.git'
You maybe not to do $ git commit -m "discription".
Solution:
007@WIN10-711082301 MINGW64 /d/1 (dev)
$ git commit -m "discription"
[dev (root-commit) 0950617] discription
3 files changed, 148 insertions(+)
create mode 100644 index.html
create mode 100644 photo.jpg
create mode 100644 style.css
007@WIN10-711082301 MINGW64 /d/1 (dev)
$ git push origin dev
To git@github.com:Tom007Cheung/Rookie-s-Resume.git
! [rejected] dev -> dev (fetch first)
error: failed to push some refs to 'git@github.com:yourRepo.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
answered Dec 17, 2017 at 15:36
MaiMai
1353 silver badges15 bronze badges
1
This is happend to me once I forgot to add files. So I got the same error. All you need to do is add your files.
- Add your files =>
git add .or the name of the files you want to add. you supposed to init first your repo withgit init. - Commit your changes =>
git commit -m 'Initial Commit'. - Now push your changes =>
git push -u origin master
answered Feb 3, 2020 at 18:28
![]()
DINA TAKLITDINA TAKLIT
6,80110 gold badges69 silver badges73 bronze badges
this error occurs when you clone a repo from one branch and you trying to push changes to another branch just try to make sure that you are in the same branch compared to the branch that you are trying to push if it isnot the same just clone your repo again from that specific branch by using git clone -b <branchname> <remote-repo-url> then retry to push changes
answered Sep 12, 2021 at 12:38
![]()
Ensure that if you are pushing the master branch then ensure that you’re currently in the master branch if not checkout to the master branch and now push your commits. To list all current branches in your working directory use :
git branch
Your currently working branch should have an asterisk at the beginning for instance if am working on my a devstage branch it would appears as shown below :
*devstage
master
In this case, push the commits in the devstage branch first then perform a git pull request if you want to merge the two branches that is the master and the devstage.
answered May 31, 2021 at 20:02
stanley mbotestanley mbote
9001 gold badge7 silver badges17 bronze badges
Check that you call the git commands from the desired directory (where the files are placed).
answered Mar 19, 2017 at 12:42
NoamGNoamG
1379 bronze badges
This error can typically occur when you have a typo in the branch name.
For example you’re on the branch adminstration and you want to invoke:
git push origin administration.
Notice that you’re on the branch without second i letter: admin(i)stration, that’s why git prevents you from pushing to a different branch!
answered Oct 6, 2017 at 8:42
Setup username and password in the git config
In terminal, type
vi .git/config
edit url with
url = https://username:password@github.com/username/repo.git
type :wq to save
answered Oct 14, 2017 at 13:55
Prashanth SamsPrashanth Sams
19.3k20 gold badges101 silver badges125 bronze badges
Only because your local branch does not math the one in your remote repository.
git push origin HEAD:master
Enable you to ignore the conflict and upload your commit anyway.
answered Jan 19, 2018 at 1:44
![]()
0
I had the same problem recently. but now resolved this issue. Because, Now GitHub changed master to main. It works well for me. Use git push origin main instead of git push origin master. Hopefully, It will work.
answered Sep 21, 2021 at 15:15
![]()
Vintage CoderVintage Coder
4311 gold badge6 silver badges9 bronze badges
For me, the fix appears to be «git .» (stages all current files). Apparently this is required after a git init?
I followed it by «get reset» (unstages all files) and proceeded with the exact same commands to stage only a few files, which then pushed successfully.
git .
git reset
answered Jun 17, 2017 at 17:01
JohnP2JohnP2
1,88919 silver badges17 bronze badges
It happened to me and I discovered that github was trying to verify my account. So you need these 2 commands:
git config --global user.email <your github email>
git config --global user.name <your github username>
answered Nov 30, 2018 at 15:00
jessjess
237 bronze badges
FWIW, ran into same error, but believe it came about due to the following sequence of events:
- Remote Git repo was created with
masterbranch. - Local clone was then created.
- Remote Git repo was then modified to include a
devbranch, which was defined as the default branch, in conjunction with permissions added to themasterbranch preventing changes without a pull request. - Code updates occurred in the local clone, ready to be pushed to the remote repo.
Then, when attempting to push changes from the local to the remote, received error «src refspec master does not match any», or when attempting to push to dev, «src refspec dev does not match any».
Because changes were pending in the local clone, I did not want to blast it and refresh.
So, fixed the issue by renaming the local branch to dev…
$ git branch -m dev
…followed by the normal push of git push origin dev, which worked this time without throwing the aforementioned error.
answered Sep 4, 2019 at 18:37
![]()
TrentiumTrentium
3,3892 gold badges12 silver badges19 bronze badges
This error is also caused due to an unmatched local branch name.
Make sure that you are giving correct local branch name (check spelling and case sensitivity)
I had the same error because my local branch name was «validated» and was trying to push the changes using git push -f origin validate, updated that to git push -f origin validated worked.
Hope this helps.
answered May 2, 2020 at 15:43
RupeshRupesh
8301 gold badge12 silver badges26 bronze badges
I also faced the same error.
In my case below is the scenario.
I have master branch which set as origin.
Other side I have release branch «Release_branch».
I have to fork my feature branch(i.efeature/testBranch) from Release branch.
Below are the steps I did.
$ git checkout Release_branch
$ git pull
$ git checkout feature/testBranch
$ git commit -m "SOME_MESSAGE"
$ git push -u origin feature/testBranch
answered May 4, 2021 at 11:16
![]()
rahulnikharerahulnikhare
1,3621 gold badge18 silver badges25 bronze badges
I had this error (error: src refspec master does not match any) with a new repository, when trying git push origin master, because GitHub changed the default name of the master branch to main.
So, git push origin main is working for me.
answered May 15, 2021 at 10:46
![]()
For a new repository, the method works for me:
-
Remote the files related with git
rm -rf .git
-
Do the commit again
git add . && git commit -m "your commit"
-
Add the git URL and try to push again
git remote add origin <your git URL>
-
And then try to push again
git push -u origin master -f
-
Success!
Since it’s a new repository, so it doesn’t matter for me to remove the git and add it again.
answered Nov 24, 2018 at 22:41
![]()
backslash112backslash112
2,0751 gold badge23 silver badges29 bronze badges
I had already committed the changes and added all the files, had the same origin as remote, still kept getting that error. My simple solution to this was just:
git push
answered Sep 1, 2020 at 20:54
Vivek SinghVivek Singh
3263 silver badges14 bronze badges
I clone my repository with:
git clone ssh://xxxxx/xx.git
But after I change some files and add and commit them, I want to push them to the server:
git add xxx.php
git commit -m "TEST"
git push origin master
But the error I get back is:
error: src refspec master does not match any.
error: failed to push some refs to 'ssh://xxxxx.com/project.git'
![]()
aloisdg
22.1k6 gold badges84 silver badges102 bronze badges
asked Nov 15, 2010 at 6:09
18
Maybe you just need to commit. I ran into this when I did:
mkdir repo && cd repo
git init
git remote add origin /path/to/origin.git
git add .
Oops! Never committed!
git push -u origin master
error: src refspec master does not match any.
All I had to do was:
git commit -m "initial commit"
git push origin main
Success!
ChrisB
1,5006 silver badges20 bronze badges
answered Sep 27, 2011 at 16:07
baisongbaisong
56.6k1 gold badge14 silver badges7 bronze badges
30
- Try
git show-refto see what refs you have. Is there arefs/heads/master?
Due to the recent «Replacing master with main in GitHub» action, you may notice that there is a
refs/heads/main. As a result, the following command may change fromgit push origin HEAD:mastertogit push origin HEAD:main
- You can try
git push origin HEAD:masteras a more local-reference-independent solution. This explicitly states that you want to push the local refHEADto the remote refmaster(see the git-push refspec documentation).
answered Nov 15, 2010 at 11:24
Vi.Vi.
36.6k18 gold badges91 silver badges148 bronze badges
22
I also had a similar error after deleting all files on my local computer, and I have to clean up all files in the repository.
My error message was something like this:
error: src refspec master does not match any.
error: failed to push some refs to 'git@github ... .git'
And it was solved by executing the following commands:
touch README
git add README
git add (all other files)
git commit -m 'reinitialized files'
git push origin master --force # <- caution, --force can delete others work.
![]()
answered Jan 4, 2012 at 17:03
AryoAryo
4,3292 gold badges28 silver badges24 bronze badges
4
git push -u origin master
error: src refspec master does not match any.
For that you need to enter the commit message as follows and then push the code:
git commit -m "initial commit"
git push origin master
Successfully pushed to master.
![]()
answered Aug 9, 2017 at 9:22
![]()
VIKAS KOHLIVIKAS KOHLI
8,0744 gold badges50 silver badges60 bronze badges
2
For me I had to make sure the public key is properly configured on the server (appended in ~/.ssh/authorized_keys) and in GitHub/Bitbucket (added to my SSH keys on GitHub or Bitbucket) — they need to match. Then:
git add --all :/
git commit -am 'message'
git push -u origin master
grg
4,8343 gold badges32 silver badges49 bronze badges
answered Sep 2, 2014 at 1:56
pyforkpyfork
3,6972 gold badges20 silver badges18 bronze badges
0
This happened to me in a brand new repository after I ran git add with only an empty directory.
As soon as I added a file (e.g. a git add README.md), then git push worked great.
answered Sep 25, 2011 at 1:44
Andrew EAndrew E
7,6093 gold badges41 silver badges38 bronze badges
7
Missing or skipping git add . or git commit may cause this error:
git push -u origin master
Username for 'https://github.com': yourusername
Password for 'https://yourusername@github.com':
error: src refspec master does not match any.
error: failed to push some refs to 'https://github.com/yourusername/foobar.git'
To fix it, reinitialize and follow the proper sequence:
git init
git add .
git commit -m 'message'
git *create remote
git push -u origin master
![]()
answered Nov 3, 2012 at 20:30
aug2uagaug2uag
3,3693 gold badges30 silver badges53 bronze badges
4
To fix it, re-initialize and follow the proper code sequence:
git init
git add .
git commit -m 'message'
git push -u origin master
![]()
Werner
2,5371 gold badge26 silver badges38 bronze badges
answered Jan 12, 2015 at 17:30
![]()
0
This happens too when you are in a specific branch and try to push another branch that does not exist yet, like:
$ git branch
* version-x # you are in this branch
version-y
$ git push -u origin master
error: src refspec master does not match any.
error: failed to push some refs to 'origin_address'
tanius
13.5k3 gold badges51 silver badges62 bronze badges
answered May 23, 2012 at 17:43
wilsonfozwilsonfoz
8678 silver badges9 bronze badges
7
I faced the same problem, and I used --allow-empty:
$ git commit -m "initial commit" --allow-empty
...
$ git push
...
Supplement
One of main reasons of this problem is that some Git servers, such as BitBucket, don’t have their master branch initialized when a fresh repository is cloned.
answered Oct 25, 2018 at 1:33
![]()
Jin KwonJin Kwon
19.9k14 gold badges109 silver badges179 bronze badges
1
Problem faced
I had the same problem when I was creating a new repository on GitHub and linking it with my React app in the client computer I have.
I used the following steps:
Commands used before the problem
git init
git commit -m "first commit"
git branch -M main
git remote add origin "_git repository link here_"
git push -u origin main
My mistake
But as you can see, my mistake was not using the git add . command.
I did this mistake, because I already had the README.md file and GitHub instructs us with basic commands while creating the repository.
My solution
My solution is to use git add . after the git init command.
Use the following set of commands in the same order to overcome the problem:
git init
git add .
git commit -m "first commit"
git branch -M main
git remote add origin "_git repository link here_"
git push -u origin main
![]()
answered Jun 14, 2021 at 7:28
3
Make sure you’ve added first, and then commit/ push:
Like:
git init
git add .
git commit -m "message"
git remote add origin "github.com/your_repo.git"
git push -u origin master
answered Apr 28, 2018 at 7:12
![]()
I faced the same issue some days ago.
If you created a new repository nowadays (2020) then the default branch is main on GitHub.
You can check on GitHub now in your repository branches.
And you can also check the branch in the terminal by running the command:
git branch
So that’s why you need to run
git push origin main
instead of
git push origin master
![]()
answered Dec 16, 2020 at 18:47
![]()
1
Two possibilities:
1- Either you forgot to include the .gitignore file.
Here are all the steps required:
-
Create an empty Git repository on remote,
-
On local, create the .gitignore
file for your project. GitHub gives you a list of examples here -
Launch a terminal, and in your project do the following commands:
git remote add origin YOUR/ORIGIN.git git add . git commit -m "initial commit or whatever message for first commit" git push -u origin master
2- Or you are trying to create a new GitHub project.
GitHub replaced master with main as the default branch name. To resolve the issue:
- On your local project:
- remove the
.gitfolder if it exists - recreate a clean repository by launching the following in your project:
- remove the
In the terminal:
git init
git add .
git commit -m "YOUR FIRST MESSAGE HERE"
git branch -M main
git remote add origin _GIT_LINK_TO_PROJECT_HERE_
git push -u origin main
2
For me,following worked to move untracked files:
git add --all
Next, I followed similar steps
git commit -m "First commit"
Then,
git remote add origin git@github.....
Last but not the least:
git push -u origin master
As you do this, Windows security will pop up asking for your username and password.
![]()
answered Jan 13, 2020 at 6:01
AreehaAreeha
8237 silver badges11 bronze badges
1
You probably forgot the command git add . after the git init command.
Lucas
5232 gold badges10 silver badges19 bronze badges
answered Apr 25, 2019 at 14:37
![]()
SumerSumer
2,65923 silver badges24 bronze badges
0
After the GitHub update 2000-10-01, you should use main instead of master.
Do it like this way…
- Create a repository on GitHub
- Delete existing
.gitfile in your local directory - Go to the local project directory and type
git init git add .git commit -m"My first commit"- Now check your branch name. It will be
masterin your local project git remote add origin <remote repository URL past here from the GitHub repository>, and then typegit remote -vgit push -f origin master- Now check the GitHub repository. You will see two branch 1.
main2.master - In your local repository create a new branch and the branch name will be
main git checkout maingit merge mastergit pull origin maingit push -f origin main
Note: from 2020-10-01, GitHub decided use main instead of master branch to use as the default branch name.
![]()
answered Oct 9, 2020 at 16:17
![]()
iamtheasadiamtheasad
99712 silver badges15 bronze badges
1
Just add an initial commit. Follow these steps:
-
git add . -
git commit -m "initial commit" -
git push origin master
This worked for me.
![]()
answered Dec 28, 2017 at 7:02
![]()
NeeruKSinghNeeruKSingh
1,5453 gold badges21 silver badges26 bronze badges
Feb, 2022 Update:
If your branch is «main»:

Run this command:
git push origin main
If your branch is «master»:

Run this command:
git push origin master
![]()
Henry Ecker♦
34.1k18 gold badges38 silver badges56 bronze badges
answered Feb 28, 2021 at 12:20
![]()
1
My issue was that the ‘master’ branch hadn’t been created locally yet.
A quick
git checkout -b "master"
created the master branch, at which point, a quick
git push -u origin master
pushed the work up to the Git repository.
answered Dec 12, 2014 at 19:38
![]()
AnthonyAnthony
13.2k13 gold badges59 silver badges79 bronze badges
0
I have faced the same issue, and this solved my problem:
Just make a branch:
git checkout -b "master"
After that,
git push -u origin master
Boom.
![]()
answered Jun 2, 2021 at 11:01
![]()
AlaminAlamin
1,81913 silver badges32 bronze badges
2
Maybe the branch is main instead of master.
Try
git push origin HEAD:main
or
git push origin main
![]()
answered May 20, 2021 at 18:21
![]()
1
This happens when you have added your file, forgot to commit and pushing.
So commit the files and then push.
answered Dec 3, 2011 at 13:29
user993563user993563
18.4k10 gold badges42 silver badges55 bronze badges
0
- First,
git add . - Second,
git commit -m "message" - Third,
git push origin branch
Please check for spelling mistakes because that could also give that error.
![]()
answered Jun 11, 2015 at 14:15
![]()
If you get this error while working in detached HEAD mode, you can do this:
git push origin HEAD:remote-branch-name
See also: Making a Git push from a detached head
If you are on a different local branch than the remote branch, you can do this:
git push origin local-branch-name:remote-branch-name
![]()
answered Mar 2, 2018 at 14:36
snapsnap
1,5211 gold badge14 silver badges20 bronze badges
It happens if you forget to commit before pushing for the first time. Just run:
git commit -m "first commit"
![]()
answered Sep 8, 2019 at 9:56
![]()
Badr BellajBadr Bellaj
11.4k2 gold badges42 silver badges44 bronze badges
To check the current status, git status.
And follow these steps as well:
git init
git add .
git commit -m "message"
git remote add origin "github.com/your_repo.git"
git push -u origin master
![]()
answered Sep 12, 2019 at 7:48
DinithDinith
79913 silver badges22 bronze badges
This just mean you forgot to do the initial commit, try
git add .
git commit -m 'initial commit'
git push origin master
answered May 4, 2014 at 14:18
xurixuri
8307 silver badges18 bronze badges
1
I had the same problem when I missed to run:
git add .
(You must have at least one file, or you will get the error again.)
![]()
answered Feb 11, 2017 at 21:49
![]()
neoDevneoDev
2,8693 gold badges33 silver badges66 bronze badges
0
I also followed GitHub’s directions as follows below, but I still faced this same error as mentioned by the OP:
git init
git add .
git commit -m "message"
git remote add origin "github.com/your_repo.git"
git push -u origin master
For me, and I hope this helps some, I was pushing a large file (1.58 GB on disk) on my MacOS. While copy pasting the suggested line of codes above, I was not waiting for my processor to actually finish the add . process. So When I typed git commit -m "message" it basically did not reference any files and has not completed whatever it needs to do to successfully commit my code to GitHub.
The proof of this is when I typed git status usually I get green fonts for the files added. But everything was red. As if it was not added at all.
So I redid the steps. I typed git add . and waited for the files to finish being added. Then I followed through the next steps.
![]()
answered Mar 22, 2019 at 16:16
![]()
GelGel
2,8362 gold badges17 silver badges25 bronze badges

When working with Git, you may come across an error that says «src refspace master does not match any».
Here’s what the error means and how you can solve it.
You may get this error when you try to trigger a push from a local repository to a master repository like this:
git push origin master
This error can occur for different reasons.
The most likely reason this error will occur is that the master branch does not exist.
Perhaps you cloned a new repository and the default branch is main, so there’s no master branch when you try to push for it.
You can display the remote branches connected to a local repository using the git branch -b command like this:
git branch -b
# results
# origin/main
# origin/feat/authentication
# origin/other branches ...
With the above results, you can see that there is no master repository (origin/master). So when you try to push to that repository, you will get the «respec error».
This result also applies to any other branch that does not exist. Let’s say, for example, I make changes and push to a remote hello branch that does not exist:
git add .
git commit -m "new changes"
git push origin hello
This command will produce the following error:
error: src refspec hello does not match any
How to Fix the «src refspec master does not match any» Error
Now you are aware that the master branch does not exist. The solution to this error is to either create a local and remote master branch that you can push the commit to or to push the commit to an existing branch – maybe main.
You can create a remote master branch on a Git managed website (like GitHub) or you can do that directly from your terminal like this:
git checkout -b master
# add commit
git push origin master
These commands will create a master branch locally. And by pushing to origin master, the master branch will also be created remotely.
But if you do not want to create a master branch, you can use the existing default branch (which may be main) instead.
Wrapping up
So if you get the Error: src refspec master does not match any error when you try to push to master, the most viable reason is that the master branch does not exist.
Learn to code for free. freeCodeCamp’s open source curriculum has helped more than 40,000 people get jobs as developers. Get started
Table of Contents
Hide
- When does git throws error: src refspec master does not match any?
- Scenario 1 – Pushing the changes to master or remote branch
- Solution for error: src refspec master does not match any.
- Scenario 2 – Check if a remote branch exists.
- Scenario 3 – Mismatch in Local and remote branch
- Scenario 4 – Committing and pushing Empty Directory in Git
There are quite a few reasons Git throws an error: src refspec master does not match any. Let us look at each of these cases and the solution to it.
Scenario 1 – Pushing the changes to master or remote branch
Let’s say you have created a git repository and added all the files from your local branch, but before committing the files, you try to push them into the remote branch or master branch.
mkdir repo && cd repo
git remote add origin /path/to/origin.git
git add .
After adding the files from the local branch, if you do git push, you will get an error: src refspec master does not match any. error: failed to push some refs to master.
git push -u origin master
error: src refspec master does not match any.
Solution for error: src refspec master does not match any.
All you need to perform is git commit with a proper message and then do git push to the remote origin to avoid any errors.
mkdir repo && cd repo
git remote add origin /path/to/origin.git
git add .
git commit -m "initial commit"
git push origin master
Scenario 2 – Check if a remote branch exists.
If you are working with Github, they have replaced the master branch with the main branch. Hence, in these circumstances, the local branch and remote branch ref will differ, and when you try to push the changes, git will throw an error since the remote branch itself is not present.
Solution – First, check what refs you have, and once you find that, make a git push to the specific remote branch.
# To get all the ref
git show-ref
# replace with your branch name according to ref
git push origin HEAD:<branch>
Scenario 3 – Mismatch in Local and remote branch
Generally, even the typo in the branch name while pushing the commit to the remote branch will lead to a refspec error.
Solution – Validate and check if you have given the right branch name while pushing the code to the remote branch.
Scenario 4 – Committing and pushing Empty Directory in Git
A certain version of Git like GitHub, bitbucket does not track the empty directories, so if a directory is empty and you are trying to commit and push, it will lead to an error: src refspec master does not match any.
Solution – Add a file to your directory before pushing it to a remote branch.
Srinivas Ramakrishna is a Solution Architect and has 14+ Years of Experience in the Software Industry. He has published many articles on Medium, Hackernoon, dev.to and solved many problems in StackOverflow. He has core expertise in various technologies such as Microsoft .NET Core, Python, Node.JS, JavaScript, Cloud (Azure), RDBMS (MSSQL), React, Powershell, etc.
Sign Up for Our Newsletters
Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.
By checking this box, you confirm that you have read and are agreeing to our terms of use regarding the storage of the data submitted through this form.
You need to add a file to a commit before you can push your changes to a remote Git repository. If you create a new repository and forget to add a file to a commit, you may encounter the “src refspec master does not match any” error.
In this guide, we discuss what this error means and why it is raised. We walk through an example of this error so you can figure out how to fix it on your computer.
Find Your Bootcamp Match
- Career Karma matches you with top tech bootcamps
- Access exclusive scholarships and prep courses
Select your interest
First name
Last name
Phone number
By continuing you agree to our Terms of Service and Privacy Policy, and you consent to receive offers and opportunities from Career Karma by telephone, text message, and email.
src refspec master does not match any
When you first create a Git repository, the repository has no commit history. If you want to push a change into a repository, you must first make a commit.
The workflow for pushing a change to a repository looks like this:
- Change a file
- Add the file to the staging area
- Create a commit
Once you have created a commit, you can push it to a remote server. If you forget the third step and try to push your code to a remote server, Git will raise an error. This is because Git will be unsure about what changes need to be made to the remote repository.
An Example Scenario
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 html-project cd html-project
We have created a directory called html-project and then we have moved into that directory.
Now that we have our folder ready, we can initialize a Git repository:
This command creates a hidden folder called .git/ which contains the configuration for our repository. Next, we create our first project file. We’re going to call this file index.html and add the following contents:
This file only contains one tag because we are still setting up our project. Now that we have a file in our repository, we’re going to link it up to a remote repository.
Our remote repository is hosted on GitHub. This will let us keep track of our project using the GitHub platform. To connect our local repository to the GitHub repository, we must add a remote reference to the GitHub repository:
git remote add origin https://github.com/career-karma-tutorials/html-project
After running this command, Git will know where our commits should go when we push them to our remote repository. Now we can add our changed file to our project:
Our index.html file is now in the staging area. To display this file on our remote repository, we can push it to the origin repository we just defined:
git push -u origin master
Let’s see what happens when we run this command:
error: src refspec master does not match any.
An error is returned.
The Solution
The git add command does not create a commit. The git add command moves files to the staging area. This is a triage space where files go before they are added to a commit. You can remove and add files from the staging area whenever you want.
This error is common if you try to push changes to a Git ref before you have created your first commit to your local repo or remote repo.
We need to create an initial commit before we push our code to our remote repository:
git commit -m "feat: Create index.html"
This will create a record of the repository at the current point in time, reflecting all the changes we added to the staging area. Now, let’s try to push our code.
Our code is successfully pushed to our remote repository.
Conclusion
The “src refspec master does not match any” error occurs if you have forgotten to add the files you have changed to a commit and try to push those changes to a remote repository before you make the first commit in your 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!
