GIT远程仓库的两种操作

一、远程克隆仓库

环境说明:	
	角色			IP
git服务端		192.168.0.106
git客户端		192.168.0.107
git客户端1		192.168.0.108

1.发布仓库

​ 在服务端先发布仓库

[root@localhost ~]# cd /data/
[root@localhost ~]# cd /data/
[root@localhost data]# mkdir base1
[root@localhost data]# git init --bare base1/
初始化空的 Git 版本库于 /data/base1/
[root@localhost data]# cd base1/
[root@localhost base1]# ls
branches  config  description  HEAD  hooks  info  objects  refs

此时我们在服务端已经初始化了一个不带工作区的仓库

2.创建克隆用户

创建系统用户,git客户端用此用户来克隆服务端得仓库

#在服务端操作
[root@localhost ~]# useradd test1
[root@localhost ~]# passwd test1
更改用户 test1 的密码 。
新的 密码:
无效的密码: 密码少于 8 个字符
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。

#将仓库的权限改为test1用户
[root@localhost ~]# chown -R test1:test1 /data/base1/

3.创建密钥

如果使用密码得话,每次更新、提交、都要输入用户密码,所以我们这里配置test1用户使用密钥通信

#在客户端1操作

[root@localhost ~]# 
[root@localhost ~]# ssh-keygen -t rsa
#过程中所有得选择都回车即可
[root@localhost ~]# ls -a
.  ..  .bash_logout  .bash_profile  .bashrc  .cache  .config  .mozilla  .ssh

#在test1用户的家目录下产生了.ssh目录
[root@localhost ~]# cd .ssh/
[test1@localhost .ssh]$ ls 
id_rsa  id_rsa.pub

#id_rsa:私钥
#id_rsa.pub 公钥

4.将公钥拷贝给客户端

#在客户端1操作

[root@localhost .ssh]# ssh-copy-id -i id_rsa.pub test1@192.168.0.106
The authenticity of host '192.168.0.106 (192.168.0.106)' can't be established.
ECDSA key fingerprint is 1f:bc:36:f6:7f:49:a9:70:ca:64:73:2c:b2:91:6a:6b.
Are you sure you want to continue connecting (yes/no)? yes #这里输入yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
test1@192.168.0.106's password:   #这里输入test1的密码

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'test1@192.168.0.106'"
and check to make sure that only the key(s) you wanted were added.

5.验证

#在客户端1操作
#查看是否能够通过密钥的形式连接到服务器

[root@localhost ~]# ssh test1@192.168.0.106
Last login: Sun Nov 29 14:37:15 2020
[test1@localhost ~]$ ifconfig ens32
ens32: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.0.106  netmask 255.255.255.0  broadcast 192.168.0.255
#我们发现已经不用通过密码就可以通过test1用户连接到服务器,IP已经是服务器的地址了

6.在克隆仓库

#在客户端1操作
#不要忘记在客户端退出test1用户,使用客户端的用户操作
#git clone格式:
#git clone 服务端的用户@服务端的IP地址:服务端仓库的绝对路径

[root@localhost ~]# mkdir /data/
[root@localhost ~]# git clone test1@192.168.0.106:/data/base1 /data
Cloning into '/data'...
warning: You appear to have cloned an empty repository. #这个警告可不用管,提示你克隆一个空的仓库

[root@localhost ~]# cd /data/

#在此目录下出现了隐藏目录。git 说明我们已经克隆成功了
[root@localhost data]# ls -a
.  ..  .git


7.查看远程仓库

#我们在data目录下输入以下命令可以查看到远程仓库的信息,因为我们是克隆的远程服务器的仓库,所以会自动建立联系
[root@localhost data]# git remote -v
origin	test1@192.168.0.106:/data/base1 (fetch)
origin	test1@192.168.0.106:/data/base1 (push

8.提交数据

#在客户端1操作
#我们将本地数据提交到服务端上的远程仓库上
[root@localhost ~]# cd /data/
[root@localhost data]# touch test1
[root@localhost data]# echo "version 1" >> test1
[root@localhost data]# git add test1
[root@localhost data]# git commit -m '1' test1
[master bfb8ea5] 1
 1 file changed, 1 insertion(+)
 create mode 100644 test1

#将本地数据提交到服务器上的远程仓库
[root@localhost data]# git push origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 208 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To test1@192.168.0.106:/data/base1
 * [new branch]      master -> master
 
 #origin:这是远程仓库的名字
 #master 这是本地的master分支
 
 

9.在客户端2测试

#首先在客户端2生成密钥,并且把公钥赋值给服务端
[root@localhost ~]# ssh-keygen  -t rsa
[root@localhost ~]# cd .ssh/
[root@localhost .ssh]# ssh-copy-id -i id_rsa.pub test1@192.168.0.106
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "id_rsa.pub"
The authenticity of host '192.168.0.106 (192.168.0.106)' can't be established.
ECDSA key fingerprint is SHA256:dGzRnSYCpHgjmv2rTLqqwBQ+M291fkU9NRCaGY2fZRI.
ECDSA key fingerprint is MD5:1f:bc:36:f6:7f:49:a9:70:ca:64:73:2c:b2:91:6a:6b.
Are you sure you want to continue connecting (yes/no)? yes  #输入yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
test1@192.168.0.106's password:  #输入test1的密码

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'test1@192.168.0.106'"
and check to make sure that only the key(s) you wanted were added.

10.在客户端2克隆仓库

[root@localhost ~]# mkdir /data
[root@localhost ~]# cd /data/
[root@localhost data]# git clone test1@192.168.0.106:/data/base1 ./
Cloning into '.'...
warning: You appear to have cloned an empty repository.

#克隆成功


11.在客户端2验证

[root@localhost data]# git clone test1@192.168.0.106:/data/base1/ ./
Cloning into '.'...
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.

#我们发现在客户端2已经有客户端1提交的数据了
[root@localhost data]# ls
test1

12.在客户端2提交数据

[root@localhost data]# touch test2
[root@localhost data]# echo "test2" >> test2
[root@localhost data]# git add -A
[root@localhost data]# git commit -m '2' test2
[root@localhost data]# git push origin master
Counting objects: 4, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 261 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To test1@192.168.0.106:/data/base1/
   45bc3bd..8b6a573  master -> master
#我们发现数据成功提交到远程仓库   
   

13.在客户端1上更新客户2的数据

[root@localhost data]# git pull
remote: Counting objects: 4, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From 192.168.0.106:/data/base1
   45bc3bd..8b6a573  master     -> origin/master
Updating 45bc3bd..8b6a573
Fast-forward
 test2 | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 test2
 
[root@localhost data]# ls
test1  test2
[root@localhost data]# cat test2
test2
#我们发现在客户端1成功拉取到了客户端2提交的数据

二、在本地添加远程仓库

1.在客户端1添加仓库

#在客户端1操作
#上边实例中的客户端是从服务端克隆下来的,自动就和服务端建立了联系
#如果我们在客户的端自己初始化的数据库,是不会与服务端建立联系的
[root@localhost ~]# mkdir /client1
[root@localhost ~]# git init /client1/
Initialized empty Git repository in /client1/.git/

#我们使用git remote -v 查看是看不到有远程仓库的
[root@localhost client1]# git remote -v

2.我们在客户端与远程仓库建立联系

[root@localhost client1]# git remote add server test1@192.168.0.106:/data/base1
[root@localhost client1]# git remote -v
server	test1@192.168.0.106:/data/base1 (fetch)
server	test1@192.168.0.106:/data/base1 (push)

3.拉取数据

[root@localhost client1]# git pull server master
remote: Counting objects: 6, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 6 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (6/6), done.
From 192.168.0.106:/data/base1
 * branch            master     -> FETCH_HEAD

#我们发现我成功从远程仓库上拉取了数据
[root@localhost client1]# ls
test1  test2

4.提交数据

[root@localhost client1]# touch test3
[root@localhost client1]# echo "test3"  >> test3
[root@localhost client1]# git add -A
[root@localhost client1]# git commit -m '3'
[master 87dbe64] 3
 1 file changed, 1 insertion(+)
 create mode 100644 test3

#将本地的master 提交到远程仓库上。此处的server 就是上边例子中origin 。server只是我们自定义的名字
[root@localhost client1]# git push server master
Counting objects: 4, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 284 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To test1@192.168.0.106:/data/base1
   8b6a573..87dbe64  master -> master

5.在客户端2拉取数据

[root@localhost ~]# cd /data/
[root@localhost data]# ls
test1  test2
[root@localhost data]# git pull
remote: Counting objects: 4, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From 192.168.0.106:/data/base1
   8b6a573..87dbe64  master     -> origin/master
Updating 8b6a573..87dbe64
Fast-forward
 test3 | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 test3
 
 #我们发现test3已成成功的拉取到了本地
[root@localhost data]# ls
test1  test2  test3
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值