如何用git命令行上傳本地代碼到github


 

注意:安裝的前提條件是配置好Git的相關環境或者安裝好git.exe,此處不再重點提及

        上傳的步驟:

    本文采用git 命令界面進行操作,先執行以下兩個命令,配置用戶名和email【設置用戶名和e-mail地址。這是非常重要的,因為每次Git提交都會使用該信息。它被永遠的嵌入到了你的提交中

           git config --global user.email "you@example.com"
           git config --global user.name "Your Name"

        然后開始創建項目並提交至遠程git倉庫,步驟如下:

        1.進入本地的項目目錄,右鍵“Git Bash here”,調出git命令行界面,然后輸入

      git init

       

       2.將目錄下的所有文件上傳,也可以將“.”換成具體的文件名

      git add .

     

       3.將項目提交到本地倉庫

      git commit -m "注釋語句"  

       

       4. 在github上創建新的repository

      

        5. 點擊 “Create repository”跳轉到一個連接,如下紅色圈獲取到本項目的github地址

        

           6. 將本地的代碼關聯到github上

          git remote add origin 項目的github地址

         

           7. 上傳代碼到github之前需要先pull  

         git pull origin master 

 

           8.上傳代碼到遠程git倉庫

         git push -u origin master

          

           9.輸入自己github的賬號,密碼,代碼會成功上傳上去

到此步,代碼已成功上傳至遠程倉庫。

---------------------------------------------------------------   以下是可能出現的錯誤 --------------------------------------------------------------

如果輸入$ git remote add origin git@github.com:djqiang(github帳號名)/gitdemo(項目名).git 

    提示出錯信息:fatal: remote origin already exists.

    解決辦法如下:

    1、先輸入$ git remote rm origin

    2、再輸入$ git remote add origin git@github.com:djqiang/gitdemo.git 就不會報錯了!   

    3、如果輸入$ git remote rm origin 還是報錯的話,error: Could not remove config section 'remote.origin'. 我們需要修改gitconfig文件的內容

    4、找到你的github的安裝路徑,我的是C:\Users\ASUS\AppData\Local\GitHub\PortableGit_ca477551eeb4aea0e4ae9fcd3358bd96720bb5c8\etc

    5、找到一個名為gitconfig的文件,打開它把裡面的[remote "origin"]那一行刪掉就好了!

 

    如果輸入$ ssh -T git@github.com
    出現錯誤提示:Permission denied (publickey).因為新生成的key不能加入ssh就會導致連接不上github。

    解決辦法如下:

    1、先輸入$ ssh-agent,再輸入$ ssh-add ~/.ssh/id_key,這樣就可以了。

    2、如果還是不行的話,輸入ssh-add ~/.ssh/id_key 命令後出現報錯Could not open a connection to your authentication agent.解決方法是key用Git
        Gui的ssh工具生成,這樣生成的時候key就直接保存在ssh中了,不需要再ssh-add命令加入了,其它的user,token等配置都用命令行來做。

    3、最好檢查一下在你複製id_rsa.pub文件的內容時有沒有產生多餘的空格或空行,有些編輯器會幫你添加這些的。

 

    如果輸入$ git push origin master

    提示出錯信息:error:failed to push som refs to .......

    解決辦法如下:

    1、先輸入$ git pull origin master //先把遠程伺服器github上面的文件拉下來

    2、再輸入$ git push origin master

    3、如果出現報錯 fatal: Couldn't find remote ref master或者fatal: 'origin' does not appear to be a git repository以及fatal:
          Could not read from remote repository.

    4、則需要重新輸入$ git remote add origingit@github.com:djqiang/gitdemo.git

 


免責聲明!

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



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