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

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'

当前回答

对我来说,这似乎是因为我已经完成了提交过程,所以我在尝试再次提交后得到了这个结果。

Your branch is ahead of 'origin/main' by 1 commit.
  (use "git push" to publish your local commits)

因为我看到路径是origin/main,所以我将其推到main而不是master,并且它起了作用。

注意:检查以确定主分支是主分支还是主分支。

截至2022年。

其他回答

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

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

在我的例子中,错误发生在我将更改推送到仅在本地拥有的分支时。远程分支不存在同名。我使用以下命令解决了该错误:

git push --set-upstream origin "your-branch-name"

当您试图将空回购推送到git服务器时,会发生此错误。这可以通过初始化README.md文件来缓解:

cat > README.md

然后键入一些内容,然后输入一个回车键,然后按CTRL+D保存。然后是通常的提交步骤:

git add .
git commit -m "Initial commit"
git push origin master

几天前我也遇到过同样的问题。

如果您现在(2020年)创建了一个新的存储库,那么默认分支是GitHub上的main。

您现在可以在存储库分支中查看GitHub。

您还可以通过运行以下命令检查终端中的分支:

git branch

所以这就是你需要跑步的原因

git push origin main

而不是

git push origin master

git分支使用-mv标志重命名分支:git分支-mv源主机

在这个git分支之后应该显示master