git推送远程时报错failed to push some refs to 'ssh://xxxxx.com/project.git'

当需要将本地的已有的项目作为工作区,并新建本地库来跟踪工作区的变化,同时需要将本地库推送到分布式的服务器上(如:github)上时,需要进行以下操作:

  • 右击本地文件夹,git bash here
  • 建立本地库
    在弹出的命令行中输入git init
  • 在github上新建库,命名为projectA
  • 将本地库和github上面的远程库关联
    git remote add origin [email protected]:username\projectA
  • 将本地库的内容推送到github上
    git push -u origin master

在最后一步,将本地库的内容推送到github上时报错,内容为:

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

错误原因:

  • 1.远程库和本地库的文件存在冲突,比如远程库有README但是本地库没有
  • 2.本地推送之前没有commit

此时可以通过git push
解决方案:

1.git pull localRepository remoteRepository
git checkout --filename

2.git push之前先git commit

强制推送,能删除一些内容,新建远程库后,确保里面的文件不需要时可采用此方法

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.

添加所有文件时,可以使用

git add -A.

git add xx命令可以将xx文件添加到暂存区,如果有很多改动可以通过 git add -A .来一次添加所有改变的文件。

注意 -A 选项后面还有一个句点。 git add -A表示添加所有内容, git add . 表示添加新文件和编辑过的文件不包括删除的文件; git add -u 表示添加编辑或者删除的文件,不包括新添加的文件

参考:
https://stackoverflow.com/questions/4181861/src-refspec-master-does-not-match-any-when-pushing-commits-in-git

猜你喜欢

转载自blog.csdn.net/u010178308/article/details/82934497