【Git】4、创建代码仓库,HTTP、SSH拉取远端代码

摘要:
我们可以通过git命令将远程仓库拉到本地,通常提供HTTPS协议和SSH协议进行管理。
拉取远端代码:使用Git命令下载远程仓库到本地

文章目录

简单复习 - 专栏 Git原理详解与实操指南

学习如何获取一个远程仓库的代码。

我们这就以代码托管平台Github为例;https://github.com/。

1、创建远程代码仓库

注册个账号

代码托管平台码云GithubGitlab等我们选个一个注册账号,

我们这选择Github吧 官网:https://github.com/

注册:https://github.com/join?source=header-home

在这里插入图片描述

2、创建仓库

登录之后,右上角有一个New repository选择,我们点击新建一个仓库。

在这里插入图片描述
进入创建仓库的页面,我们简单填写一下仓库名称等信息就行了。

在这里插入图片描述

点击“Create repository”,我们就成功建立了一个远程仓库了。

3、进入仓库

创建完毕后,就进入仓库。

在这里插入图片描述

我们可以通过 git 的命令将远程仓库拉取到本地,一般会提供 HTTPS 协议和 SSH 两种协议提供管理。

4、HTTP(S)获取远程仓库

HTTP 协议方式拉取代码(远程仓库)是比较简单的,直接执行 git 的 clone 命令即可,不需要额外的配置操作,但相对 SSH协议来说安全性较低。

首次拉取

HTTP 协议首次拉取代码的命令格式如下所示:

git clone 版本库地址	[本地文件夹名称]

我要把刚才新建的仓库代码拉取到本地,并且本地的文件夹名称叫play-spring-family(也可以不指定本地文件夹名称,默认名字为远程仓库名字),参考命令如下所示

git clone https://github.com/liuawen/play-spring-family.git   play-spring-family

那我去操作一下,先复制版本库地址吧

在这里插入图片描述

https://github.com/liuawen/play-spring-family.git

创建文件夹play-spring-family,执行命令git clone https://github.com/liuawen/play-spring-family.git play-spring-family即可把远程仓库拉取到本地。

命令执行完成后,会要求你输入用户名和密码,只有当你输入正确的用户名和密码之后代码才能正常拉取。应该是会出现这个。

Username for 'https://github.com': liuawen
Password for 'https://liuawen@github.com':

操作如下:

liuawen@DESKTOP-HVI7SH0:~$ su root
Password:
➜  liuawen pwd
/home/liuawen
➜  liuawen mkdir play_spring_family
➜  liuawen ls
git  play_spring_family  sources.list
➜  liuawen git clone https://github.com/liuawen/play-spring-family.git   play-spring-family
Cloning into 'play-spring-family'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
Unpacking objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
➜  liuawen cd play-spring-family
➜  play-spring-family git:(master) ls
README.md
➜  play-spring-family git:(master) ls -al
total 0
drwxr-xr-x 1 root    root    4096 Mar 21 19:24 .
drwxr-xr-x 1 liuawen liuawen 4096 Mar 21 19:24 ..
drwxr-xr-x 1 root    root    4096 Mar 21 19:25 .git
-rw-r--r-- 1 root    root      44 Mar 21 19:24 README.md
➜  play-spring-family git:(master)

更新代码

假设远程代码有变更,我想本地代码也更新一波时,我应该怎么做呢?

我可以在本地的版本库目录下通过git pull命令更新,不需要再指定远程地址,

命令如下

git pull

默认情况下会再次提示输入密码,因为 git 默认没有缓存 HTTP 认证权限。我是设置了

➜  play-spring-family git:(master) git pull
Already up to date.
➜  play-spring-family git:(master)

临时记住密码

如果不想每次都输入 git 的认证信息,可以设置缓存认证数据,默认记住 15 分钟,如下命令所示:

git config  --global credential.helper cache

当然也可以指定缓存时长,比如下面是自定义配置记住 1 分钟的命令:

git config credential.helper ‘cache –timeout=60’

credential n. 证书;凭据

timeout的单位为秒。

永久记住密码

感觉很麻烦啦,频繁输入用户名、密码。

我不想每次提交代码都要输入用户名密码,我要让 Git 永久记住密码,那怎么操作呢?

git config --global credential.helper store

命令执行完毕之后,会在当前用户主目录的.gitconfig文件中新增一项配置,执行命令查看

➜  .git git:(master) vim ~/.gitconfig

配置如下所示

在这里插入图片描述

上面是设计全局的,也可以设计局部的,那我设置下本地当前仓库的。

git config  credential.helper store

我去当前仓库.config文件查看下,是否设置好了

config

[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
[remote "origin"]
        url = https://github.com/liuawen/play-spring-family.git
        fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
        remote = origin
        merge = refs/heads/master

[credential]
        helper = store
~                        

执行过的命令:

➜  play-spring-family git:(master) git config  credential.helper store
➜  play-spring-family git:(master) pwd
/home/liuawen/play-spring-family
➜  play-spring-family git:(master) ls -al
total 0
drwxr-xr-x 1 root    root    4096 Mar 21 19:24 .
drwxr-xr-x 1 liuawen liuawen 4096 Mar 21 19:24 ..
drwxr-xr-x 1 root    root    4096 Mar 21 20:34 .git
-rw-r--r-- 1 root    root      44 Mar 21 19:24 README.md
➜  play-spring-family git:(master) ls al
ls: cannot access 'al': No such file or directory
➜  play-spring-family git:(master) cd .git
➜  .git git:(master) ls -al
total 0
drwxr-xr-x 1 root root 4096 Mar 21 20:34 .
drwxr-xr-x 1 root root 4096 Mar 21 19:24 ..
-rw-r--r-- 1 root root  107 Mar 21 20:22 FETCH_HEAD
-rw-r--r-- 1 root root   23 Mar 21 19:24 HEAD
-rw-r--r-- 1 root root   41 Mar 21 20:22 ORIG_HEAD
drwxr-xr-x 1 root root 4096 Mar 21 19:24 branches
-rw-r--r-- 1 root root  304 Mar 21 20:33 config
-rw-r--r-- 1 root root   73 Mar 21 19:24 description
drwxr-xr-x 1 root root 4096 Mar 21 19:24 hooks
-rw-r--r-- 1 root root  137 Mar 21 19:25 index
drwxr-xr-x 1 root root 4096 Mar 21 19:24 info
drwxr-xr-x 1 root root 4096 Mar 21 19:24 logs
drwxr-xr-x 1 root root 4096 Mar 21 19:24 objects
-rw-r--r-- 1 root root  114 Mar 21 19:24 packed-refs
drwxr-xr-x 1 root root 4096 Mar 21 19:24 refs
➜  .git git:(master) vim config

如果没有加上--global,则会在当前项目下的.git/config文件增加配置

[credential]
        helper = store

git 永久记住密码是根据配置文件所决定,我也可以直接复制配置文件。

https拉取远程仓库每次都要输用户名密码,用了git config --global credential.helper store,这样会把账号和密码明文保存在.gitconfig-credentials里。

5、 SSH拉取

SSH( Secure Shell )方式拉取代码,相比HTTP(S)来说更加安全。

为什么呢更安全呢?因为SSH方式使用的是非对称加密,采用公钥与私钥的方式。

相对来说配置起来会麻烦一些;好处是一次配置之后,后续不需要每次都进行认证,也更加安全效率也高点吧。

拉取代码

SSH

git clone git@github.com:liuawen/play-spring-family.git play-spring-family-ssh

SSH地址:

在这里插入图片描述

执行git clone git@github.com:liuawen/play-spring-family.git play-spring-family-ssh命令,提示需要权限验证。

➜  liuawen ls
git  play-spring-family  play_spring_family  sources.list
➜  liuawen git clone git@github.com:liuawen/play-spring-family.git play-spring-family-ssh
Cloning into 'play-spring-family-ssh'...
The authenticity of host 'github.com (13.250.177.223)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no)?

没有配置公钥与私钥,所以我们这第一次拉取代码失败了。

➜  liuawen git clone git@github.com:liuawen/play-spring-family.git play-spring-family-ssh
Cloning into 'play-spring-family-ssh'...
The authenticity of host 'github.com (13.250.177.223)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,13.250.177.223' (RSA) to the list of known hosts.
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
➜  liuawen

怎么办呢?看下面吧。

创建一个ssh key

通过 ssh 协议拉取代码要保证当前用户的主目录存在一个.ssh的文件夹,并且里面已经存在私钥文件,如果没有的话我们可以通过ssh-keygen,生成一份公钥与私钥。

➜  liuawen ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/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:z5hcJzLsnISUNxjOFeZz9R1PPsWk8SirFwoRyWYe0VE root@DESKTOP-HVI7SH0
The key's randomart image is:
+---[RSA 2048]----+
|      ..*=.oE .++|
|     o B*... . O=|
|      *+*.. . +.=|
|     . +.=   o  .|
|      . S o +    |
|       = @ = .   |
|        B = .    |
|           .     |
|                 |
+----[SHA256]-----+
➜  liuawen cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDRaxJM1CAl0u+ImEVhC2P2iiGloo1Bx4NQ2bOjQxgy96ncTwUbrG3QmUmX86Oyfl49DRgpnwYe3wBA2wy7fxrqz2sYJAPdhqVlyJpL9SiKdOAw9Vu8aA1IVzDBhKN4W2bHJ7xQ6X7OnQ5tTz3Mcjdyk8cbwVIg1zYI56ABQZaEewt3deTlU24rAaqb+0voA/RrzoaBqtv6XHXpef/s4QIWQZ21m2IyppUkQERC28xVaAwJGLedIrjQ6AyLLxAkgd5BZkhCv02PPDYWT2ixlHK2DDpX45BEgUNDbi6kqKMglK0G67kLFiFssmDwF2vLDGjlKIyWYPwgwNYyvR73d7SF root@DESKTOP-HVI7SH0
➜  liuawen

ssh-keygen,最终会在当前用户目录下生成公钥和私钥,查看生成的公钥的命令为cat ~/.ssh/id_rsa.pub

要保存好私钥哦。

添加公钥到服务器

ssh-keygen
cat ~/.ssh/id_rsa.pub

当我们确认公钥和私钥生成完毕之后,我们还需要将公钥放到远程的 Github 仓库中去。

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

把上面的cat ~/.ssh/id_rsa.pub命令查看到的密钥复制过来,点击"Add SSH key"。

再次拉取代码

当我们把公钥添加到Github版本库进去之后,就已经完成了权限配置。

我们再次使用ssh方式拉取代码,就不会提示没有权限。

➜  liuawen git clone git@github.com:liuawen/play-spring-family.git play-spring-family-ssh
Cloning into 'play-spring-family-ssh'...
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.
➜  liuawen ls
git  play-spring-family  play-spring-family-ssh  play_spring_family  sources.list
➜  liuawen cd play-spring-family-ssh
➜  play-spring-family-ssh git:(master) ls -al
total 0
drwxr-xr-x 1 root    root    4096 Mar 21 21:04 .
drwxr-xr-x 1 liuawen liuawen 4096 Mar 21 21:04 ..
drwxr-xr-x 1 root    root    4096 Mar 21 21:06 .git
-rw-r--r-- 1 root    root      44 Mar 21 21:04 README.md
➜  play-spring-family-ssh git:(master)

执行git clone git@github.com:liuawen/play-spring-family.git play-spring-family-ssh命令之后,代码已经成功拉取咯。

我们来更新一波代码

更新代码

ssh 方式更新代码命令和上面的 http 方式拉取代码命令一致,同样需要在 目录play-spring-family-ssh下执行命令:git pull,然后可以看到git成功的拉取到了代码

➜  play-spring-family-ssh git:(master) git pull
Warning: Permanently added the RSA host key for IP address '13.229.188.59' to the list of known hosts.
Already up to date.
➜  play-spring-family-ssh git:(master)
6、小结

使用过的命令

git clone 版本库地址	[本地文件夹名称]
git pull
git config  --global credential.helper cache
git config credential.helper ‘cache –timeout=60’
git config --global credential.helper store
vim ~/.gitconfig
git clone git@github.com:liuawen/play-spring-family.git play-spring-family-ssh
ssh-keygen
cat ~/.ssh/id_rsa.pub

学习了如何在Github创建一个远程仓库,以及两种方式拉取远程仓库代码拉取到本地。

HTTP(S)和SSH协议方式拉取代码。

HTTP拉取

git clone https://github.com/liuawen/play-spring-family.git   play-spring-family
git pull
git config  --global credential.helper cache
git config credential.helper ‘cache –timeout=60’
git config --global credential.helper store

Q:HTTP(S)协议默认每次需要输入账号密码,那我不想每次输入账号密码怎么解决呢?

A:可以通过缓存认证方式处理,永久(临时)记住密码。

使用git config --global credential.helper store命令,这样会把账号和密码明文保存在.gitconfig-credentials里。

SSH拉取

SSH第一次直接拉取代码,会提示没有权限,

我们要将SSH协议生成的公钥放到 Git 服务器当中去,配置好了Git会自动通过ssh协议进行鉴权,不需要通过账号加密码。

git clone git@github.com:liuawen/play-spring-family.git play-spring-family-ssh
git pull
ssh-keygen

Q:换了计算机,要不要重新配置SSH?

A:可以重新配置,也可以将原来计算机的.ssh文件夹复制到新的计算机。

免责声明:文章转载自《【Git】4、创建代码仓库,HTTP、SSH拉取远端代码》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇前端worker之web workerlinux和windows双系统互拷文件乱码问题下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Django(一):从socket到MVC

一、socket的http套路 web应用本质上是一个socket服务端,用户的浏览器是一个socket客户端。socket处在应用层与传输层之间,是操作系统中I/O系统的延伸部分(接口),负责系统进程和应用之间的通信。 HTTP协议又称超文本传输协议。 1 //浏览器发送一个HTTP请求; 2 //服务器收到请求,根据请求信息,进行函数处理,生成一个H...

记一次阿里云负载测试

环境: 服务器:主服务器A,备服务器B(主服务器配置和带宽都大于备服务器器) 业务:tomcat 端口:443,80 协议:https 域名:www.danny.com 证书:ssl证书,域名绑定了ssl证书并都配置安装在AB服务器中 要求:主要使用A服务器跑业务,B服务器只有在A服务器不可用时才接受访问流量 阿里云负载均衡后台转发服务器组选择: 1.后端...

Jenkins自动触发构建maven多模块项目

一、要求 在一个Spring boot项目中,通过Jenkins来触发构建某单一模块时,其他模块和其余分支不受影响 二、Jenkins配置 1. 安装插件 插件:Generic Webhook Trigger Plugin 2. 配置用户 设置用户token,不然会报403错误 jenkins---> 用户列表----> 具体用户------...

windows环境 pip离线安装pytorch-gpu版本总结(没用anaconda)

1.确定你自己的环境信息。 我的环境是:win8+cuda8.0+python3.6.5 各位一定要根据python版本和cuDa版本去官网查看所对应的.whl文件再下载! 2.去官网查看环境匹配的torch、torchversion版本信息,然后去镜像源下载对应的文件 (直接去官网下载会出现中断的情况,如果去官网下载建议尝试迅雷下载)或者镜像网站下载对应...

开源中国社区项目部署

一.官方网站 https://git.oschina.net/ 开源中国社区成立于2008年8月,其目的是为中国的IT技术人员提供一个全面的、快捷更新的用来检索开源软件以及交流使用开源经验的平台 目前国内有很多公司会将公司的项目部署在OSChina 二.与GitHUB的对比 服务器在国内,速度更快 免费账户同样可以建立私有项目,而GitHUB上要建...

nexus在linux上搭建

Maven 仓库的分类:(maven的仓库只有两大类) 1.本地仓库  2.远程仓库,在远程仓库中又分成了3种: 2.1 中央仓库  2.2 私服  2.3 其它公共库 有个maven私服可以很方便地管理我们的jar包和发布构建到远程仓库,本文就介绍了如何在linux下一步步使用nexus搭建maven私服。 私服是架设在局域网的一种特殊的远程仓库,目的是...