這裏假設你的倉庫地址爲/user/test.git,email地址爲youremail@example.com,使用的時候替換成你本身的地址便可。首先註冊github賬號,因爲你的本地Git倉庫和github倉庫之間的傳輸是經過SSH加密的,因此須要使用ssh key:git
1)建立SSH Key。在用戶主目錄下,看看有沒有.ssh目錄,若是有,再看看這個目錄下有沒有id_rsa和id_rsa.pub這兩個文件,若是有的話,直接跳過此以下命令,若是沒有的話,打開命令行,輸入以下命令:github
# ssh-keygen -t rsa -b 4096 -C "youremail@example.com"sql
id_rsa是私鑰,不能泄露出去,id_rsa.pub是公鑰,能夠放心地告訴任何人。ssh
2)經過ssh-agent來管理密鑰ide
這裏咱們經過ssh-agent來管理密鑰,ssh-agent是一個密鑰管理器,運行ssh-agent之後,使用ssh-add將私鑰交給ssh-agent保管,其餘程序須要身份驗證的時候能夠將驗證申請交給ssh-agent來完成整個認證過程。post
# eval "$(ssh-agent -s)"ui
# ssh-add –l加密
4096 7d:8c:84:86:95:ce:47:29:d4:9b:39:8e:28:0c:62:c9 /root/.ssh/id_rsa (RSA)url
3)登陸github,打開」settings」中的SSH Keys頁面,而後點擊「Add SSH Key」,填上任意title,在Key文本框裏黏貼id_rsa.pub文件的內容。spa
這時候對本地倉庫和github進行同步
# git push -u origin master
error: The requested URL returned error: 403 Forbidden while accessing https://github.com/user/test.git/info/refs
fatal: HTTP request failed
提示403 Forbidden!
打開開關看詳細日誌
# export GIT_CURL_VERBOSE=1
# git push
* Couldn't find host github.com in the .netrc file; using defaults
* About to connect() to github.com port 443 (#0)
* Trying 192.30.253.112... * Connected to github.com (192.30.253.112) port 443 (#0)
* Initializing NSS with certpath: sql:/etc/pki/nssdb
* CAfile: /etc/pki/tls/certs/ca-bundle.crt
CApath: none
* SSL connection using TLS_RSA_WITH_AES_128_CBC_SHA
* Server certificate:
* subject: CN=github.com,O="GitHub, Inc.",L=San Francisco,ST=California,C=US,postalCode=94107,STREET="88 Colin P Kelly, Jr Street",serialNumber=5157550,incorporationState=Delaware,incorporationCountry=US,businessCategory=Private Organization
* start date: Mar 10 00:00:00 2016 GMT
* expire date: May 17 12:00:00 2018 GMT
* common name: github.com
* issuer: CN=DigiCert SHA2 Extended Validation Server CA,OU=www.digicert.com,O=DigiCert Inc,C=US
> GET /user/test.git/info/refs?service=git-receive-pack HTTP/1.1
User-Agent: git/1.7.1
Host: github.com
Accept: */*
Pragma: no-cache
* The requested URL returned error: 401 Authorization Required
* Closing connection #0
* Couldn't find host github.com in the .netrc file; using defaults
* About to connect() to github.com port 443 (#0)
* Trying 192.30.253.112... * Connected to github.com (192.30.253.112) port 443 (#0)
* CAfile: /etc/pki/tls/certs/ca-bundle.crt
CApath: none
* SSL connection using TLS_RSA_WITH_AES_128_CBC_SHA
* Server certificate:
* subject: CN=github.com,O="GitHub, Inc.",L=San Francisco,ST=California,C=US,postalCode=94107,STREET="88 Colin P Kelly, Jr Street",serialNumber=5157550,incorporationState=Delaware,incorporationCountry=US,businessCategory=Private Organization
* start date: Mar 10 00:00:00 2016 GMT
* expire date: May 17 12:00:00 2018 GMT
* common name: github.com
* issuer: CN=DigiCert SHA2 Extended Validation Server CA,OU=www.digicert.com,O=DigiCert Inc,C=US
> GET /user/test.git/info/refs HTTP/1.1
User-Agent: git/1.7.1
Host: github.com
Accept: */*
Pragma: no-cache
* The requested URL returned error: 403 Forbidden
* Closing connection #0
error: The requested URL returned error: 403 Forbidden while accessing https://github.com/user/test.git/info/refs
fatal: HTTP request failed
能夠看到上面紅色加粗的文字(The requested URL returned error: 401 Authorization Required),是權限問題致使的,能夠修改.git/config文件追加用戶名和密碼:
1)編輯.git/config文件
2)在[remote 「origin」]下找到找到url變量
3)修改url = https://github.com/user/test.git,修改成url = ssh://git@github.com/user/test.git,修改完了保存
4)經過git push origin master進行同步,已經能夠成功了