在GitHub創(chuàng)建一個(gè)新的倉庫,名字也命名為:learngit
然后在本地learngit倉庫下運(yùn)行命令:
g@g-PC MINGW64 /
$ git remote add origin git@github.com/liangqinglin/learngit.git
fatal: Not a git repository (or any of the parent directories): .git
# 出現(xiàn)這樣的錯(cuò)誤,可能是沒有進(jìn)入git倉庫下
斷開與遠(yuǎn)程庫的關(guān)聯(lián)(git remote remove origin)
g@g-PC MINGW64 /learngit (master)
$ git remote add origin git@github.com/linkingliang/learngit.bit
fatal: remote origin already exists.
g@g-PC MINGW64 /learngit (master)
$ git remote remove origin
g@g-PC MINGW64 /learngit (master)
$ git remote add origin git@github.com/linkingliang/learngit.git
g@g-PC MINGW64 /learngit (master)
$ git push -u origin master
fatal: 'git@github.com/linkingliang/learngit.git' does not appear to be a git repository
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
# 出現(xiàn)這種錯(cuò)誤,可能是因?yàn)槁窂讲粚?,或者是秘鑰文件沒有添加到GitHub上
生成秘鑰
如果在用戶主目錄下存在.ssh目錄,說明秘鑰文件已經(jīng)生成了。如果想要重新生成,也可重新執(zhí)行keygen命令
ssh-keygen -t rsa -C "郵箱"
注意,此處是郵箱,而不是用戶名;id_rsa和id_rsa.pub是SSH key的密鑰對,id_rsa是私鑰,不能泄露出去;.pub是公鑰,可以放心的告訴任何人
g@g-PC MINGW64 /learngit (master)
$ ssh-keygen -t rsa -C "15210251075@163.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/g/.ssh/id_rsa):
/c/Users/g/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/g/.ssh/id_rsa.
Your public key has been saved in /c/Users/g/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:J23clHOlfL2vQ08jrbzvg02yB33f0WDLQkMhw0NdBfg 15210251075@163.com
The key's randomart image is:
+---[RSA 2048]----+
| o+..+ooo|
| oo+o o.|
| o+.+ o|
| o oooE..|
| S =..+o+.|
| + .++*=|
| .oXoB|
| =.=+|
| .*+.|
+----[SHA256]-----+
查看秘鑰
g@g-PC MINGW64 /learngit (master)
$ cat /c/Users/g/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDHGyGwO0DDDshVHHPfNwhsKqN3+vOhSz6XR3hTZavTv8z1mH/L9QFF1gxIJEj+UuyPrN8YYAmXiYIxAKuAay0OJpwA8oXqEZ3kEtWFBpWaJi1y2n1NJrzlBp+LqNCCXHRRS4mmBTX42Zdx4RNyQrhsFH6lWNfxQyW9aYvp1l0xeOcUapMeKFXkZGSYAyAw5982RJfC9xChjv01PDLfWrrwTv00pn4/DKewgObPKFrk6i5gFdsXQhA9iEn3CSPKRUnl9sCd8j1J5DIOqiUxUtJ5luZllOUKvfVvMFHj9dBv5HcvIbEoOV9kJ3iR3Y/ScvGTiO5O1mQRIwSvI7+LokZR heming
將秘鑰復(fù)制到GitHub上,即可向GitHub推送你的內(nèi)容。
那么,為什么GitHub需要SSH Key呢?因?yàn)镚itHub需要識別出你推送的提交確實(shí)是你推送的,而不是別人冒充的,而Git支持SSH協(xié)議,所以,GitHub只要知道了你的公鑰,就可以確認(rèn)只有你自己才能推送。
當(dāng)然,GitHub允許你添加多個(gè)Key。假定你有若干電腦,你一會兒在公司提交,一會兒在家里提交,只要把每臺電腦的Key都添加到GitHub,就可以在每臺電腦上往GitHub推送了。
添加遠(yuǎn)程倉庫,并將分支內(nèi)容push上去
Administrator@LENOV-20151017A MINGW64 ~/learngit (master)
$ git remote add origin git@github.com:liangqinglin/learngit.git
Administrator@LENOV-20151017A MINGW64 ~/learngit (master)
$ git push -u origin master
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 264 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
Branch master set up to track remote branch master from origin.
To github.com:liangqinglin/learngit.git
* [new branch] master -> master
由于遠(yuǎn)程庫是空的,我們第一次推送master分支時(shí),加上了-u參數(shù),Git不但會把本地的master分支內(nèi)容推送的遠(yuǎn)程新的master分支,還會把本地的master分支和遠(yuǎn)程的master分支關(guān)聯(lián)起來,在以后的推送或者拉取時(shí)就可以簡化命令。
注意:
git@github.com:linkingliang/learngit.git