在 Git 中推送提交时出现消息“src refspec master 不匹配任何”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4181861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 09:29:06  来源:igfitidea点击:

Message 'src refspec master does not match any' when pushing commits in Git

gitcommit

提问by sinoohe

I clone my repository with:

我克隆我的存储库:

git clone ssh://xxxxx/xx.git 

But after I change some files and addand committhem, I want to push them to the server:

但我改变了一些文件,后addcommit他们,我想他们推到服务器:

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'

回答by baisong

Maybe you just need to commit. I ran into this when I did:

也许你只需要承诺。当我这样做时,我遇到了这个:

mkdir repo && cd repo
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 master

Success!

成功!

回答by Vi.

  1. Try git show-refto see what refs you have. Is there a refs/heads/master?

  2. You can try git push origin HEAD:masteras a more local-reference-independent solution. This explicitly states that you want to push the local ref HEADto the remote ref master(see the git-push refspecdocumentation).

  1. 试着git show-ref看看你有什么参考。有refs/heads/master吗?

  2. 您可以尝试git push origin HEAD:master作为更独立于本地参考的解决方案。这明确指出您要将本地引用推HEAD送到远程引用master(请参阅git-push refspec文档)。

回答by Aryo

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.

回答by tldr

  1. My changes were already committed
  2. Force push still gave me the same error.
  1. 我的更改已经提交
  2. 强制推送仍然给了我同样的错误。

So I tried Vi's solution:

所以我尝试了Vi 的解决方案

git push origin HEAD:<remoteBranch> 

This worked for me.

这对我有用。

回答by VIKAS KOHLI

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.

成功推送到master。

回答by Red

For me I had to make sure the public keyis 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.

对我来说,我必须确保公共密钥是正确的服务器(在〜/ .ssh / authorized_keys的附加)上配置并在GitHub上/到位桶(加入GitHub上或到位桶我的SSH密钥) -他们需要匹配。

Then:

然后:

git add --all :/

git commit -am 'message'

git push -u origin master

It worked for me in the end.

它最终对我有用。

回答by Andrew E

I found this happened in a brand new repository after I Git added only a directory.

在我 Git 只添加了一个目录后,我发现这发生在一个全新的存储库中。

As soon as I added a file (e.g. a README), Git push worked great.

一旦我添加了一个文件(例如一个自述文件),Git push 就很好用。

回答by aug2uag

Missing or skipping git add .or git commitmay cause this error:

缺少或跳过git add .git commit可能导致此错误:

git push -u origin master
Username for 'https://github.com': yourusername
Password for 'https://[email protected]': 
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

回答by pratik kumar

To fix it, re-initialize and follow the proper code sequence:

要修复它,请重新初始化并遵循正确的代码序列:

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

回答by Saurabh Singh

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