1. 实验环境
操作系统:Centos7.4_64位
配置:2CPU+2G内存+50G硬盘,最好4G内存
软件包:最小化安装 关闭firewalld和selinux
主机名 | IP地址 |
---|---|
ci-node1 | 10.0.0.246 |
ci-node2 | 10.0.0.247 |
1.1 系统初始化
1、设置主机名解析
root@node1 ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
10.0.0.246 ci‐node1
10.0.0.247 ci‐node2
2、安装EPEL 仓库和常用命令
[root@node1 ~]# rpm ‐ivh http://mirrors.aliyun.com/epel/epel‐release‐latest‐7.noarch.rpm
[root@node1 ~]# yum install ‐y net‐tools vim lrzsz tree screen lsof wget ntpdate
3、关闭NetworkManager 和防火墙
[root@node1 ~]# systemctl disable firewalld
[root@node1 ~]# systemctl stop NetworkManager
4、关闭SELinux
[root@node1 ~]# vim /etc/sysconfig/selinux
SELINUX=disabled #修改为disabled
5、设置时间:
[root@node1 ~]# crontab ‐l
*/5 * * * * /usr/sbin/ntpdate time1.aliyun.com
6、更改时区
[root@node1 ~]# ln ‐sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
2. git安装
2.1 yum安装
Centos 下安装Git,默认在CentOS 下,我们可以通过yum 的方式来安装Git
[root@ci‐node1 ~]# yum install git -y
[root@ci-node1 tools]# git --version
git version 1.8.3.1
使用yum 安装的Git 的版本是1.8,版本较低,我们还可以通过源码编译的方式来安装Git 的最新版本。
2.2 源码编译安装
首先需要安装依赖的库:
[root@ci‐node1 ~]# yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel gcc perl-ExtUtils-MakeMaker -y
下载最新的源码包
#先卸载centos7自带的git版本
[root@ci-node1 git-2.9.5]# git --version
git version 1.8.3.1
[root@ci-node1 ~]# yum remove git 或者把自带的命令删除了 rm ‐rf /usr/bin/git
[root@ci-node1 ~]# mkdir -p /server/tools
[root@ci-node1 ~]# cd /server/tools/
[root@ci‐node1 tools]# wget https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.9.5.tar.gz
[root@ci‐node1 tools]# tar xf git-2.9.5.tar.gz
[root@ci‐node1 tools]# cd git‐2.9.5
[root@ci‐node1 git‐2.9.5]# make prefix=/usr/local/git all
[root@ci‐node1 git‐2.9.5]# make prefix=/usr/local/git install
[root@ci‐node1 git‐2.9.5]# ln ‐s /usr/local/git/bin/git /usr/bin/git #软链接好像报错,那就添加环境变量
[root@ci-node1 git-2.9.5]# echo 'export PATH=$PATH:/usr/local/git/bin' >> /etc/profile
[root@ci-node1 git-2.9.5]# tail -1 /etc/profile
export PATH=$PATH:/usr/local/git/bin
[root@ci-node1 git-2.9.5]# source /etc/profile
[root@ci-node1 git-2.9.5]# git --version
git version 2.9.5
2.3 Git配置
Git的配置从上到下分为三层system(系统)/global(全局或用户)/local(仓库),使用三个不同参数进行设置,每个层次的配置存储在不同的位置,
1)/etc/gitconfig 文件:包含了适用于系统所有用户和所有库的值。如果你传递参数选项’–system’ 给git config,它将明确的读和写这个文件。
2)~/.gitconfig 文件:具体到你的用户。你可以通过传递–global 选项使Git 读或写这个特定的文件。
3)位于git 目录的config 文件(也就是.git/config) :无论你当前在用的库是什么,特定指向该单一的库。每个级别重写前一个级别的值。因此,在.git/config 中的值覆盖了在/etc/gitconfig 中的同一个值。
这里使用用户配置
[root@ci-node1 ~]# git config --global user.name luohong #配置git使用用户
[root@ci-node1 ~]# git config --global user.email 543613429@qq.com #配置git使用邮箱
[root@ci-node1 ~]# git config --global color.ui true #配置颜色
使用–global参数配置后,会在家目录下生成一个.gitconfig文件
[root@ci-node1 ~]# ls .gitconfig
.gitconfig
[root@ci-node1 ~]# cat .gitconfig
[user]
name = luohong
email = 543613429@qq.com
[color]
ui = true
[root@ci-node1 ~]# git config --list
user.name=luohong
user.email=543613429@qq.com
color.ui=true
通常我们只配置global 级别,使用其他级别会在相应目录生成配置文件。
2.4 Git常用命令
- add #添加文件内容至索引
- bisect #通过二分查找定位引入 bug 的变更
- branch #列出、创建或删除分支
- checkout #检出一个分支或路径到工作区
- clone #克隆一个版本库到一个新目录
- commit #记录变更到版本库
- diff #显示提交之间、提交和工作区之间等的差异
- fetch #从另外一个版本库下载对象和引用
- grep #输出和模式匹配的行
- init #创建一个空的 Git 版本库或重新初始化一个已存在的版本库
- log #显示提交日志
- merge #合并两个或更多开发历史
- mv #移动或重命名一个文件、目录或符号链接
- pull #获取并合并另外的版本库或一个本地分支
- push #更新远程引用和相关的对象
- rebase #本地提交转移至更新后的上游分支中
- reset #重置当前HEAD到指定状态
- rm #从工作区和索引中删除文件
- show #显示各种类型的对象
- status #显示工作区状态
- tag #创建、列出、删除或校验一个GPG签名的 tag 对象
2.5 Git 仓库初始化
2.5.1 建立仓库
创建一个空白仓库,这是后续操作的前提,我们可以简单的把工作目录理解成是一个被Git服务程序管理的目录,Git会时刻的追踪目录内文件的改动。
[root@ci-node1 ~]# mkdir git_test
[root@ci-node1 ~]# cd git_test/
[root@ci-node1 git_test]# git init #初始化为git工作目录
Initialized empty Git repository in /root/git_test/.git/
空仓库创建完成后git_test 文件夹下会生成一个.git 隐藏文件夹。仓库默认包含一个主支,即master,默认操作都是在主分支master 上进行的。
[root@ci-node1 git_test]# ll -a
total 0
drwxr-xr-x 3 root root 18 Aug 31 20:39 .
dr-xr-x---. 4 root root 197 Aug 31 20:38 ..
drwxr-xr-x 7 root root 119 Aug 31 20:39 .git
#.get目录就是仓库
#查看目录
[root@ci-node1 git_test]# cd .git/
[root@ci-node1 .git]# tree
.
├── branches
├── config
├── description
├── HEAD
├── hooks
│ ├── applypatch-msg.sample
│ ├── commit-msg.sample
│ ├── post-update.sample
│ ├── pre-applypatch.sample
│ ├── pre-commit.sample
│ ├── prepare-commit-msg.sample
│ ├── pre-push.sample
│ ├── pre-rebase.sample
│ └── update.sample
├── info
│ └── exclude
├── objects #仓库目录下数据具体存放位置
│ ├── info
│ └── pack
└── refs
├── heads
└── tags
2.5.2 设置过滤文件
有了仓库,我们便可以在git_test 文件夹下的工作区做文件增删修改工作了,但很多时候,我们只在意开发过程中的源文件,并不需要管理自动产生的其他临时文件。这时候我们便需要一个过滤文件,在这个文件中设置过滤规则,让Git 能够自动过滤掉那些临时文件,这个文件便是.gitignore
文件。
在仓库目录下创建.gitignore 文件
[root@ci-node1 git_test]# touch .gitignore
[root@ci-node1 git_test]# vim .gitignore
[root@ci-node1 git_test]# cat .gitignore
test.txt #过滤test.txt 文件
/test/ #过滤test 目录
*.txt #过滤所有以.txt 结尾的文件
常用的通配规则:
- 以斜杠“/”开头表示目录
- 以星号“*”通配多个字符
- 以问号“?”通配单个字符
- 以方括号“[]”包含单个字符的匹配列表
- 以叹号“!”表示不忽略(跟踪)匹配到的文件或目录
3.仓库基础操作
3.1 Git 的四个区域
- workspace:工作区(他持有实际文件)—相当于上面例子中本地创建的目录git_test
- Index/Stage/Cached:暂存区(它像一个结存区域,临时保存你的改动)一般存放在".git 目录下" 下的index 文件(.git/index)中,所以我们把暂存区有时也叫作索引(index),index文件会在第一次暂存后自动生成。
- Repository:本地仓库工作区有一个隐藏目录.git,这个不算工作区,而是Git 的版本库。
- Remote:远程仓库,比如gihub和gitlab
在正式使用前,我们还需要弄清楚Git的三种重要模式,分别是已提交、已修改、已暂存
- 已提交(committed):表示数据文件已经顺利提交到Git数据库中。
- 已修改(modified):表示数据文件已经被修改,但未被保存到Git数据库中。
- 已暂存(staged):表示数据文件已经被修改,并会在下次提交时提交到Git数据库中。
提交前的数据文件可能会被随意修改或丢失,但只要把文件快照顺利提交到Git数据库中,那就可以完全放心了,流程为:
1.在工作目录中修改数据文件。
2.将文件的快照放入暂存区域。
3.将暂存区域的文件快照提交到Git仓库中。
3.2 Git的四种状态
前面讲了Git 有四个区域,而单就文件改动状态层面而言,Git区域内的文件也有 4种状态(需要注意的是文件状态并不是与Git 区域一一对应的),这是Git 第二个重要概念。
- Untracked:新增的文件的状态,未受Git 管理,记录在工作区
- Modified:受Git 管理过的文件的改动状态(包括改动内容、删除文件),记录在工作区
- Staged:将记录在工作区的文件变动状态通知了Git,记录在暂存区
- Unmodified:受Git 管理中的文件状态(没有变动),记录在本地仓库/远程仓库
3.3 git status查看git仓库文件改动状态
Git 仓库内文件改动有 4 种状态,除了 Unmodified状态的文件因为并未改动默认没有状态不做显示之外,其他文件改动状态都可以通过git status 来查看。让我们开始在工作区创建a、b、c 三个文件。
新建文件
[root@ci-node1 git_test]# touch a
[root@ci-node1 git_test]# touch b
[root@ci-node1 git_test]# touch c
[root@ci-node1 git_test]# ll
total 0
-rw-r--r-- 1 root root 0 May 22 15:04 a
-rw-r--r-- 1 root root 0 May 22 15:04 b
-rw-r--r-- 1 root root 0 May 22 15:04 c
查看状态
[root@ci-node1 git_test]# git status
On branch master
Initial commit
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
a
b
c
nothing added to commit but untracked files present (use "git add" to track)
Git 仓库目录下的文件改动操作默认都发生在Git工作区内,Git 并不会主动管理。如果希望Git 能够管理这些变动,你需要主动通知Git。共有3 种通知Git 的命令(gitadd/rm/mv)
3.4 git add将工作区文件改动添加到暂存区
git add 是第一种通知Git 命令,这个命令用于告诉Git 我们新增了文件改动,被git add命令操作过的文件(改动)便会处于Git 暂存区。
[root@ci-node1 git_test]# git add a
[root@ci-node1 git_test]# git status
On branch master
Initial commit
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: a
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
b
c
此时提交到暂存区后自动生成了index文件
[root@ci-node1 git_test]# ll -a .git/
-rw-r--r-- 1 root root 96 Aug 31 21:14 index
同时我们可以使用git add .
或者git add *
一次将多个文件改动添加到暂存区。
[root@ci-node1 git_test]# git add .
[root@ci-node1 git_test]# git status
On branch master
Initial commit
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: .gitignore
new file: a
new file: b
new file: c
3.5 git rm将暂存区文件改动回退到工作区
Git rm 命令用于告诉Git,我们想把之前用git add 添加的文件改动从Git 暂存区里拿出去,不需要Git 记录了。拿出去有两种拿法,一种是从暂存区退回到工作区,另一种是直接丢弃文件改动。我们试着将b 退回到工作区,c 直接丢弃。
将b 的改动从暂存区拿到工作区
[root@ci-node1 git_test]# git rm --cached b
rm 'b'
[root@ci-node1 git_test]# git status
On branch master
Initial commit
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: .gitignore
new file: a
new file: c
Untracked files:
(use "git add <file>..." to include in what will be committed)
b
将c 文件直接从git 空间里删除,也是就是从暂存区和工作区都删除。
[root@ci-node1 git_test]# git rm -f c
rm 'c'
[root@ci-node1 git_test]# git status
On branch master
Initial commit
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: .gitignore
new file: a
Untracked files:
(use "git add <file>..." to include in what will be committed)
b
#当前目录已经没有c文件了,查看本地文件夹也没有
[root@ci-node1 git_test]# ll
total 0
-rw-r--r-- 1 root root 0 May 22 15:04 a
-rw-r--r-- 1 root root 0 May 22 15:04 b
3.6 git mv将暂存区文件移动位置或重命名
Git mv 命令用于告诉Git 我们想把之前用git add 添加的文件直接在暂存区里重新命名或移动到新位置。
将a 文件改名为a.txt
[root@ci-node1 git_test]# git mv a a.txt
[root@ci-node1 git_test]# ll
total 0
-rw-r--r-- 1 root root 0 May 22 15:04 a.txt
-rw-r--r-- 1 root root 0 May 22 15:04 b
[root@ci-node1 git_test]# git status
On branch master
Initial commit
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: .gitignore
new file: a.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
b
3.7 git commit提交git仓库
当我们在仓库工作区下完成了文件增删改操作之后,并且使用git add 将文件改动记录在暂存区之后,便可以开始将其提交到Git 本地仓库。
语法:git commit -m "描述信息"
将暂存区内的文件a.txt 提交到本地仓库
[root@ci-node1 git_test]# git commit -m "commit a"
[master (root-commit) 8e0ef95] commit a
2 files changed, 3 insertions(+)
create mode 100644 .gitignore
create mode 100644 a.txt
[root@ci-node1 git_test]# git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
b
nothing added to commit but untracked files present (use "git add" to track)
只有当提交到本地仓库后,才是真正意义上的被git管理。当出现下面的提示则是工作区、暂存区、本地仓库三者内容一致。
[root@ci-node1 git_test]# git status
# On branch master
nothing to commit, working directory clean
3.8 git diff查看Git 区域文件的具体改动
git status
只能让我们知道文件在Git 区域内的改动状态,但如果我们想查看某个文件内具体改了什么(也可以理解为在不同Git 区域中的差异),此时需要用git diff
命令。
- 对于b 文件,由于是新增的文件,其只存在于工作区,且处于Untracked 状态,Git 认为无论是哪两个Git 区域之间的比对都没有意义,得到的结果是没有区别。
- 而对于a.txt 文件,由于已经被提交到仓库了,处于Git 管理中,所以这个文件同时存在于三个Git 空间(工作区,暂存区,仓库),我们在工作区内对其进行了文件改动,无论是比对工作区vs 暂存区或者工作区vs 仓库,得到的结果应该都是a.txt 文件里的具体变化内容。而如果比对暂存区vs 仓库,得到的结果也应该是没有区别。
git diff 查看文件当前变动(工作区 vs 暂存区)
查看b 文件得不到任何结果
[root@ci-node1 git_test]# git diff b #b只存在工作区,暂存区和仓库都没有,比对没有意义
[root@ci-node1 git_test]# git diff a.txt #三个区域相同,也不会有变化
改变工作区a.txt内容
[root@ci-node1 git_test]# echo "test" >> a.txt
[root@ci-node1 git_test]# git diff a.txt
diff --git a/a.txt b/a.txt
index e69de29..9daeafb 100644
--- a/a.txt
+++ b/a.txt
@@ -0,0 +1 @@
+test #变化的内容
git diff --cached查看文件当前变动(暂存区 vs 本地仓库)
[root@ci-node1 git_test]# git add a.txt #将a.txt 文件的变动暂存后,查看变化
[root@ci-node1 git_test]# git diff a.txt #对比工作区与暂存区,无变化
[root@ci-node1 git_test]# git diff --cached a.txt #对比暂存区与本地仓库,可以看到具体变
diff --git a/a.txt b/a.txt
index e69de29..9daeafb 100644
--- a/a.txt
+++ b/a.txt
@@ -0,0 +1 @@
+test
3.9 git log查看历史提交
当我们在仓库里做了很多次提交之后,免不了需要回看提交记录,看看自己之前的改动。
有两种Git 命令可以帮我们查看记录:
- git log 是最直接的查看历史提交的命令,但是只能查看最终在仓库存在的提交信息,git log 可直接用也可带参数用,常用的有下面几种:
- 标准查看:git log
- 精简查看:git log --oneline
- 完整查看:git log -p
- git reflog 能查看最终在仓库存在的提交信息,已删除的提交及在本地具体Git 命令操作记录。
#标准查看:git log
#显示所有历史提交标准信息,每个提交信息包括SHA号,作者,时间以及标题
[root@ci-node1 git_test]# git log
commit 8e0ef956de4f41988d599e5856020c93436533d2
Author: luohong <543613429@qq.com>
Date: Fri May 22 15:22:26 2020 +0800
commit a
#精简查看git log --oneline
[root@ci-node1 git_test]# git log --oneline
8e0ef95 commit a
#完整查看git log -p
[root@ci-node1 git_test]# git log -p
commit 8e0ef956de4f41988d599e5856020c93436533d2
Author: luohong <543613429@qq.com>
Date: Fri May 22 15:22:26 2020 +0800
commit a
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..b9999cb
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,3 @@
+test.txt #过滤test.txt 文件
+/test/ #过滤test 目录
+*.txt #过滤所有以.txt 结尾的文件
diff --git a/a.txt b/a.txt
new file mode 100644
index 0000000..e69de29
[root@ci-node1 git_test]# git reflog
8e0ef95 HEAD@{0}: commit (initial): commit a
3.10 git checkout签出
如果仓库中已经存在文件a.txt,在工作区中对a.txt 修改了,如果想撤销可以使用checkout,签出覆盖。检出命令git checkout 是git 最常用的命令之一,同时也是一个很危险的命令,因为这条命令会重写工作区。
用法:git checkout --filename
用暂存区中filename 文件来覆盖工作区中的filename 文件。相当于取消自上次执行git add filename
以来(如果执行过)的本地修改。回退单个文件
[root@ci-node1 git_test]# cat a.txt
test
[root@ci-node1 git_test]# echo "111">>a.txt
[root@ci-node1 git_test]# cat a.txt
test
111
[root@ci-node1 git_test]# git checkout -- a.txt
[root@ci-node1 git_test]# cat a.txt
test
注意:git checkout .
这条命令最危险,相当于用暂存区的所有文件直接覆盖本地文件,不给用户任何确认的机会!
3.11 commit 撤销提交
原理就是放弃工作区和暂存区的改动,同时HEAD 指针指向前一个commit 对象。要通过git log 查看提交日志,也可直接指定提交编号或序号,相当于恢复快照
[root@ci-node1 git_test]# git log --oneline
8e0ef95 commit a
[root@ci-node1 git_test]# git reset --hard 8e0ef95
HEAD is now at 8e0ef95 commit a
[root@ci-node1 git_test]# cat a.txt #本来又test内容,现在已回退,就没有了
[root@ci-node1 git_test]#