在GitHub创建一个新的仓库,名字也命名为:learngit
然后在本地learngit仓库下运行命令:
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
# 出现这样的错误,可能是没有进入git仓库下
断开与远程库的关联(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.
# 出现这种错误,可能是因为路径不对,或者是秘钥文件没有添加到GitHub上
生成秘钥
如果在用户主目录下存在.ssh目录,说明秘钥文件已经生成了。如果想要重新生成,也可重新执行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
将秘钥复制到GitHub上,即可向GitHub推送你的内容。
那么,为什么GitHub需要SSH Key呢?因为GitHub需要识别出你推送的提交确实是你推送的,而不是别人冒充的,而Git支持SSH协议,所以,GitHub只要知道了你的公钥,就可以确认只有你自己才能推送。
当然,GitHub允许你添加多个Key。假定你有若干电脑,你一会儿在公司提交,一会儿在家里提交,只要把每台电脑的Key都添加到GitHub,就可以在每台电脑上往GitHub推送了。
添加远程仓库,并将分支内容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
由于远程库是空的,我们第一次推送master分支时,加上了-u参数,Git不但会把本地的master分支内容推送的远程新的master分支,还会把本地的master分支和远程的master分支关联起来,在以后的推送或者拉取时就可以简化命令。
注意:
git@github.com:linkingliang/learngit.git
网友评论