一,版本控制
1,版本控制简介
版本控制(Version control),软件产品开发过程中,项目文件代码保持一致性的一种方式。
版本控制系统(Version control system),是指能自动实现版本控制功能的一种软件实现平台,它是文件多版本状态共存的一种机制。
2,版本控制系统介绍
版本控制系统主要分为两类:集中式和分布式。
集中式开源版本控制软件的代表为:svn
分布式开源版本控制软件的代表为:git (目前的主流)

2.1 svn和git的区别(集中式和分布式的区别)
svn:
- 客户端连接到服务端(代码仓库)进行操作
- 一旦客户端和服务端网络不通,将不能进行正常工作
- 容易产生代码冲突问题
git:
- 客户端从服务端(代码仓库)克隆一份代码到本地,在本地进行操作
- 开发完成之后再将代码推送到服务端(代码仓库)
2.2 版本软件
git :分布式版本管理系统软件
gitlab:基于git的源码私有仓库解决方案
github:基于git的源码公有仓库解决方案
3,版本管理系统常见术语
3.1 基本术语
仓库:代码文件存放的位置。常见的有工作目录->缓存区->本地仓库->远程仓库
版本:项目开发过程中使用版本来记录代码在每个阶段的变化内容
分支:保存文件的一种方式,而且分支之间实现了资源隔离
3.2 操作术语
获取:从上级仓库中获取文件到当前位置
提交:将修改好的文件提交到上级仓库
合并:将某个仓库位置中的文件和本地位置中的文件合并到一起
冲突:合并的过程中,由于文件内容,语意方面的不一致,导致不能正常合并
解决:对冲突的场景进行人为干预,最终达到正常合并的条件
3.3 其他术语
hook:触发器,我们在做某件事情的时候,会自动触发已定义好的事件
锁定:对仓库中文件进行安全保护的一种权限设置
4,git安装及使用
4.1 git安装
[root@localhost ~]# yum install git -y
4.2 git命令帮助
[root@localhost ~]# git help <子命令>
4.2 git的使用
4.2.1 本地仓库的创建
本地仓库创建有两种方式:一种是从远程仓库克隆一个到本地。另外一种是在本地一个干净的工作目录下面初始化一个本地仓库
在本地初始化一个本地仓库:
[root@localhost git]# mkdir /data/git
[root@localhost git]# cd /data/git
[root@localhost git]# git init (初始化本地仓库,如果加上--bare参数,则标识创建的仓库是个远程仓库)
初始化空的 Git 版本库于 /data/git/.git/
get init初始化本地仓库后,会在当前目录下面创建一个.git目录,这个目录有git既定的目录结构。所有git需要的数据和资源都存放在这个目录中。
如果当前目录下面保存有现成的代码文件,就相当于直接把当前的代码文件置于git的管理了。
默认情况下,初始化的本地仓库是没有任何分支的
[root@localhost git]# tree .git/
.git/
├── branches # 代码分支
├── config # git config命令设置的配置信息
├── description # 显示对仓库的描述
├── HEAD #当前分支的标识符,一般指向refs/heads目录下的分支文件
├── 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 #存放git对象,是git最核心的文件目录
│ ├── info
│ └── pack
└── refs # 各个提交对象的标识文件
├── heads
└── tags
9 directories, 13 files
4.2.2 git权限体系
--system git配置是操作系统级别的(/etc/gitconfig 默认不存在)
--global git配置是用户级别的(~/.gitconfig 默认不存在)
--local git配置是目录级别的(dir/.git/config 存在,利如上面.git目录下面就由config文件)
--worktree
--file
4.2.3 git配置
local级别设置
[root@localhost .git]# cat config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[root@localhost .git]# git config --local user.name "whp"
[root@localhost .git]# git config --local user.email "whp@qq.com"
[root@localhost .git]# cat config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[user]
name = whp
email = whp@qq.com
global级别设置:
[root@localhost .git]# git config --global user.name "whp"
[root@localhost .git]# git config --global user.email "whp@qq.com"
[root@localhost .git]# cat /root/.gitconfig
[user]
name = whp
email = whp@qq.com
system级别设置:
[root@localhost .git]# git config --system user.name "whp"
[root@localhost .git]# git config --system user.email "whp@qq.com"
[root@localhost .git]# cat /etc/gitconfig
[user]
name = whp
email = whp@qq.com
清除信息命令:git config --unset --system|global|local user.name
[root@localhost .git]# git config --unset --system user.name
[root@localhost .git]# cat /etc/gitconfig
[user]
email = whp@qq.com
查看命令:
[root@localhost .git]# git config --list (查看全部设置)
user.email=whp@qq.com
user.name=whp
user.email=whp@qq.com
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
user.name=whp
user.email=whp@qq.com
[root@localhost .git]# git config --list --local(查看某个级别的设置)
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
user.name=whp
user.email=whp@qq.com
权限优先级local > global > system
4.4 git区域管理
 4.4.1 区域设置
工作目录:就是上面创建的/data/git目录
暂存区域: /data/git/.git/index
本地仓库:/data/git/.git/
远程仓库: 远程仓库地址
4.4.2 区域操作命令
git init(--bare) 创建本地仓库
git clone 从远程仓库获取文件到本地主机,包括所有配置信息
git pull 从远程仓库获取最新代码,合并到本地仓库
git fetch 从远程仓库获取最新代码,不合并到本地仓库
git push 将本地仓库的代码推送到远程仓库
git add 将工作目录中的文件添加到暂存区域
git commit 将文件从暂存区移动到本地仓库
git reset 将文件从本地仓库移动到暂存区或者工作目录
git checkout 将文件从暂存区移动到工作目录
git rm 移除指定文件
4.5 文件状态

untracked(未跟踪) -- 在工作目录下创建的新文件,此时还没有和本地代码仓库有任何关联。
unmodified -- 文件未修改(把文件从暂存区域推送到本地仓库)
modified -- 文件被修改(从本地仓库拉回到本地工作目录)
staged -- 文件提交到了本地仓库的暂存区域
[root@localhost git]# git status
# 位于分支 master
#
# 初始提交
#
无文件要提交(创建/拷贝文件并使用 "git add" 建立跟踪)
root@localhost git]# echo test > test.txt
[root@localhost git]# git status
# 位于分支 master
#
# 初始提交
#
# 未跟踪的文件:
# (使用 "git add <file>..." 以包含要提交的内容)
#
# test.txt
提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)
[root@localhost git]# git add . (git add 命令将本地工作目录中的文件提交到本地仓库暂存区域)
git add filename也可以将文件进行提交
[root@localhost git]# git status
# 位于分支 master
#
# 初始提交
#
# 要提交的变更:
# (使用 "git rm --cached <file>..." 撤出暂存区)
#
# 新文件: test.txt
#
[root@localhost git]# ls .git/ (可以看到.git目录下面多个了index文件)
branches config description HEAD hooks index info objects refs
撤销提交
[root@localhost git]# git rm --cached test.txt (从暂存区域把文件拉到本地工作目录)
rm 'test.txt'
[root@localhost git]# git status
# 位于分支 master
#
# 初始提交
#
# 未跟踪的文件:
# (使用 "git add <file>..." 以包含要提交的内容)
#
# test.txt
提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)
[root@localhost git]# git commit -m "first file" (git commit 命令把暂存区域中的文件提交到本地仓库中)
[master(根提交) fc7b76d] first file
1 file changed, 1 insertion(+)
create mode 100644 test.txt
[root@localhost git]# ls .git/objects/ (提交到本地仓库中后,可以看到在.git/objects/fc目录下面有相应的文件)
2b 9d fc info pack
[root@localhost git]# ls .git/objects/fc/
7b76db9b8395e27e1204b8b765eef59e7424e0
[root@localhost git]# git status
# 位于分支 master
无文件要提交,干净的工作区
修改文件后再提交
[root@localhost git]# echo xxxx >> test.txt (文件编辑时文件由本地仓库转移到本地工作目录)
[root@localhost git]# git status
# 位于分支 master
# 尚未暂存以备提交的变更:
# (使用 "git add <file>..." 更新要提交的内容)
# (使用 "git checkout -- <file>..." 丢弃工作区的改动)
#
# 修改: test.txt
#
修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
[root@localhost git]# git commit -a -m "v2" (git commit -a 命令是直接把本地工作目录中的文件推送到本地仓库)
[master 3d7b33d] v2
1 file changed, 1 insertion(+)
[root@localhost git]# git ls-files --stage (显示暂存区域中的文件)
100644 5a00129e930a2a5a78abe4baf6fd8b5a8d891ac0 0 test.txt
[root@localhost git]# git cat-file -p 5a00129e930a2a5a78abe4baf6fd8b5a8d891ac0 (查看暂存区域中文件的内容)
test
xxxx
[root@localhost git]# git log
commit 3d7b33d93306a1a69e7488c80ae63bcc32b3205a
Author: whp <whp@qq.com>
Date: Mon Oct 25 22:37:00 2021 +0800
v2
commit fc7b76db9b8395e27e1204b8b765eef59e7424e0
Author: whp <whp@qq.com>
Date: Mon Oct 25 22:29:29 2021 +0800
first file
对比文件修改的内容
[root@localhost git]# git diff test.txt
diff --git a/test.txt b/test.txt
index 5a00129..95dd6a3 100644
--- a/test.txt #修改前的文件
+++ b/test.txt #修改后的文件
@@ -1,2 +1,3 @@
test
xxxx
+yyyyy # 修改的内容
4.5 log命令详解
git log [options]
常见参数:
[root@localhost git]# git log
commit 00a3c641d9d1199c8321d493080785947e10689b
Author: whp <whp@qq.com>
Date: Mon Oct 25 22:51:41 2021 +0800
v3
commit 3d7b33d93306a1a69e7488c80ae63bcc32b3205a
Author: whp <whp@qq.com>
Date: Mon Oct 25 22:37:00 2021 +0800
v2
commit fc7b76db9b8395e27e1204b8b765eef59e7424e0
Author: whp <whp@qq.com>
Date: Mon Oct 25 22:29:29 2021 +0800
first file
-p -n :详细显示每次提交的内容变化
[root@localhost git]# git log -p (git log -p 比git log命令更详细的显示了每个版本变动的内容)
commit 00a3c641d9d1199c8321d493080785947e10689b
Author: whp <whp@qq.com>
Date: Mon Oct 25 22:51:41 2021 +0800
v3
diff --git a/test.txt b/test.txt
index 5a00129..8a69339 100644
--- a/test.txt
+++ b/test.txt
@@ -1,2 +1,3 @@
test
xxxx
+yydyy
commit 3d7b33d93306a1a69e7488c80ae63bcc32b3205a
Author: whp <whp@qq.com>
Date: Mon Oct 25 22:37:00 2021 +0800
v2
diff --git a/test.txt b/test.txt
index 9daeafb..5a00129 100644
--- a/test.txt
+++ b/test.txt
@@ -1 +1,2 @@
test
+xxxx
--stat 显示简要的增改行数统计
[root@localhost git]# git log --stat
commit 00a3c641d9d1199c8321d493080785947e10689b
Author: whp <whp@qq.com>
Date: Mon Oct 25 22:51:41 2021 +0800
v3
test.txt | 1 +
1 file changed, 1 insertion(+)
commit 3d7b33d93306a1a69e7488c80ae63bcc32b3205a
Author: whp <whp@qq.com>
Date: Mon Oct 25 22:37:00 2021 +0800
v2
test.txt | 1 +
1 file changed, 1 insertion(+)
commit fc7b76db9b8395e27e1204b8b765eef59e7424e0
Author: whp <whp@qq.com>
Date: Mon Oct 25 22:29:29 2021 +0800
first file
test.txt | 1 +
1 file changed, 1 insertion(+)
--pretty onelin|short|full|fuller
[root@localhost git]# git log --pretty=oneline
00a3c641d9d1199c8321d493080785947e10689b v3
3d7b33d93306a1a69e7488c80ae63bcc32b3205a v2
fc7b76db9b8395e27e1204b8b765eef59e7424e0 first file
[root@localhost git]# git log --pretty=short
commit 00a3c641d9d1199c8321d493080785947e10689b
Author: whp <whp@qq.com>
v3
commit 3d7b33d93306a1a69e7488c80ae63bcc32b3205a
Author: whp <whp@qq.com>
v2
commit fc7b76db9b8395e27e1204b8b765eef59e7424e0
Author: whp <whp@qq.com>
first file
[root@localhost git]# git log --pretty=full
commit 00a3c641d9d1199c8321d493080785947e10689b
Author: whp <whp@qq.com>
Commit: whp <whp@qq.com>
v3
commit 3d7b33d93306a1a69e7488c80ae63bcc32b3205a
Author: whp <whp@qq.com>
Commit: whp <whp@qq.com>
v2
commit fc7b76db9b8395e27e1204b8b765eef59e7424e0
Author: whp <whp@qq.com>
Commit: whp <whp@qq.com>
first file
[root@localhost git]# git log --pretty=fuller
commit 00a3c641d9d1199c8321d493080785947e10689b
Author: whp <whp@qq.com>
AuthorDate: Mon Oct 25 22:51:41 2021 +0800
Commit: whp <whp@qq.com>
CommitDate: Mon Oct 25 22:51:41 2021 +0800
v3
commit 3d7b33d93306a1a69e7488c80ae63bcc32b3205a
Author: whp <whp@qq.com>
AuthorDate: Mon Oct 25 22:37:00 2021 +0800
Commit: whp <whp@qq.com>
CommitDate: Mon Oct 25 22:37:00 2021 +0800
v2
commit fc7b76db9b8395e27e1204b8b765eef59e7424e0
Author: whp <whp@qq.com>
AuthorDate: Mon Oct 25 22:29:29 2021 +0800
Commit: whp <whp@qq.com>
CommitDate: Mon Oct 25 22:29:29 2021 +0800
first file
4.6,提交撤销的文件
撤销动作根据对象不同,分为文件级别撤销,仓库级别撤销
4.6.1 文件级别撤销
覆盖式撤销:
git commit --amend -m "注释"
--amend的作用就是在提交代码的时候,将本地提交的commit记录覆盖上一次的commit记录
指定文件撤销
git reset <commit_it> <file_name1>...<file_name2>
[root@localhost git]# git reset 6d6a854e28 bb.txt
[root@localhost git]# git log -2
commit 180a14212fcb6c75cc993388fabd63d99849493c
Author: whp <whp@qq.com>
Date: Tue Oct 26 18:29:22 2021 +0800
暂存区撤销提交
git rm ---cached filename
|