Git环境部署

 部署git 服务器环境

 

  1. 系统环境准备

192.168.30.25   master     git   gitlab

192.168.30.26   client      git

关闭防火墙

[root@master ~]# iptables -F

[root@master ~]# setenforce 0

[root@master ~]# systemctl stop firewalld

  1. 使用yum安装git  (两台服务器都需要安装)

[root@master ~]# yum -y install git

[root@client ~]# yum -y install git

创建git版本仓库,一般规范的方式要以.git为后缀

[root@master ~]# mkdir linuxprobe.git

[root@master ~]# useradd git

[root@master ~]# passwd git

更改用户 git 的密码 。

新的 密码:

无效的密码: 密码是一个回文

重新输入新的 密码:

passwd:所有的身份验证令牌已经成功更新。

[root@master ~]# chown -Rf git:git linuxprobe.git

初始化版本仓库

[root@master ~]# cd linuxprobe.git/

[root@master linuxprobe.git]# git --bare init

初始化空的 Git 版本库于 /root/linuxprobe.git/

此时Git服务器已经部署好了,但不能向服务器推送数据,也不能克隆Git版本仓库,需要

在服务器上开放至少一种支持Git的协议,比如http/https/ssh等现在用的最多的就是HTTPSSSH ,本案例中使用的是SSH协议,切换至Git客户机来生成SSH秘钥

[root@client ~]# ssh-keygen //一路回车

+---[RSA 2048]----+

|      .=.oo*o.   |

|     .. X.B..    |

|    o oo Xo+     |

|     * +.o*oo    |

|      B S. *o    |

|     . X  o oo   |

|      o +  oE .  |

|       . ..  .   |

|        oo       |

+----[SHA256]-----+                    

[root@client ~]# ssh-copy-id 192.168.30.25  //将客户机的公钥传递给git服务器

此时就已经可以从Git服务器中克隆版本仓库了(此时目录没有文件是正常的)

[root@client ~]# git clone root@192.168.30.25:/root/linuxprobe.git

正克隆到 'linuxprobe'...

warning: 您似乎克隆了一个空版本库。

配置的是用户名名称和电子邮件地址。每次git提交都会引用这两条信息,记录提交了文件的用户,并且会随更新内容一起呗永久纳入历史记录

[root@client ~]# git config --global user.name "zhan san"

[root@client ~]# git config --global user.email root@linuxprobe.com

设置vim 为默认的文本编辑器

[root@client ~]# git config --global core.editor vim

此时查看git工作环境信息

[root@client ~]# git config --list

user.email=root@linuxprobe.com

user.name=zhan san

core.editor=vim

Git只能追踪类似于txt文件,网页,程序源码等文本文件内容的变化,下面模拟代码提交的过程

[root@client ~]# cd linuxprobe/

[root@client linuxprobe]# echo "Initialization Git repository" > readme.txt

将文件添加到暂存区

[root@client linuxprobe]# git add readme.txt

添加到暂存区后再次修改文件的内容

[root@client linuxprobe]# echo "Something not important" >> readme.txt

将暂存区的文件提交到git版本仓库,命令格式为“git commit -m ”提交说明

[root@client linuxprobe]# git commit -m "add the readme file"

[master(根提交) b4cc72b] add the readme file

 1 file changed, 1 insertion(+)

 create mode 100644 readme.txt

查看当前工作目录的状态

[root@client linuxprobe]# git status

# 位于分支 master

# 尚未暂存以备提交的变更:

#   (使用 "git add <file>..." 更新要提交的内容)

#   (使用 "git checkout -- <file>..." 丢弃工作区的改动)

#

# 修改:      readme.txt

#

修改尚未加入提交(使用 "git add" /"git commit -a"

1) 第一次修改提交代码:git 将代码文件提交到本地git版本数据库,此时会在暂存区生产一个快照版本

2) 第二次修改提交代码:当再次修改代码是,需要重新提交到暂存区,此时还会生产一个快照版本

3) 提交代码:只有将暂存区的代码提交到Git版本数据库才能算真正提交

查看当前文件内容与Git版本数据库中的差别

[root@client linuxprobe]# git diff readme.txt

diff --git a/readme.txt b/readme.txt

index cb06697..33d16d0 100644

--- a/readme.txt

+++ b/readme.txt

@@ -1 +1,2 @@

 Initialization Git repository

+Something not important

现在把文件提交Git版本数据库吧

[root@client linuxprobe]# git add readme.txt

再来查看下当前git版本仓库的状态

[root@client linuxprobe]# git commit -m "added a line of words"

[master 55930eb] added a line of words

 1 file changed, 1 insertion(+)

[root@client linuxprobe]# git status

# 位于分支 master

无文件要提交,干净的工作区

但是这次的操作还是只将文件提交到了本地的git仓库,并没有推送带远程的Git服务器

,所有需要定义远程的git服务器

[root@client linuxprobe]# git remote add server root@192.168.30.25:/root/linuxprobe.git

将文件提交到远程git服务器

[root@client linuxprobe]# git push -u server master

Counting objects: 6, done.

Compressing objects: 100% (3/3), done.

Writing objects: 100% (6/6), 517 bytes | 0 bytes/s, done.

Total 6 (delta 0), reused 0 (delta 0)

To root@192.168.30.25:/root/linuxprobe.git

 * [new branch]      master -> master

分支 master 设置为跟踪来自 server 的远程分支 master

为了验证是否推送到了远程的git服务,可以换个目录在克隆一份版本仓库

[root@client linuxprobe]# mkdir test

[root@client linuxprobe]# cd test

[root@client test]# git clone root@192.168.30.25:/root/linuxprobe.git

正克隆到 'linuxprobe'...

remote: Counting objects: 6, done.

remote: Compressing objects: 100% (3/3), done.

remote: Total 6 (delta 0), reused 0 (delta 0)

接收对象中: 100% (6/6), done.

原文地址:https://www.cnblogs.com/zc1741845455/p/10935120.html