使用git想GitHub遠程倉庫上傳代碼的基本步驟一般是
- 初始化為git倉庫
git init
- 添加所有要提交的文件
git add .
- 本次提交說明
git commit -m '提交說明'
- 關聯GitHub遠程倉庫
git remote add origin 倉庫地址(沒有添加ssh key建議使用 https地址)
- 拉去遠程倉庫最新代碼
git pull origin master
- 推送本次提交到倉庫
git push origin master
可是當你第一次push的時候一般會遇到如下報錯:
這種錯誤的主要原因是你的遠程倉庫的內容有改動但是你本地並沒有拉去最新的代碼所以會報錯。
$ git push origin master
To https://github.com/yuanchao614/wecoder.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/yuanchao614/wecoder.git' hint: Updates were rejected because the tip of your current branch is behind hint: its remote counterpart. Integrate the remote changes (e.g. hint: 'git pull ...') before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details.
解決方法
首先:
git pull origin master --allow-unrelated-histories
可能會出現vim編輯不用管直接保存退出就行
然后建議做完上一步之后再重新:
git add .
git commit -m '提交說明' git push origin master