git-jenkins阶段01 DevOps介绍, 版本控制系统, Git的安装与应用(内容对比,历史记录,恢复,分支)
铁三角
开发 测试 运维
老板的想法 产品经理的构造 开发的代码实现 测试的功能测试 运维平台构建 代码的上线
开发 测试 变化 代码的更新
运维 稳定 网站能够正常运行下去
vcs
记录文件的所有的历史变化
随时可以恢复到任何一个历史状态
多人进行协作开发
常见的版本管理工具
Git
SVN 集中式的版本控制(SVN公司管理)
#环境准备(需要2g内存) [root@git ~]# cat /etc/redhat-release CentOS Linux release 7.6.1810 (Core) [root@git ~]# uname -r 3.10.0-957.el7.x86_64 [root@git ~]# getenforce Disabled [root@git ~]# iptables-save #命令执行没有输出结果,表示防火墙是关闭的 [root@git ~]# #安装 [root@git ~]# yum install -y git [root@git ~]# git config usage: git config [options] Config file location --global use global config file #全局 --system use system config file #系统级别配置 --local use repository config file #使用版本库级别配置 #配置git使用用户 [root@git ~]# git config --global user.name "qls" [root@git ~]# git config --global user.email "123@qq.com" #配置用户的邮箱 #配置全局,语法高亮 [root@git ~]# git config --global color.ui true #显示配置列表 [root@git ~]# git config --list user.name=qls user.email=123@qq.com color.ui=true #此时会多出.gitconfig, 就是git的配置文件 [root@git ~]# ll -a total 40 ... -rw-r--r-- 1 root root 58 Jun 4 00:34 .gitconfig #查看配置文件 [root@git ~]# cat .gitconfig [user] name = qls email = 123@qq.com [color] ui = true #Git初始化 #创建工作目录 [root@git ~]# mkdir git_data [root@git ~]# cd git_data/ [root@git git_data]# ll total 0 [root@git git_data]# git init #初始化工作目录 Initialized empty Git repository in /root/git_data/.git/ [root@git git_data]# ll .git/ total 12 drwxr-xr-x 2 root root 6 Jun 4 00:40 branches #分支目录 -rw-r--r-- 1 root root 92 Jun 4 00:40 config #特有的配置选项 -rw-r--r-- 1 root root 73 Jun 4 00:40 description #Git web程序使用的 -rw-r--r-- 1 root root 23 Jun 4 00:40 HEAD #指示当前的分支 drwxr-xr-x 2 root root 242 Jun 4 00:40 hooks #Git的钩子文件 drwxr-xr-x 2 root root 21 Jun 4 00:40 info #全局排除文件(exclude文件) drwxr-xr-x 4 root root 30 Jun 4 00:40 objects #存放所有数据 info pack drwxr-xr-x 4 root root 31 Jun 4 00:40 refs #存放指向数据分支的提交的对象的指针 index #保存暂存区信息(非文件) [root@git git_data]# git status #查看工作区的状态 # On branch master # # Initial commit # nothing to commit (create/copy files and use "git add" to track)
git的一家子
工作目录 暂存区域 本地仓库 远程仓库
Git的四种状态 Untracked 为跟踪 Unmodified 未修改 Modified 已修改 Staged 已暂存 Git的基础命令 [root@git git_data]# git status #显示当前工作区的状态 # On branch master # # Initial commit # nothing to commit (create/copy files and use "git add" to track) #创建一些测试文件 [root@git git_data]# touch test.txt [root@git git_data]# touch oldboy.txt [root@git git_data]# touch oldgirl.txt [root@git git_data]# git status # On branch master # # Initial commit # # Untracked files: #发现未跟踪的文件 # (use "git add <file>..." to include in what will be committed) # # oldboy.txt # oldgirl.txt # test.txt nothing added to commit but untracked files present (use "git add" to track) #文件提交到暂存区 [root@git git_data]# git add test.txt #将文件提交到暂存区 [root@git git_data]# git status # On branch master # # Initial commit # # Changes to be committed: # (use "git rm --cached <file>..." to unstage) #可以删除暂存区下该文件 # # new file: test.txt # # Untracked files: # (use "git add <file>..." to include in what will be committed) # # oldboy.txt # oldgirl.txt [root@git git_data]# git add . #添加所有文件到暂存区 或者使用 git add * [root@git git_data]# git status # On branch master # # Initial commit # # Changes to be committed: # (use "git rm --cached <file>..." to unstage) # # new file: oldboy.txt # new file: oldgirl.txt # new file: test.txt [root@git git_data]# git rm --cached test.txt #从暂存区将文件删除 rm 'test.txt' [root@git git_data]# git status # On branch master # # Initial commit # # Changes to be committed: # (use "git rm --cached <file>..." to unstage) # # new file: oldboy.txt # new file: oldgirl.txt # # Untracked files: # (use "git add <file>..." to include in what will be committed) # # test.txt [root@git git_data]# rm -rf test.txt #删除工作目录中的文件 [root@git git_data]# git status # On branch master # # Initial commit # # Changes to be committed: # (use "git rm --cached <file>..." to unstage) # # new file: oldboy.txt # new file: oldgirl.txt #删除文件 两种方法 第一种 1.先删除暂存区里面的文件,再删除工作目录中的文件 git rm --cached test.txt rm -rf test.txt 2.直接从暂存区连同工作目录中的文件删除 [root@git git_data]# git rm -f oldgirl.txt rm 'oldgirl.txt' [root@git git_data]# git status # On branch master # # Initial commit # # Changes to be committed: # (use "git rm --cached <file>..." to unstage) # # new file: oldboy.txt # [root@git git_data]# ls oldboy.txt #从暂存区提交到本地仓库 -m 注释信息 [root@git git_data]# git commit -m "new 3 file" [master (root-commit) 220403a] new 3 file 3 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 a.txt create mode 100644 b.txt create mode 100644 oldboy.txt [root@git git_data]# git status # On branch master nothing to commit, working directory clean #文件重命名 两种方法 1.本地重命名,修改工作目录中文件的名称 [root@git git_data]# mv a.txt aaa.txt [root@git git_data]# git status # On branch master # Changes not staged for commit: # (use "git add/rm <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory)#通过暂存区覆盖回来 # # deleted: a.txt # # Untracked files: # (use "git add <file>..." to include in what will be committed) # # aaa.txt no changes added to commit (use "git add" and/or "git commit -a") 2.删除暂存区中的文件 [root@git git_data]# git rm --cached a.txt rm 'a.txt' [root@git git_data]# git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # deleted: a.txt # # Untracked files: # (use "git add <file>..." to include in what will be committed) # # aaa.txt 3.将重命名好的文件提交到暂存区 [root@git git_data]# git add . [root@git git_data]# git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # renamed: a.txt -> aaa.txt 4.提交到本地仓库 [root@git git_data]# git commit -m "mv a.txt aaa.txt" [master 1c67b18] mv a.txt aaa.txt 1 file changed, 0 insertions(+), 0 deletions(-) rename a.txt => aaa.txt (100%) 第二种方法 直接使用git进行重命名 [root@git git_data]# git mv b.txt bbb.txt [root@git git_data]# git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # renamed: b.txt -> bbb.txt # 提交到本地仓库 [root@git git_data]# git commit -m "mv b.txt bbb.txt" [master 87498ce] mv b.txt bbb.txt 1 file changed, 0 insertions(+), 0 deletions(-) rename b.txt => bbb.txt (100%) #文件内容比对 #比对工作目录与暂存区文件内容不同之处 [root@git git_data]# echo "1111111111111" >> oldboy.txt [root@git git_data]# git diff oldboy.txt diff --git a/oldboy.txt b/oldboy.txt index e69de29..a5fdf3a 100644 --- a/oldboy.txt +++ b/oldboy.txt @@ -0,0 +1 @@ +1111111111111 [root@git git_data]# git add oldboy.txt #将文件提交到暂存区 [root@git git_data]# git diff oldboy.txt #再次进行比对发现,暂存区域工作目录的内容相同 #比对暂存区文件内容和本地仓库文件内容不同之处 [root@git git_data]# git diff --cached oldboy.txt diff --git a/oldboy.txt b/oldboy.txt index e69de29..a5fdf3a 100644 --- a/oldboy.txt +++ b/oldboy.txt @@ -0,0 +1 @@ +1111111111111 [root@git git_data]# git commit -m "modify oldboy.txt" #提交到本地仓库 [master 91d1614] modify oldboy.txt 1 file changed, 1 insertion(+) [root@git git_data]# git diff --cached oldboy.txt [root@git git_data]# echo "222222" >> oldboy.txt #同时提交暂存区和本地仓库 [root@git git_data]# git commit -am "modify oldboy.txt 2" [master a88a12f] modify oldboy.txt 2 1 file changed, 1 insertion(+) #查看Git的历史操作记录 [root@git git_data]# git log commit a88a12fc0ef8e6a525c9a8e137a246f5f8459524 Author: qls <123@qq.com> Date: Fri Jun 7 04:34:28 2024 +0800 modify oldboy.txt 2 #使用一行来显示Git的历史记录 [root@git git_data]# git log --oneline a88a12f modify oldboy.txt 2 91d1614 modify oldboy.txt 87498ce mv b.txt bbb.txt 1c67b18 mv a.txt aaa.txt 220403a new 3 file #decorate 显示当前指针所指向的分支 [root@git git_data]# git log --oneline --decorate a88a12f (HEAD, master) modify oldboy.txt 2 91d1614 modify oldboy.txt 87498ce mv b.txt bbb.txt 1c67b18 mv a.txt aaa.txt 220403a new 3 file #显示具体内容的变化 [root@git git_data]# git log -p commit a88a12fc0ef8e6a525c9a8e137a246f5f8459524 Author: qls <123@qq.com> Date: Fri Jun 7 04:34:28 2024 +0800 modify oldboy.txt 2 diff --git a/oldboy.txt b/oldboy.txt index a5fdf3a..c217021 100644 --- a/oldboy.txt +++ b/oldboy.txt @@ -1 +1,2 @@ 1111111111111 +222222 ... #显示最近的一次记录(可修改次数) [root@git git_data]# git log -1 commit a88a12fc0ef8e6a525c9a8e137a246f5f8459524 Author: qls <123@qq.com> Date: Fri Jun 7 04:34:28 2024 +0800 modify oldboy.txt 2 #恢复历史数据 1.改变了工作区中文件的内容,发现改错了(或者文件被误删了) [root@git git_data]# echo "44444" >> oldboy.txt [root@git git_data]# git status # On branch master # Changes not staged for commit: # (use "git add <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # modified: oldboy.txt # no changes added to commit (use "git add" and/or "git commit -a") #将暂存区里面的内容覆盖到工作目录(被误删后也可以拉回来) [root@git git_data]# git checkout -- oldboy.txt #不加--也可以 [root@git git_data]# git status # On branch master nothing to commit, working directory clean [root@git git_data]# cat oldboy.txt 1111111111111 222222 2.工作目录和暂存区都发生了改变,没有提交到本地仓库,发现改错了 [root@git git_data]# echo "444444" >>oldboy.txt [root@git git_data]# git add . [root@git git_data]# git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) #将本地仓库恢复到暂存区 # # modified: oldboy.txt # #比对本地仓库和暂存区的不同 [root@git git_data]# git diff --cached oldboy.txt diff --git a/oldboy.txt b/oldboy.txt index c217021..54bb024 100644 --- a/oldboy.txt +++ b/oldboy.txt @@ -1,2 +1,3 @@ 1111111111111 222222 +444444 #从本地仓库覆盖到暂存区 [root@git git_data]# git reset HEAD oldboy.txt Unstaged changes after reset: M oldboy.txt [root@git git_data]# git diff --cached oldboy.txt [root@git git_data]# cat oldboy.txt 1111111111111 222222 444444 #从暂存区覆盖工作目录 [root@git git_data]# git checkout -- oldboy.txt [root@git git_data]# cat oldboy.txt 1111111111111 222222 3.修改了工作区,暂存区,也提交到了本地仓库,发现写错了 [root@git git_data]# echo "44444" >> oldboy.txt [root@git git_data]# git add . [root@git git_data]# git commit -m "modify oldboy.txt 4" [master 09308a3] modify oldboy.txt 4 1 file changed, 1 insertion(+) [root@git git_data]# git status # On branch master nothing to commit, working directory clean #通过历史记录来恢复数据 [root@git git_data]# git log --oneline 09308a3 modify oldboy.txt 4 a88a12f modify oldboy.txt 2 91d1614 modify oldboy.txt 87498ce mv b.txt bbb.txt 1c67b18 mv a.txt aaa.txt 220403a new 3 file [root@git git_data]# git status # On branch master nothing to commit, working directory clean [root@git git_data]# git reset --hard a88a12f #将所有地方的数据恢复到这个快照 HEAD is now at a88a12f modify oldboy.txt 2 [root@git git_data]# cat oldboy.txt 1111111111111 222222 [root@git git_data]# git diff oldboy.txt [root@git git_data]# git diff --cached oldboy.txt #发现刚才恢复错了 [root@git git_data]# git log --oneline #查看日志时,发现没有了modify 4那一次的修改了 a88a12f modify oldboy.txt 2 91d1614 modify oldboy.txt 87498ce mv b.txt bbb.txt 1c67b18 mv a.txt aaa.txt 220403a new 3 file [root@git git_data]# git reflog #显示所有的git历史记录 a88a12f HEAD@{0}: reset: moving to a88a12f 09308a3 HEAD@{1}: commit: modify oldboy.txt 4 a88a12f HEAD@{2}: commit: modify oldboy.txt 2 91d1614 HEAD@{3}: commit: modify oldboy.txt 87498ce HEAD@{4}: commit: mv b.txt bbb.txt 1c67b18 HEAD@{5}: commit: mv a.txt aaa.txt 220403a HEAD@{6}: commit (initial): new 3 file [root@git git_data]# git reset --hard 09308a3 HEAD is now at 09308a3 modify oldboy.txt 4 [root@git git_data]# cat oldboy.txt 1111111111111 222222 44444 #git分支 #显示你当前的指针指向哪个分支 [root@git git_data]# git log --oneline --decorate 09308a3 (HEAD, master) modify oldboy.txt 4 a88a12f modify oldboy.txt 2 91d1614 modify oldboy.txt 87498ce mv b.txt bbb.txt 1c67b18 mv a.txt aaa.txt 220403a new 3 file #创建分支 [root@git git_data]# git branch test #显示当前所在的分支 [root@git git_data]# git branch * master #*指向当前所在的分支 test #切换分支 [root@git git_data]# git checkout test Switched to branch 'test' [root@git git_data]# git branch master * test [root@git git_data]# touch test.txt [root@git git_data]# git add . [root@git git_data]# git commit -m "test commit" [test d4c9af7] test commit 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 test.txt [root@git git_data]# git log --oneline --decorate d4c9af7 (HEAD, test) test commit 09308a3 (master) modify oldboy.txt 4 a88a12f modify oldboy.txt 2 91d1614 modify oldboy.txt 87498ce mv b.txt bbb.txt 1c67b18 mv a.txt aaa.txt 220403a new 3 file [root@git git_data]# git checkout master #切换到master分支 Switched to branch 'master' [root@git git_data]# ll total 4 -rw-r--r-- 1 root root 0 Jun 7 02:57 aaa.txt -rw-r--r-- 1 root root 0 Jun 7 02:57 bbb.txt -rw-r--r-- 1 root root 27 Jun 7 08:14 oldboy.txt [root@git git_data]# git branch * master test #合并分支 [root@git git_data]# git merge test Updating 09308a3..d4c9af7 Fast-forward test.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 test.txt [root@git git_data]# ll #合并分支之后,test创建的文件就显示出来了 total 4 -rw-r--r-- 1 root root 0 Jun 7 02:57 aaa.txt -rw-r--r-- 1 root root 0 Jun 7 02:57 bbb.txt -rw-r--r-- 1 root root 27 Jun 7 08:14 oldboy.txt -rw-r--r-- 1 root root 0 Jun 7 21:40 test.txt [root@git git_data]# git log --oneline --decorate d4c9af7 (HEAD, test, master) test commit 09308a3 modify oldboy.txt 4 a88a12f modify oldboy.txt 2 91d1614 modify oldboy.txt 87498ce mv b.txt bbb.txt 1c67b18 mv a.txt aaa.txt 220403a new 3 file #子分支上,合并主分支内容 [root@git git_data]# touch master.txt [root@git git_data]# ll total 4 -rw-r--r-- 1 root root 0 Jun 7 02:57 aaa.txt -rw-r--r-- 1 root root 0 Jun 7 02:57 bbb.txt -rw-r--r-- 1 root root 0 Jun 7 21:44 master.txt -rw-r--r-- 1 root root 27 Jun 7 08:14 oldboy.txt -rw-r--r-- 1 root root 0 Jun 7 21:40 test.txt [root@git git_data]# git add . [root@git git_data]# git commit -m "master touch master.txt" [master b912bb0] master touch master.txt 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 master.txt [root@git git_data]# git checkout test Switched to branch 'test' [root@git git_data]# ll total 4 -rw-r--r-- 1 root root 0 Jun 7 02:57 aaa.txt -rw-r--r-- 1 root root 0 Jun 7 02:57 bbb.txt -rw-r--r-- 1 root root 27 Jun 7 08:14 oldboy.txt -rw-r--r-- 1 root root 0 Jun 7 21:40 test.txt [root@git git_data]# git merge master #合并主分支 Updating d4c9af7..b912bb0 Fast-forward master.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 master.txt [root@git git_data]# ll total 4 -rw-r--r-- 1 root root 0 Jun 7 02:57 aaa.txt -rw-r--r-- 1 root root 0 Jun 7 02:57 bbb.txt -rw-r--r-- 1 root root 0 Jun 7 21:45 master.txt -rw-r--r-- 1 root root 27 Jun 7 08:14 oldboy.txt -rw-r--r-- 1 root root 0 Jun 7 21:40 test.txt #合并冲突 [root@git git_data]# git checkout master Switched to branch 'master' [root@git git_data]# git branch * master test [root@git git_data]# echo "aaaa" >>oldboy.txt [root@git git_data]# git commit -am "modify master" [master b5bf6fd] modify master 1 file changed, 1 insertion(+) [root@git git_data]# git checkout test Switched to branch 'test' [root@git git_data]# echo "bbbb" >>oldboy.txt [root@git git_data]# git commit -am "modify test" [test c22a40f] modify test 1 file changed, 1 insertion(+) [root@git git_data]# git checkout master Switched to branch 'master' [root@git git_data]# git merge test #合并发生冲突 Auto-merging oldboy.txt CONFLICT (content): Merge conflict in oldboy.txt Automatic merge failed; fix conflicts and then commit the result. [root@git git_data]# cat oldboy.txt 1111111111111 222222 44444 <<<<<<< HEAD #当前分支操作 aaaa ======= bbbb #test指针的操作 >>>>>>> test [root@git git_data]# git log --oneline --decorate b5bf6fd (HEAD, master) modify master b912bb0 master touch master.txt d4c9af7 test commit 09308a3 modify oldboy.txt 4 a88a12f modify oldboy.txt 2 91d1614 modify oldboy.txt 87498ce mv b.txt bbb.txt 1c67b18 mv a.txt aaa.txt 220403a new 3 file #手动修改冲突 [root@git git_data]# vim oldboy.txt 1111111111111 222222 44444 aaaa [root@git git_data]# git commit -am "commit modify oldboy.txt" [master 3f8051e] commit modify oldboy.txt #test分支 合并分支 [root@git git_data]# git checkout test Switched to branch 'test' [root@git git_data]# cat oldboy.txt 1111111111111 222222 44444 bbbb [root@git git_data]# git merge master Updating c22a40f..3f8051e Fast-forward oldboy.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) [root@git git_data]# cat oldboy.txt 1111111111111 222222 44444 aaaa #删除分支 [root@git git_data]# git checkout master Switched to branch 'master' [root@git git_data]# git branch -d test Deleted branch test (was 3f8051e). [root@git git_data]# git branch * master [root@git git_data]# git reflog 3f8051e HEAD@{0}: checkout: moving from test to master 3f8051e HEAD@{1}: merge master: Fast-forward c22a40f HEAD@{2}: checkout: moving from master to test 3f8051e HEAD@{3}: commit (merge): commit modify oldboy.txt b5bf6fd HEAD@{4}: checkout: moving from test to master c22a40f HEAD@{5}: commit: modify test b912bb0 HEAD@{6}: checkout: moving from master to test b5bf6fd HEAD@{7}: reset: moving to b5bf6fd f4e3d14 HEAD@{8}: commit: modify test #z这里改错了,在主分支修改了,做了一次回退 b5bf6fd HEAD@{9}: commit: modify master b912bb0 HEAD@{10}: checkout: moving from test to master b912bb0 HEAD@{11}: merge master: Fast-forward d4c9af7 HEAD@{12}: checkout: moving from master to test b912bb0 HEAD@{13}: commit: master touch master.txt d4c9af7 HEAD@{14}: merge test: Fast-forward 09308a3 HEAD@{15}: checkout: moving from test to master d4c9af7 HEAD@{16}: commit: test commit 09308a3 HEAD@{17}: checkout: moving from master to test 09308a3 HEAD@{18}: reset: moving to 09308a3 a88a12f HEAD@{19}: reset: moving to a88a12f 09308a3 HEAD@{20}: commit: modify oldboy.txt 4 a88a12f HEAD@{21}: commit: modify oldboy.txt 2 91d1614 HEAD@{22}: commit: modify oldboy.txt 87498ce HEAD@{23}: commit: mv b.txt bbb.txt 1c67b18 HEAD@{24}: commit: mv a.txt aaa.txt 220403a HEAD@{25}: commit (initial): new 3 file