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



個人博客 地址:https://www.wenhaofan.com/article/20190508220440

介紹

一般來說開發過程中都是先在git創建遠程倉庫,然后fetch到本地倉庫,再進行commit push等操作,但是有時候也需要將本地已經開發的項目上傳至一個空的遠程倉庫中,期間也是遇到不少問題,特此總結一下

初始化本地倉庫

初始化倉庫

git init

將文件提交至本地倉庫

git commit -m "注釋"

關聯線上倉庫

git remote add origin <線上倉庫url>

線上倉庫url 為如下鏈接

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

提交代碼

    常見錯誤及解決方案

                現在已經創建好了本地倉庫,並關聯上了遠程倉庫,如果我們直接使用git push -u origin master將本地內容推送至線上那么可能會出現出現以下錯誤

        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 了,如果拋了異常,那么可以接着往下看

        git pull

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

        出現這個問題是因為本地庫和遠程庫沒有進行關聯遠, 然后本地推送到遠程庫, 遠端因為這個本地庫跟自己沒有關聯, 所以告知無法合並,該情況有兩種解決方法

        第一種:

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

        第二種方法:

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

git pull origin master --allow-unrelated-histories

        然后再進行提交

git push gitlab master:init

        


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM