將本地專案上傳到個人GitHub上
阿新 • • 發佈:2018-12-20
一、前期準備工作
安裝git,具體的下載地址,安裝比較簡單在此不再詳述嘍,本次簡介是windows系統下的步驟
二、具體步驟
0、登入自己的GitHub賬號,建立一個空的repository,最好不要加README.md
1、進入需要上傳的專案資料夾中,選中該資料夾,點選滑鼠右鍵,選擇git bash
建立完成之後也會提示怎麼將程式碼加進去,如下圖
2、 進入git命令視窗,分別輸入以下命令
$ git init //用於建立git倉庫
$ git add . //表示將該目錄下的所有檔案都新增到倉庫
$ git commit -m "wrote a readme and a learn_git file" //雙引號的內容隨意,算是提示資訊
$ git remote add origin https://github.com/xxxxxx/x'x'x'x.git //最後的地址請自己將第0步建立的repository的地址填入即可,將本地的倉庫關聯到github上
$ git pull origin master --allow-unrelated-histories // 取回遠端主機某個分支的更新,再與本地的指定分支合併
$ git push -u origin master //上傳程式碼到github遠端倉庫
3 、 開啟自己的github主頁,重新整理剛剛建立的repository,你會發現你將自己本地專案已經上傳到github上了
三、錯誤總結
1、執行語句git pull origin master
出現如下錯誤提示
fatal: refusing to merge unrelated histories
解決:將命令修改成git pull origin master --allow-unrelated-histories
即可。
2、 執行語句git push -u origin master
出現如下錯誤提示
To https://github.com/xxxx.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/xxxx.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.
解決:由於剛開始建立空repository的時候帶了README.md檔案,所以出現錯誤的主要原因是github中的README.md檔案不在原生代碼目錄中,需要將github建立的README.md
檔案複製到本地專案中再次執行命令即可成功。或者剛開始建立repository的時候不帶README.md
檔案。