Git-for-windows連接github

安裝git

地址:https://git-scm.com/book/zh-tw/v1/開始-安裝Git

git配置

  1. 初始化git
    使用git先要在本地創建一個項目,用於存放代碼。在git終端進入項目地址,初始化git。

     $ cd Desktop/project
     $ git init
    
  2. 創建ssh key

     $ ssh-keygen -t rsa -b 4096 -C "[email protected]"
    

詢問你Enter a file in which to save the key (/c/Users/you/.ssh/id_rsa):[Press enter]直接按回車使用默認地址存放ssh key,也可以自己輸入地址。

	Enter passphrase (empty for no passphrase): [Type a passphrase]
	Enter same passphrase again: [Type passphrase again]

這裏提示你輸入密碼
3. 添加ssh key到ssh-agent
如果你遇到Permission denied (publickey)錯誤,可能是因爲這一步沒有完成。

	$ eval $(ssh-agent -s)
	Agent pid 59566
    $ ssh-add ~/.ssh/id_rsa
  1. 在github添加ssh
    在終端複製ssh key

     $ clip < ~/.ssh/id_rsa.pub
    

在github個人主頁點擊“setting–SSH and GPG keys–New SSH key”,點擊添加。
5. 測試連接

	ssh -T [email protected]

看到

	The authenticity of host 'github.com (192.30.252.1)' can't be established.
	RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
	Are you sure you want to continue connecting (yes/no)?

輸入yes,然後會看到

	Hi username! You've successfully authenticated, but GitHub does not provide shell access.

表示連接成功。

  1. 設置username和email
    在把本項目上傳到github之前還需要分別輸入設置username和email,因爲github每次commit都會記錄他們。所以分別輸入如下命令:

     $ git config --global user.name "your name"
     $ git config --global user.email "[email protected]"
    
  2. 添加遠程地址
    在與github連接成功後,如何才能讓相應的項目上傳到對應的倉庫呢?這裏就需要添加遠程地址,從而讓我們的本地項目順利到達對應的倉庫。
    打開終端,輸入

     $ git remote add origin [email protected]:yourName/yourRepo.git
    

後面的yourName和yourRepo分別是你的github的用戶名和剛纔新建的倉庫名。

上傳項目

進入項目,在終端運行

$ git status

查看要上傳的文件是否正確,然後將項目下的所有文件添加到git跟蹤範圍。

$ git add .

記錄此次提交併上傳

$ git commit -m 'my project push'
$ git push origin master

這裏的master指的是主分支名,如果是其他分支,則填寫相應的分支名。

這樣我們就將我們的項目上傳到github倉庫。

常見錯誤

  • Permission denied (publickey)
    上傳代碼時出現這個錯誤,解決辦法是直接重新生成一下公鑰,然後重新更新就行,其中密碼沒必要設置

      $ ssh-keygen -t rsa
    

其他情況參考https://help.github.com/articles/error-permission-denied-publickey/

  • error setting certificate verify locations:
    CAfile: D:/Program Files/Git/mingw64/libexec/ssl/certs/ca-bundle.crt
    CApath: none
    這是由於HTTPS需要認證證書,在終端輸入
    $ git config --system http.sslcainfo “D:\Program Files\Git\mingw64\ssl\certs\ca-bundle.crt”

參考資料:
https://segmentfault.com/a/1190000007466317
https://help.github.com/articles/connecting-to-github-with-ssh/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章