1. 程式人生 > >git推送遠端時報錯failed to push some refs to 'ssh://xxxxx.com/project.git'

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