我使用以下内容克隆存储库:

git clone ssh://xxxxx/xx.git 

但在我更改一些文件并添加和提交它们之后,我想将它们推送到服务器:

git add xxx.php
git commit -m "TEST"
git push origin master

但我得到的错误是:

error: src refspec master does not match any.  
error: failed to push some refs to 'ssh://xxxxx.com/project.git'

当前回答

在我的例子中,我克隆了一个存储库,但我没有切换到本地分支。

我通过这样做解决了这个问题:

在对代码进行更改之前,应执行以下操作:

git checkout branch-name

然后对代码进行更改

之后,将代码推送到分支:

git push -u origin branch-name

此外,如果您第一次将本地存储库推送到GitHub,则需要首先创建一个主分支:

git branch -M main

然后,在添加原点(或给遥控器取的任何名称)后,按下分支:

git push -u origin main

其他回答

git init
git add .
git commit -m "message"
prompt me to enter email and user name
  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"

输入上述详细信息后,文件将提交并成功推送:

git push

我收到这个错误,因为我的本地分支名称与我试图使用git push origin创建的新远程分支不匹配。

如果您是第一次使用Git,则需要配置Git安装,包括:

git config --global user.email "you@example.com"

git config --global user.name "Your Name"

我遇到了同样的问题,我使用了--允许空:

$ git commit -m "initial commit" --allow-empty
...
$ git push
...

补充

这个问题的一个主要原因是,在克隆新的存储库时,一些Git服务器(如BitBucket)没有初始化其主分支。

万一您在执行gitinit并推送初始提交后仍面临此问题。然后,您可以尝试以下操作:,

git checkout -b "new branch name"
git push origin "new branch name"

您的代码将作为新分支推送。