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

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 init
git add .
git commit -m <Your commit message>
git remote add origin "your repository link here"
git push -u origin master

其他回答

您可能忘记了命令gitadd。在gitinit命令之后。

也许分支是主分支而不是主分支。

Try

git推送原点HEAD:main

or

git推送原点主

我今天也遇到了同样的问题。我创建了一个新的repo并将其克隆到我的机器上。我提交了我的代码并尝试推送它。我也收到了同样的错误。我观察到这是因为我在使用:

git推送原始主机

我在这里做错的是,我假设我的默认分支是master,而GitHub上的新默认分支是main。我推送时使用:

git推送原点主

而且效果很好。

我的解决方案只适用于最近面临这个问题的较新的repo或用户,因为GitHub正在取代主要的over-master术语。因此,如果您收到此错误,请确保检查要推送到的分支以及GitHub上的分支名称。

2020年底,GitHub将其主分支更改为主分支。

我注意到GitHub创建了一个新的分支master,当我使用git push-u origin master时,这不是主分支:

现在,当我尝试使用gitpush-uorigin-main来直接推送到main分支时,会出现以下错误:

我面临这个错误:

src refspec main does not match any
error: failed to push some refs to 'https://github.com/<my_project_name>.git

在我第一次提交到main之后,我使用以下步骤修复了它。在以下代码中更改GitHub的URL:

git branch -M main
git remote add origin https://github.com/Sidrah-Madiha/<my_project_url>.git
git push -u origin main

对我来说,只需签出我希望代码推送的分支,然后简单地推送您的代码。

git checkout -b branchname-on-which-i-will-push-my-code

git add .
git commit -m "my commit message"
git push origin branchname-on-which-i-will-push-my-code