使用git將本地項目上傳至git倉庫


我的博客 地址:https://www.wenhaofan.com/article/20190508220440

介紹

通常來講開發過程當中都是先在git建立遠程倉庫,而後fetch到本地倉庫,再進行commit push等操做,可是有時候也須要將本地已經開發的項目上傳至一個空的遠程倉庫中,期間也是遇到很多問題,特此總結一下git

初始化本地倉庫

初始化倉庫github

git init

將文件提交至本地倉庫gitlab

git commit -m "註釋"

關聯線上倉庫

git remote add origin <線上倉庫url>

線上倉庫url 爲以下連接fetch

https://github.com/wenhaofan/xxx.git
url

提交代碼

    常見錯誤及解決方案

                如今已經建立好了本地倉庫,並關聯上了遠程倉庫,若是咱們直接使用git push -u origin master將本地內容推送至線上那麼可能會出現出現如下錯誤spa

        git push

failed to push some refs to 'https://github.com/xxx/xxx.git'
hint: Updates were rejected because the remote contains work that you dogit
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

        以上錯誤很明顯的提示先執行 git pull 再push,須要先執行如下指令將遠程倉庫中master分支中的文件拉取到本地開發

git pull origin master

        若是沒有拋異常 那麼就能夠愉快的再次執行 git push 了,若是拋了異常,那麼能夠接着往下看rem

        git pull

 * branch            master     -> FETCH_HEAD
fatal: refusing to merge unrelated histories

        出現這個問題是由於本地庫和遠程庫沒有進行關聯遠, 而後本地推送到遠程庫, 遠端由於這個本地庫跟本身沒有關聯, 因此告知沒法合併,該狀況有兩種解決方法get

        第一種: 博客

       先從遠端庫拉下來代碼,而後將本地代碼放入本地庫中, 而後再執行push提交上去  

        第二種方法:

       使用如下命令,把兩段不相干的 分支進行強行合併

git pull origin master --allow-unrelated-histories

        而後再進行提交

git push gitlab master:init
相關文章
相關標籤/搜索