【运维知识高级篇】一篇文章带你搞懂Git!(Git安装+全局配置+Git初始化代码仓库+Git四大区域+Git四种状态+Git常用命令+Git分支+Git测试代码回滚)

简介: 【运维知识高级篇】一篇文章带你搞懂Git!(Git安装+全局配置+Git初始化代码仓库+Git四大区域+Git四种状态+Git常用命令+Git分支+Git测试代码回滚)

版本流程控制系统(version control system)是一种记录一个或若干个文件内容变化,以便将来查阅特定版本内容情况的系统,它会记录文件的所有历史变化,我们可以随时恢复到任何一个历史状态,同时支持多人协作开发。


常见的版本管理工具

SVN,集中式的版本控制系统,只有一个中央数据仓库,如果中央数据仓库挂了或者不可访问,所有的使用者将无法使用SVN,无法进行提交或备份文件。

因为它是直接与CVN服务器进行交互,开发写好的代码上传到SVN服务器,也可以从SVN服务器上拉取下来目前最新的版本代码。

这样的方式并不好,我们在企业中一般采取分布式的版本控制系统,在每个使用者的电脑上都有一个完整的数据仓库,没有网络依旧可以使用Git,同时可以将本地数据同步到Git服务器或者Github等代码仓库,以便进行团队协作。

Git安装与全局配置

1、环境准备

1. [root@Gitlab ~]# cat /etc/redhat-release 
2. CentOS Linux release 7.9.2009 (Core)
3. [root@Gitlab ~]# uname -r
4. 3.10.0-1160.el7.x86_64
5. [root@Gitlab ~]# getenforce
6. Disabled
7. [root@Gitlab ~]# systemctl status firewalld
8. ● firewalld.service - firewalld - dynamic firewall daemon
9.    Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
10.    Active: inactive (dead)
11.      Docs: man:firewalld(1)

2、Git安装部署

1. [root@Gitlab ~]# rpm -qa|grep ^git     #Centos7.9默认已经安装好了
2. git-1.8.3.1-23.el7_8.x86_64
3. 
4. [root@Gitlab ~]# yum -y install git    #如果Linux系统版本低可以yum安装

3、全局配置

1. #git配置文件
2. [root@Gitlab ~]# git config
3. usage: git config [options]
4. 
5. Config file location
6.     --global              use global config file        #全局配置文件
7.     --system              use system config file        #系统级配置文件
8.     --local               use repository config file    #版本库级配置文件
9. ......
10. 
11. #git全局配置
12. [root@Gitlab ~]# git config --global user.name "koten"    #配置git使用用户
13. [root@Gitlab ~]# git config --global user.email "888888@qq.com"    #配置git使用邮箱
14. [root@Gitlab ~]# git config --global color.ui true    #配置语法高亮
15. [root@Gitlab ~]# git config --list    #列出刚刚配置的内容
16. user.name=koten
17. user.email=888888@qq.com
18. color.ui=true
19. [root@Gitlab ~]# cat .gitconfig       #全局配置实际是保存在了这个配置文件
20. [user]
21.   name = koten
22.   email = 888888@qq.com
23. [color]
24.   ui = true

Git初始化代码仓库

1. #初始化工作目录,不管里面有没有文件都可以进行初试化
2. [root@Gitlab ~]# mkdir git_data
3. [root@Gitlab ~]# cd git_data
4. [root@Gitlab git_data]# git init    #初始化git
5. Initialized empty Git repository in /root/git_data/.git/
6. [root@Gitlab git_data]# git status
7. # On branch master    #位于哪个分支
8. #    
9. # Initial commit      #初始提交
10. #
11. nothing to commit (create/copy files and use "git add" to track)    #无文件要提交(创建/拷贝文件并使用"git add"建立跟踪)
12. [root@Gitlab git_data]# ll -a
13. total 0
14. drwxr-xr-x  3 root root  18 May 18 15:11 .
15. dr-xr-x---. 4 root root 197 May 18 15:10 ..
16. drwxr-xr-x  7 root root 119 May 18 15:11 .git
17. [root@Gitlab gitdata]# ll .git/
18. total 12
19. drwxr-xr-x 2 root root   6 May 19 17:31 branches        #分支目录
20. -rw-r--r-- 1 root root  92 May 19 17:31 config          #定义项目特有的配置选项
21. -rw-r--r-- 1 root root  73 May 19 17:31 description     #仅供git web程序使用
22. -rw-r--r-- 1 root root  23 May 19 17:31 HEAD            #指示当前的分支
23. drwxr-xr-x 2 root root 242 May 19 17:31 hooks           #包含git钩子文件
24. drwxr-xr-x 2 root root  21 May 19 17:31 info            #包含一个全局排除文件(exclude文件)
25. drwxr-xr-x 4 root root  30 May 19 17:31 objects         #存放所有数据内容,有info和pack两个子文件夹
26. drwxr-xr-x 4 root root  31 May 19 17:31 refs            #存放指向数据(分支)的提交对象的指针
27. index    #哈希值的方式保存暂存区信息,在执行git init的时候还没有这个文件

Git常规使用

一、git四大区域

学git首先得认识git四大区域、工作区、暂存区、本地仓库、远程仓库,四大区域层层递进,需要一级一级往上提交最终实现项目存储,以下命令是几个区域交互文件时使用的命令

1. git add                    #工作区提交项目到暂存区
2. git commit                 #暂存区提交项目到本地仓库
3. git push                   #本地仓库提交项目到远程仓库
4. git rm                     #从Git的暂存区域移除文件、在工作区中将被跟踪文件删除
5. git reset HEAD             #取消已经暂存的文件,将其转换回未暂存的状态
6. git reset --hard 3de15d4   #将当前分支的HEAD指向commit哈希值为3de15d4的提交,并且重置Staging Area和工作目录,将它们恢复到该提交时的状态,恢复指定版本
7. git clone                  #远程仓库拉取到本地仓库
8. git pull                   #远程仓库中的代码更新到本地

二、git四种状态

在Git中,文件存在四种状态,分别为:

1、未跟踪(Untracked):这些是新创建的文件,或者已存在但从未被包含在版本控制中的文件。这些文件不受Git版本控制,可以使用 `git add` 命令将它们添加到暂存区。

2、已暂存(Staged):这些是已被 Git 记录并准备提交到版本库的文件。使用 `git add` 命令可以将未跟踪的文件添加到缓存区。

3、已修改(Modified):向已有文件追加内容、删除部分或修改现有内容后,文件会进入“已修改”状态。Git 知道文件已被修改,但它尚未记录更改。需要先将文件添加到暂存区,然后再通过`git commit` 命令提交变更。

4、已提交(Committed):表示文件的当前版本已经永久存储在本地 Git 仓库中。通过执行 `git commit` 命令后,所有已暂存的文件都会成为已提交状态。

三、git基础命令

1. git init         # 初始化目录
2. git status       # 查看仓库的状态
3. git add file     # 将代码提交带暂存区
4. git add .        # 将当前所有的文件提交到暂存区
5. git rm --cached  # 删除暂存区的文件
6. git commit -m "newfile test.txt" # 将暂存区的文件提交到本地仓库
7. git checkout -- test.txt         # 如果文件已经被添加到缓存区,将使该文件与暂存区保持一致(即恢复到最近一次提交时的状态),而如果文件处于修改状态但尚未添加到缓存区,则会将其还原为最近一次 Git 的状态。在所有情况下,Git 会强制覆盖当前工作目录中的指定文件。
8. git rm file                      # 删除文件
9. 注意: 提交新的代码或者删除或者修改名称都必须执行 git add file 和 git commit
10. git diff               # 比对的是工作目录和暂存区的不同
11. git diff --cached          # 比对的是暂存区和本地仓库的不同
12. git log --oneline            # 一行显示日志信息
13. git reset --hard 920064b     # 代码回滚到任意的历史版本
14. git reflog             # 查看所有的历史版本信息
15. 
16. [root@Gitlab git_data]# git init        #初始化目录
17. [root@Gitlab git_data]# git status      #查看状态
18. # On branch master
19. #
20. # Initial commit
21. #
22. nothing to commit (create/copy files and use "git add" to track)
23. [root@Gitlab git_data]# touch test.txt
24. [root@Gitlab git_data]# git status
25. # On branch master
26. #
27. # Initial commit
28. #
29. # Untracked files:
30. #   (use "git add <file>..." to include in what will be committed)
31. #
32. # test.txt    #红色
33. nothing added to commit but untracked files present (use "git add" to track)
34. [root@Gitlab git_data]# git add test.txt    #将代码提交道暂存区
35. [root@Gitlab git_data]# git status
36. # On branch master
37. #
38. # Initial commit
39. #
40. # Changes to be committed:
41. #   (use "git rm --cached <file>..." to unstage)
42. #
43. # new file:   test.txt    #绿色
44. #
45. [root@Gitlab git_data]# ll .git/
46. total 16
47. drwxr-xr-x 2 root root   6 May 18 15:11 branches
48. -rw-r--r-- 1 root root  92 May 18 15:11 config
49. -rw-r--r-- 1 root root  73 May 18 15:11 description
50. -rw-r--r-- 1 root root  23 May 18 15:11 HEAD
51. drwxr-xr-x 2 root root 242 May 18 15:11 hooks
52. -rw-r--r-- 1 root root 104 May 18 15:17 index       #推送文件后多了index,刚才没有
53. drwxr-xr-x 2 root root  21 May 18 15:11 info
54. drwxr-xr-x 5 root root  40 May 18 15:17 objects
55. drwxr-xr-x 4 root root  31 May 18 15:11 refs
56. [root@Gitlab git_data]# git rm --cached test.txt    #删除暂存取文件
57. rm 'test.txt'
58. [root@Gitlab git_data]# git status
59. # On branch master
60. #
61. # Initial commit
62. #
63. # Untracked files:
64. #   (use "git add <file>..." to include in what will be committed)
65. #
66. # test.txt    #又变成了红色
67. nothing added to commit but untracked files present (use "git add" to track)
68. [root@Gitlab git_data]# git commit -m "newfile test.txt"    #将暂存区文件提交到本地仓库
69. [master (root-commit) 088cd87] newfile test.txt
70. 1 file changed, 0 insertions(+), 0 deletions(-)
71.  create mode 100644 test.txt

四、git测试本地仓库提交

注意:工作区git add . 提交到暂存区,. git commit -m ""提交到本地仓库,提交到本地仓库后暂存区就没有该文件了

1. [root@Gitlab git_data]# touch 1.txt
2. [root@Gitlab git_data]# git add 1.txt           #工作区的1.txt提交到暂存区
3. [root@Gitlab git_data]# git commit -m "newfile 1.txt"    #暂存区的文件提交到本地仓库
4. [master 37c6c58] newfile 1.txt
5. 1 file changed, 0 insertions(+), 0 deletions(-)
6.  create mode 100644 1.txt
7. [root@Gitlab git_data]# ll
8. total 0
9. -rw-r--r-- 1 root root 0 May 18 15:21 1.txt
10. -rw-r--r-- 1 root root 0 May 18 15:16 test.txt
11. [root@Gitlab git_data]# git rm test.txt
12. rm 'test.txt'
13. [root@Gitlab git_data]# git status
14. # On branch master                                #位于master分支
15. # Changes to be committed:                        #要提交的变更
16. #   (use "git reset HEAD <file>..." to unstage)   #使用git reset HEAD恢复操作
17. #
18. # deleted:    test.txt                          #删除test.txt文件
19. #
20. [root@Gitlab git_data]# ll
21. total 0
22. -rw-r--r-- 1 root root 0 May 18 15:21 1.txt
23. [root@Gitlab git_data]# git reset HEAD test.txt    
24. Unstaged changes after reset:
25. D test.txt
26. [root@Gitlab git_data]# ll
27. total 0
28. -rw-r--r-- 1 root root 0 May 18 15:21 1.txt
29. [root@Gitlab git_data]# git status
30. # On branch master
31. # Changes not staged for commit:
32. #   (use "git add/rm <file>..." to update what will be committed)
33. #   (use "git checkout -- <file>..." to discard changes in working directory)
34. #
35. # deleted:    test.txt    #红色
36. #
37. no changes added to commit (use "git add" and/or "git commit -a")
38. [root@Gitlab git_data]# git checkout -- test.txt    #将暂存区恢复代码目录
39. [root@Gitlab git_data]# ll
40. total 0
41. -rw-r--r-- 1 root root 0 May 18 15:21 1.txt
42. -rw-r--r-- 1 root root 0 May 18 15:28 test.txt
43. [root@Gitlab git_data]# git rm -f test.txt          #删除的工作目录
44. rm 'test.txt'
45. [root@Gitlab git_data]# git status
46. # On branch master
47. # Changes to be committed:
48. #   (use "git reset HEAD <file>..." to unstage)
49. #
50. # deleted:    test.txt
51. #
52. [root@Gitlab git_data]# git commit -m "delete test.txt"  #删除当前版本的本地仓库的文件[master 28a657e] delete test.txt
53. 1 file changed, 0 insertions(+), 0 deletions(-)
54. delete mode 100644 test.txt
55. 注意:提交新的代码或者删除或者修改名称都必须执行git add 或者git commit
56. 
57. 
58. #git add . 或 *      #添加目录中所有改动过的文件
59. #git rm --cached     #将文件从暂存区撤回到工作区,再rm -f
60. #git rm -rf          #直接从暂存区域同工作区域一同删除文件命令

五、git本地仓库改名

两种方式,一种是改工作区,从暂存区删除文件,再将改名后的文件加入到暂存区,再提交到本地仓库,另一种方式是直接用git命令修改,再提交到本地仓库

1. [root@Gitlab git_data]# git mv 1.txt 2.txt
2. [root@Gitlab git_data]# git status
3. # On branch master
4. # Changes to be committed:
5. #   (use "git reset HEAD <file>..." to unstage)
6. #
7. #  renamed:    1.txt -> 2.txt
8. #
9. [root@Gitlab git_data]# git add .
10. [root@Gitlab git_data]# git commit -m "rename 1.txt-->2.txt"
11. [master 396ca91] rename 1.txt-->2.txt
12. 1 file changed, 0 insertions(+), 0 deletions(-)
13.  rename 1.txt => 2.txt (100%)

六、git比对工作目录和暂存区

git status只能查看区域状态的不同,不能查看文件内容的变化,git diff查看内容的不同

注意,对比的时候如果两边一致则返回空,如果两边有其中一边没有文件(通常是暂存区没有文件),则就没有了对比的必要,所以也会返回为空,这里不要混淆。

1. [root@Gitlab git_data]# git diff             #比对工作区和暂存区的不同
2. [root@Gitlab git_data]# git diff --cached    #比对暂存区和本地仓库的不同
3. 
4. [root@Gitlab git_data]# echo 123 > 2.txt
5. [root@Gitlab git_data]# git diff
6. diff --git a/2.txt b/2.txt
7. index e69de29..190a180 100644
8. --- a/2.txt
9. +++ b/2.txt
10. @@ -0,0 +1 @@
11. +123
12. 
13. [root@Gitlab git_data]# git add .
14. [root@Gitlab git_data]# git diff    #工作区与暂存区一致所以对比返回空
15. [root@Gitlab git_data]# git diff --cached
16. diff --git a/2.txt b/2.txt
17. index e69de29..190a180 100644
18. --- a/2.txt
19. +++ b/2.txt
20. @@ -0,0 +1 @@
21. +123
22. [root@Gitlab git_data]# git commit -m "modifed 2.txt"    #修改本地仓库的2.txt
23. [master 97bf824] modifed 2.txt
24. 2 files changed, 2 insertions(+)
25.  create mode 100644 2.bak
26. [root@Gitlab git_data]# git diff --cached    #这里空并不是因为暂存区与本地仓库最后一个版本文件一致,而是因为暂存区提交到本地仓库后,暂存区没有数据,没有对比的必要,所以会返回空

七、查看git各版本操作和版本详细信息

git commit        #相当于虚拟机的镜像、任何操作都被做了一次快照,可恢复到任意一个位置

git log               #查看历史的git commit快照操作,会显示哈希唯一标识、作者个人信息、时间和个人写的提交描述信息

git log --oneline --decorate        #显示当前指针指向哪里

git log -p        #显示具体内容的变化

git log -1        #只显示1条内容

1. [root@Gitlab git_data]# git log --oneline    #一行显示详细信息
2. 97bf824 modifed 2.txt
3. 396ca91 rename 1.txt-->2.txt
4. 28a657e delete test.txt
5. 37c6c58 newfile 1.txt
6. 088cd87 newfile test.tx
7. [root@Gitlab git_data]# git show 97bf824    #查看详细信息
8. commit 97bf8240149c07ca3ee5c7d51b1d8ba63376c359
9. Author: koten <1225640773@qq.com>
10. Date:   Thu May 18 15:40:10 2023 +0800
11. 
12.     modifed 2.txt
13. 
14. diff --git a/2.bak b/2.bak
15. new file mode 100644
16. index 0000000..190a180
17. --- /dev/null
18. +++ b/2.bak
19. @@ -0,0 +1 @@
20. +123
21. diff --git a/2.txt b/2.txt
22. index e69de29..190a180 100644
23. --- a/2.txt
24. +++ b/2.txt
25. @@ -0,0 +1 @@
26. +123
27. (END)

八、测试代码回滚,恢复历史数据

1、如果只改了工作区,可以使用 git checkout --  文件,从暂存区覆盖本地工作目录

2、如果修改了工作区,且提交到了暂存区,可以使用 git reset HEAD 文件,本地仓库覆盖暂存区域,重置暂存区的操作变更

3、如果修改了工作区,且提交了暂存区和本地仓库后进行数据恢复,可以进行如下操作恢复版本

1. [root@Gitlab git_data]# git log --oneline          #简单显示历史版本
2. 97bf824 modifed 2.txt
3. 396ca91 rename 1.txt-->2.txt
4. 28a657e delete test.txt
5. 37c6c58 newfile 1.txt
6. 088cd87 newfile test.txt
7. 
8. #Git服务程序中有一个叫做HEAD的版本指针,当用户申请还原数据时,其实就是将HEAD指针指向到某个特定的提交版本,但是因为Git是分布式 版本控制系统,为了避免历史记录冲突,故使用了SHA-1计算出十六进制的哈希字串来区分每个提交版本,另外默认的HEAD版本指针会指向到最近的一次提交版本记录。
9. [root@Gitlab git_data]# git reset --hard 37c6c58
10. HEAD is now at 37c6c58 newfile 1.txt
11. [root@Gitlab git_data]# ll
12. total 0
13. -rw-r--r-- 1 root root 0 May 18 15:43 1.txt
14. -rw-r--r-- 1 root root 0 May 18 15:43 test.txt
15. [root@Gitlab git_data]# git log --oneline   
16. 37c6c58 newfile 1.txt
17. 088cd87 newfile test.txt
18. #看不到之前历史版本,往上拉,因为我们当前的工作版本是历史的一个提交点,这个历史提交点还没有发生过add bbb 更新记录,所以当然就看不到了,要是想"还原到未来"的历史更新点,可以用git reflog命令来查看所有的历史记录
19. [root@Gitlab git_data]# git reset --hard 97bf824
20. HEAD is now at 97bf824 modifed 2.txt
21. [root@Gitlab git_data]# ll
22. total 4
23. -rw-r--r-- 1 root root 4 May 18 15:44 2.txt
24. [root@Gitlab git_data]# git log --oneline
25. 97bf824 modifed 2.txt
26. 396ca91 rename 1.txt-->2.txt
27. 28a657e delete test.txt
28. 37c6c58 newfile 1.txt
29. 088cd87 newfile test.txt
30. [root@Gitlab git_data]# git reflog            #查看所有版本,包括reset也有记录
31. 97bf824 HEAD@{0}: reset: moving to 97bf824
32. 37c6c58 HEAD@{1}: reset: moving to 37c6c58
33. 97bf824 HEAD@{2}: commit: modifed 2.txt
34. 396ca91 HEAD@{3}: commit: rename 1.txt-->2.txt
35. 28a657e HEAD@{4}: commit: delete test.txt
36. 37c6c58 HEAD@{5}: commit: newfile 1.txt
37. 088cd87 HEAD@{6}: commit (initial): newfile test.txt

九、git中tag标签的使用

标签也是指向了一次commit提交,给git本地仓库的版本,打上tag,并不是所有的版本都打,一些稳定版本可以打上,比如有稳定版本v1.1和v1.2,v1.2版本出了问题,我们就回滚到v1.1上,用tag标签回滚,而不是用哈希值进行回滚,会方便些。

tag和当前的master是平行关系,修改master里面的内容,v1.1不变

1. [root@Gitlab git_data]# git tag -a v1.3 -m "v1.3稳定版本"    #-a指定标签名字,-m指定说明文字,给当前版本打tag
2. [root@Gitlab git_data]# git tag
3. v1.3
4. [root@Gitlab git_data]# git log --oneline
5. 4e601a1 testing newfile test.txt
6. 2eba431 newfile test1.txt
7. 97bf824 modifed 2.txt
8. 396ca91 rename 1.txt-->2.txt
9. 28a657e delete test.txt
10. 37c6c58 newfile 1.txt
11. 088cd87 newfile test.txt
12. [root@Gitlab git_data]# git tag -a v1.2 28a657e -m "v1.2稳定版本"    #给指定版本打tag
13. [root@Gitlab git_data]# git reset --hard v1.2    #还原到指定版本
14. HEAD is now at 28a657e delete test.txt
15. [root@Gitlab git_data]# git reset --hard v1.3
16. HEAD is now at 4e601a1 testing newfile test.txt
17. [root@Gitlab git_data]# git log --oneline --decorate
18. 4e601a1 (HEAD, tag: v1.3, testing, master) testing newf
19. 2eba431 newfile test1.txt
20. 97bf824 modifed 2.txt
21. 396ca91 rename 1.txt-->2.txt
22. 28a657e (tag: v1.2) delete test.txt
23. 37c6c58 newfile 1.txt
24. 088cd87 newfile test.txt
25. [root@Gitlab git_data]# git show v1.2    #查看v1.2的tag信息
26. tag v1.2
27. Tagger: koten <1225640773@qq.com>
28. Date:   Fri May 19 09:33:15 2023 +0800
29. 
30. v1.2稳定版本
31. 
32. commit 28a657e1f78a6543e5e07ff4a11b5cad4724ea58
33. Author: koten <1225640773@qq.com>
34. Date:   Thu May 18 15:31:16 2023 +0800
35. 
36. delete test.txt
37. 
38. diff --git a/test.txt b/test.txt
39. deleted file mode 100644
40. [root@Gitlab git_data]# git tag -d v1.2    #删除tag标签,-d指定版本
41. Deleted tag 'v1.2' (was 82d7773)

Git分支

我们可以将半成品代码放到git分支中,此时代码只属于你自己,其他人看不到,当版本稳定后再与原来项目主分支上合并。

在实际项目中,要保证master分支非常稳定,仅用于发布新版本,工作时候可以创建不同的功能工作分支,工作完成后经过test分支测试,测试没有问题再合并到master分支,有的公司在test分支和master分支之间还有bug修复分支,只负责修复功能,修复完成后再应用到master上。

1. [root@Gitlab git_data]# git log --oneline --decorate   #查看所有版本加当前指针指向哪里
2. 2eba431 (HEAD, master) newfile test1.txt        #HEAD指针指向哪个分支,说明你当前在哪个分支下工作
3. 97bf824 modifed 2.txt
4. 396ca91 rename 1.txt-->2.txt
5. 28a657e delete test.txt
6. 37c6c58 newfile 1.txt
7. 088cd87 newfile test.txt
8. [root@Gitlab git_data]# git branch testing    #创建分支
9. [root@Gitlab git_data]# git branch            #查看所有分支
10. * master                                      #*号在哪里就说明当前在哪个分支下面
11.   testing    
12. [root@Gitlab git_data]# git checkout testing    #切换到testing分支,目录不会变化,实际是进入了另一个平行空间
13. A 1.txt
14. A 3.txt
15. Switched to branch 'testing'
16. [root@Gitlab git_data]# git branch
17.   master
18. * testing
19. 
20. #分支操作不影响主干
21. [root@Gitlab git_data]# touch test.txt
22. [root@Gitlab git_data]# git add .
23. [root@Gitlab git_data]# git commit -m "testing newfile test.txt"    #暂存区和新创建的都会提交
24. [testing 4e601a1] testing newfile test.txt
25. 3 files changed, 1 insertion(+)
26.  create mode 100644 1.txt
27.  create mode 100644 3.txt
28.  create mode 100644 test.txt
29. [root@Gitlab git_data]# git checkout master
30. Switched to branch 'master'
31. [root@Gitlab git_data]# ll
32. total 8
33. -rw-r--r-- 1 root root 4 May 18 21:25 2.bak
34. -rw-r--r-- 1 root root 4 May 18 15:44 2.txt
35. -rw-r--r-- 1 root root 0 May 18 20:02 test1.txt
36. 
37. #合并操作到主干
38. [root@Gitlab git_data]# git merge testing    #将testing的文件合并到主干分支,可能会提示输入描述信息,相当于git的-m参数
39. Updating 2eba431..4e601a1
40. Fast-forward
41. 1.txt    | 1 +
42. 3.txt    | 0
43. test.txt | 0
44. 3 files changed, 1 insertion(+)
45.  create mode 100644 1.txt
46.  create mode 100644 3.txt
47.  create mode 100644 test.txt
48. [root@Gitlab git_data]# ll
49. total 12
50. -rw-r--r-- 1 root root 3 May 19 09:08 1.txt
51. -rw-r--r-- 1 root root 4 May 18 21:25 2.bak
52. -rw-r--r-- 1 root root 4 May 18 15:44 2.txt
53. -rw-r--r-- 1 root root 0 May 19 09:08 3.txt
54. -rw-r--r-- 1 root root 0 May 18 20:02 test1.txt
55. -rw-r--r-- 1 root root 0 May 19 09:08 test.txt

一、git分支冲突问题

主干和分支文件不一致时,会出现代码冲突,需要进行手动修改,进行冲突合并

1. 1、在master修改2.bak第二行为456
2. 2、切换testing分支,修改2.bak第二行内容为789
3. 3、切换到master,合并testing分支后出现合并失败,手动编辑2.bak,删除特殊符号,留下想要的行
4. 然后再次执行xxx
5. 
6. [root@Gitlab git_data]# echo 456 >> 2.bak 
7. [root@Gitlab git_data]# git add .
8. [root@Gitlab git_data]# git commit -m 'modify 2.bak'
9. [master 8941612] modify 2.bak
10. 1 file changed, 1 insertion(+)
11. [root@Gitlab git_data]# git checkout testing 
12. Switched to branch 'testing'
13.   oot@Gitlab git_data]# echo 789 >> 2.bak 
14. [root@Gitlab git_data]# git add .
15. [root@Gitlab git_data]# git commit -m 'modify 2.bak'
16. [testing 5a0e7a3] modify 2.bak
17. 1 file changed, 1 insertion(+)
18. [root@Gitlab git_data]# git checkout master 
19. Switched to branch 'master'
20. [root@Gitlab git_data]# git merge testing
21. Auto-merging 2.bak
22. CONFLICT (content): Merge conflict in 2.bak
23. Automatic merge failed; fix conflicts and then commit the result.                                         
24. [root@Gitlab git_data]# cat 2.bak
25. 123
26. <<<<<<< HEAD
27. 456
28. =======
29. 789
30. >>>>>>> testing            
31. [root@Gitlab git_data]# cat 2.bak
32. 123
33. 456
34. 789

分支要勤于删除创建,用完就删,不然可能会落后于主分支代码,这种情况重新克隆一个分支即可

1. git commit -b "testing"    #创建并切换分支
2. git branch -d  "testing"   #删除分支

主干合并分支的时候,如果主干有了新文件,分支没有的话,合并时,并不会影响主干的新文件,相同文件会将分支文件覆盖到主干上,有远程仓库的时候这种情况就不让提交了


我是koten,10年运维经验,持续分享运维干货,感谢大家的阅读和关注!

目录
相关文章
|
1月前
|
开发工具 git 开发者
|
1月前
|
开发工具 git
|
1月前
|
网络协议 Shell Linux
【Shell 命令集合 网络通讯 】⭐⭐⭐Linux 测试与目标主机之间的网络连接ping 命令 使用指南
【Shell 命令集合 网络通讯 】⭐⭐⭐Linux 测试与目标主机之间的网络连接ping 命令 使用指南
43 1
|
1月前
|
开发工具 git 开发者
|
1月前
|
开发工具 git
|
4天前
|
存储 运维 Shell
Ansible自动化运维工具安装和基本使用
Ansible 是一款无代理的IT自动化工具,通过SSH连接目标主机执行配置管理、应用部署和云端管理任务。它使用YAML编写的Playbook定义任务,核心组件包括Playbook、模块、主机清单、变量等。Ansible的优势在于易用、功能强大、无须在目标主机安装额外软件,并且开源。安装过程涉及配置网络源、yum安装和SSH密钥设置。通过定义主机清单和使用模块进行通信测试,确保连接成功。
Ansible自动化运维工具安装和基本使用
|
5天前
|
运维 网络协议 Linux
【运维系列】Centos7安装并配置PXE服务
PXE是Intel开发的预启动执行环境,允许工作站通过网络从远程服务器启动操作系统。它依赖DHCP分配IP,DNS服务分配主机名,TFTP提供引导程序,HTTP/FTP/NFS提供安装源。要部署PXE服务器,需关闭selinux和防火墙,安装dhcpd、httpd、tftp、xinetd及相关服务,配置引导文件和Centos7安装源。最后,通过syslinux安装引导文件,并创建pxelinux.cfg/default配置文件来定义启动参数。
18 0
|
5天前
|
运维 网络协议 Linux
【运维系列】Centos7安装并配置postfix服务
安装CentOS7的Postfix和Dovecot,配置Postfix的`main.cf`文件,包括修改完全域名、允许所有IP、启用邮箱等。然后,配置Dovecot的多个配置文件以启用auth服务和调整相关设置。重启Postfix和Dovecot,设置开机自启,并关闭防火墙进行测试。最后,创建邮箱账户并在Windows邮箱客户端中添加账户设置。
12 0
|
5天前
|
Ubuntu Linux 测试技术
Linux(32)Rockchip RK3568 Ubuntu22.04上部署 Docker: 详细配置与功能测试(下)
Linux(32)Rockchip RK3568 Ubuntu22.04上部署 Docker: 详细配置与功能测试
35 1
|
6天前
|
分布式计算 Hadoop Scala
Spark【环境搭建 01】spark-3.0.0-without 单机版(安装+配置+测试案例)
【4月更文挑战第13天】Spark【环境搭建 01】spark-3.0.0-without 单机版(安装+配置+测试案例)
8 0

热门文章

最新文章