Linux git(9)----从远程库克隆

随声附和 提交于 2020-02-06 10:47:24
用到的新命令
1.git clone:克隆一个本地库

从远程库克隆,HTTPS克隆
1.在github上新创建一个仓库名为gitskills

2.用命令git clone克隆一个本地库,其中git clone后面的地址可以从图1中标记位置找到
[root@VM_0_11_centos learn_git]# git clone https://github.com/TQ5911/gitskills.git
Cloning into 'gitskills'...
Username for 'https://github.com': TQ5911
Password for 'https://TQ5911@github.com': 
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.

3.查看库情况,gitskills已经从github上远程克隆过来了
[root@VM_0_11_centos learn_git]# ls
gitskills  LICENSE  readme.txt  text.txt
[root@VM_0_11_centos learn_git]# ls -al gitskills/
total 16
drwxr-xr-x 3 root root 4096 Apr 24 23:11 .
drwxr-xr-x 4 root root 4096 Apr 24 23:11 ..
drwxr-xr-x 8 root root 4096 Apr 24 23:11 .git
-rw-r--r-- 1 root root   11 Apr 24 23:11 README.md


1.GitHub给出的地址不止一个,还可以使用SSH克隆
[root@VM_0_11_centos learn_git]# git clone git@github.com:TQ5911/gitskills.git
Cloning into 'gitskills'...
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
<== 但是提示被拒绝,原因是publickey过期,或是不存在

2.解决公钥问题
[root@VM_0_11_centos ~]# ssh-keygen -t rsa -C "ztq5911@163.com"
Generating public/private rsa key pair.    <==生成公钥/私钥rsa密钥对。
Enter file in which to save the key (/root/.ssh/id_rsa): /root/.ssh/id_rsa<==()里地址就行
Enter passphrase (empty for no passphrase): <== 输入密码
Enter same passphrase again: <== 确认密码,后续输出都是自动生成的
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:ohvLenn+bqSrhp/CnHpxhKn9varCYu7YGu2GGMTQxH8 ztq5911@163.com
The key's randomart image is:
+---[RSA 2048]----+
| +.              |
|. o              |
|o  +             |
| oo o E          |
|.o . .. S        |
|o.o .. ..        |
|+=.*oo o         |
|**O.==+ .        |
|OO=BO=+*o        |
+----[SHA256]-----+
[root@VM_0_11_centos ~]# cat /root/.ssh/id_rsa.pub    <==查看我们生成的公钥,都是字符串
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC77ZehfFS1nTNqghg3yt/Gws78UAqPH05Vt/2XL8TcjE3IcwIc4ke70pEaeLIp8vgsGis3P1jfm+2yHPC2xAyO4ic3eVi2JDWaTaVFNmxqrAMJV+Z3Fa/2HAI3wDstpV1DLDWONfQZsxutrxgbQqVMdauniqi0/TCIYgcS3dZZ69RvLu7bVzrb4xef2w+yT4poVZ7jxwP5Qa1RyoJv5ky5xWd8lICOaGRkPzPtWsGrO2H7BIic5jpBbcFnJfZAsW7xCduCmvSR6Vq1PqPL2YNe7qYulYP2xSgr/QzMCG506hNWLL874mNNM03EVssKpMdvh5Skyihk6ZdmB6EJo17b ztq5911@163.com

3.先测试与github的联通性
[root@VM_0_11_centos ~]# ssh git@github.com
Permission denied (publickey).    <==被拒绝

4.在github上创建新的SSH Key,title可以随便写,Key内容为上面产生的长串字符串,如图2
再尝试连接
[root@VM_0_11_centos ~]# ssh git@github.com
Enter passphrase for key '/root/.ssh/id_rsa':     <==密码
PTY allocation request failed on channel 0
Hi TQ5911! You've successfully authenticated, but GitHub does not provide shell access.
Connection to github.com closed.
<==成功联通

5.通过SSH来clone
[root@VM_0_11_centos ~]# git clone git@github.com:TQ5911/gitskills.git
Cloning into 'gitskills'...
Enter passphrase for key '/root/.ssh/id_rsa': 
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.
[root@VM_0_11_centos ~]# ls
gitskills learn_git    <==gitskills已成功clone过来了



Git支持多种协议,默认的git://使用ssh,但也可以使用https等其他协议。
使用https除了速度慢以外,还有个最大的麻烦是每次推送都必须输入口令,但是在某些只开放http端口的公司内部就无法使用ssh协议而只能用https。
Git支持多种协议,包括https,但通过ssh支持的原生git协议速度最快。

图1

图2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!