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

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

目录

常见的版本管理工具

Git安装与全局配置

Git初始化代码仓库

Git常规使用

一、git四大区域

二、git四种状态

三、git基础命令

四、git测试本地仓库提交

五、git本地仓库改名

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

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

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

九、git中tag标签的使用

Git分支

一、git分支冲突问题


常见的版本管理工具

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

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

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

Git安装与全局配置

1、环境准备

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

2、Git安装部署

[root@Gitlab ~]# rpm -qa|grep ^git     #Centos7.9默认已经安装好了
git-1.8.3.1-23.el7_8.x86_64

[root@Gitlab ~]# yum -y install git    #如果Linux系统版本低可以yum安装

3、全局配置

#git配置文件
[root@Gitlab ~]# 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@Gitlab ~]# git config --global user.name "koten"    #配置git使用用户
[root@Gitlab ~]# git config --global user.email "[email protected]"    #配置git使用邮箱
[root@Gitlab ~]# git config --global color.ui true    #配置语法高亮
[root@Gitlab ~]# git config --list    #列出刚刚配置的内容
user.name=koten
[email protected]
color.ui=true
[root@Gitlab ~]# cat .gitconfig       #全局配置实际是保存在了这个配置文件
[user]
	name = koten
	email = [email protected]
[color]
	ui = true

Git初始化代码仓库

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

Git常规使用

一、git四大区域

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

git add                    #工作区提交项目到暂存区
git commit                 #暂存区提交项目到本地仓库
git push                   #本地仓库提交项目到远程仓库
git rm                     #从Git的暂存区域移除文件、在工作区中将被跟踪文件删除
git reset HEAD             #取消已经暂存的文件,将其转换回未暂存的状态
git reset --hard 3de15d4   #将当前分支的HEAD指向commit哈希值为3de15d4的提交,并且重置Staging Area和工作目录,将它们恢复到该提交时的状态,恢复指定版本
git clone                  #远程仓库拉取到本地仓库
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基础命令

git init         # 初始化目录
git status       # 查看仓库的状态
git add file     # 将代码提交带暂存区
git add .        # 将当前所有的文件提交到暂存区
git rm --cached  # 删除暂存区的文件
git commit -m "newfile test.txt" # 将暂存区的文件提交到本地仓库
git checkout -- test.txt         # 如果文件已经被添加到缓存区,将使该文件与暂存区保持一致(即恢复到最近一次提交时的状态),而如果文件处于修改状态但尚未添加到缓存区,则会将其还原为最近一次 Git 的状态。在所有情况下,Git 会强制覆盖当前工作目录中的指定文件。
git rm file                      # 删除文件
注意: 提交新的代码或者删除或者修改名称都必须执行 git add file 和 git commit
git diff         			 # 比对的是工作目录和暂存区的不同
git diff --cached 		     # 比对的是暂存区和本地仓库的不同
git log --oneline            # 一行显示日志信息
git reset --hard 920064b     # 代码回滚到任意的历史版本
git reflog 				     # 查看所有的历史版本信息

[root@Gitlab git_data]# git init        #初始化目录
[root@Gitlab git_data]# git status      #查看状态
# On branch master
#
# Initial commit
#
nothing to commit (create/copy files and use "git add" to track)
[root@Gitlab git_data]# touch test.txt
[root@Gitlab git_data]# git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#	test.txt    #红色
nothing added to commit but untracked files present (use "git add" to track)
[root@Gitlab git_data]# git add test.txt    #将代码提交道暂存区
[root@Gitlab git_data]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#	new file:   test.txt    #绿色
#
[root@Gitlab git_data]# ll .git/
total 16
drwxr-xr-x 2 root root   6 May 18 15:11 branches
-rw-r--r-- 1 root root  92 May 18 15:11 config
-rw-r--r-- 1 root root  73 May 18 15:11 description
-rw-r--r-- 1 root root  23 May 18 15:11 HEAD
drwxr-xr-x 2 root root 242 May 18 15:11 hooks
-rw-r--r-- 1 root root 104 May 18 15:17 index       #推送文件后多了index,刚才没有
drwxr-xr-x 2 root root  21 May 18 15:11 info
drwxr-xr-x 5 root root  40 May 18 15:17 objects
drwxr-xr-x 4 root root  31 May 18 15:11 refs
[root@Gitlab git_data]# git rm --cached test.txt    #删除暂存取文件
rm 'test.txt'
[root@Gitlab git_data]# git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#	test.txt    #又变成了红色
nothing added to commit but untracked files present (use "git add" to track)
[root@Gitlab git_data]# git commit -m "newfile test.txt"    #将暂存区文件提交到本地仓库
[master (root-commit) 088cd87] newfile test.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 test.txt

四、git测试本地仓库提交

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

[root@Gitlab git_data]# touch 1.txt
[root@Gitlab git_data]# git add 1.txt           #工作区的1.txt提交到暂存区
[root@Gitlab git_data]# git commit -m "newfile 1.txt"    #暂存区的文件提交到本地仓库
[master 37c6c58] newfile 1.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 1.txt
[root@Gitlab git_data]# ll
total 0
-rw-r--r-- 1 root root 0 May 18 15:21 1.txt
-rw-r--r-- 1 root root 0 May 18 15:16 test.txt
[root@Gitlab git_data]# git rm test.txt
rm 'test.txt'
[root@Gitlab git_data]# git status
# On branch master                                #位于master分支
# Changes to be committed:                        #要提交的变更
#   (use "git reset HEAD <file>..." to unstage)   #使用git reset HEAD恢复操作
#
#	deleted:    test.txt                          #删除test.txt文件
#
[root@Gitlab git_data]# ll
total 0
-rw-r--r-- 1 root root 0 May 18 15:21 1.txt
[root@Gitlab git_data]# git reset HEAD test.txt    
Unstaged changes after reset:
D	test.txt
[root@Gitlab git_data]# ll
total 0
-rw-r--r-- 1 root root 0 May 18 15:21 1.txt
[root@Gitlab 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:    test.txt    #红色
#
no changes added to commit (use "git add" and/or "git commit -a")
[root@Gitlab git_data]# git checkout -- test.txt    #将暂存区恢复代码目录
[root@Gitlab git_data]# ll
total 0
-rw-r--r-- 1 root root 0 May 18 15:21 1.txt
-rw-r--r-- 1 root root 0 May 18 15:28 test.txt
[root@Gitlab git_data]# git rm -f test.txt          #删除的工作目录
rm 'test.txt'
[root@Gitlab git_data]# git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#	deleted:    test.txt
#
[root@Gitlab git_data]# git commit -m "delete test.txt"  #删除当前版本的本地仓库的文件[master 28a657e] delete test.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 test.txt
注意:提交新的代码或者删除或者修改名称都必须执行git add 或者git commit


#git add . 或 *      #添加目录中所有改动过的文件
#git rm --cached     #将文件从暂存区撤回到工作区,再rm -f
#git rm -rf          #直接从暂存区域同工作区域一同删除文件命令

五、git本地仓库改名

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

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

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

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

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

[root@Gitlab git_data]# git diff             #比对工作区和暂存区的不同
[root@Gitlab git_data]# git diff --cached    #比对暂存区和本地仓库的不同

[root@Gitlab git_data]# echo 123 > 2.txt
[root@Gitlab git_data]# git diff
diff --git a/2.txt b/2.txt
index e69de29..190a180 100644
--- a/2.txt
+++ b/2.txt
@@ -0,0 +1 @@
+123

[root@Gitlab git_data]# git add .
[root@Gitlab git_data]# git diff    #工作区与暂存区一致所以对比返回空
[root@Gitlab git_data]# git diff --cached
diff --git a/2.txt b/2.txt
index e69de29..190a180 100644
--- a/2.txt
+++ b/2.txt
@@ -0,0 +1 @@
+123
[root@Gitlab git_data]# git commit -m "modifed 2.txt"    #修改本地仓库的2.txt
[master 97bf824] modifed 2.txt
 2 files changed, 2 insertions(+)
 create mode 100644 2.bak
[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条内容

[root@Gitlab git_data]# git log --oneline    #一行显示详细信息
97bf824 modifed 2.txt
396ca91 rename 1.txt-->2.txt
28a657e delete test.txt
37c6c58 newfile 1.txt
088cd87 newfile test.tx
[root@Gitlab git_data]# git show 97bf824    #查看详细信息
commit 97bf8240149c07ca3ee5c7d51b1d8ba63376c359
Author: koten <[email protected]>
Date:   Thu May 18 15:40:10 2023 +0800

    modifed 2.txt

diff --git a/2.bak b/2.bak
new file mode 100644
index 0000000..190a180
--- /dev/null
+++ b/2.bak
@@ -0,0 +1 @@
+123
diff --git a/2.txt b/2.txt
index e69de29..190a180 100644
--- a/2.txt
+++ b/2.txt
@@ -0,0 +1 @@
+123
(END)

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

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

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

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

[root@Gitlab git_data]# git log --oneline          #简单显示历史版本
97bf824 modifed 2.txt
396ca91 rename 1.txt-->2.txt
28a657e delete test.txt
37c6c58 newfile 1.txt
088cd87 newfile test.txt

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

[root@Gitlab git_data]# git tag -a v1.3 -m "v1.3稳定版本"    #-a指定标签名字,-m指定说明文字,给当前版本打tag
[root@Gitlab git_data]# git tag
v1.3
[root@Gitlab git_data]# git log --oneline
4e601a1 testing newfile test.txt
2eba431 newfile test1.txt
97bf824 modifed 2.txt
396ca91 rename 1.txt-->2.txt
28a657e delete test.txt
37c6c58 newfile 1.txt
088cd87 newfile test.txt
[root@Gitlab git_data]# git tag -a v1.2 28a657e -m "v1.2稳定版本"    #给指定版本打tag
[root@Gitlab git_data]# git reset --hard v1.2    #还原到指定版本
HEAD is now at 28a657e delete test.txt
[root@Gitlab git_data]# git reset --hard v1.3
HEAD is now at 4e601a1 testing newfile test.txt
[root@Gitlab git_data]# git log --oneline --decorate
4e601a1 (HEAD, tag: v1.3, testing, master) testing newf
2eba431 newfile test1.txt
97bf824 modifed 2.txt
396ca91 rename 1.txt-->2.txt
28a657e (tag: v1.2) delete test.txt
37c6c58 newfile 1.txt
088cd87 newfile test.txt
[root@Gitlab git_data]# git show v1.2    #查看v1.2的tag信息
tag v1.2
Tagger: koten <[email protected]>
Date:   Fri May 19 09:33:15 2023 +0800

v1.2稳定版本

commit 28a657e1f78a6543e5e07ff4a11b5cad4724ea58
Author: koten <[email protected]>
Date:   Thu May 18 15:31:16 2023 +0800

    delete test.txt

diff --git a/test.txt b/test.txt
deleted file mode 100644
[root@Gitlab git_data]# git tag -d v1.2    #删除tag标签,-d指定版本
Deleted tag 'v1.2' (was 82d7773)

Git分支

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

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

[root@Gitlab git_data]# git log --oneline --decorate   #查看所有版本加当前指针指向哪里
2eba431 (HEAD, master) newfile test1.txt        #HEAD指针指向哪个分支,说明你当前在哪个分支下工作
97bf824 modifed 2.txt
396ca91 rename 1.txt-->2.txt
28a657e delete test.txt
37c6c58 newfile 1.txt
088cd87 newfile test.txt
[root@Gitlab git_data]# git branch testing    #创建分支
[root@Gitlab git_data]# git branch            #查看所有分支
* master                                      #*号在哪里就说明当前在哪个分支下面
  testing    
[root@Gitlab git_data]# git checkout testing    #切换到testing分支,目录不会变化,实际是进入了另一个平行空间
A	1.txt
A	3.txt
Switched to branch 'testing'
[root@Gitlab git_data]# git branch
  master
* testing

#分支操作不影响主干
[root@Gitlab git_data]# touch test.txt
[root@Gitlab git_data]# git add .
[root@Gitlab git_data]# git commit -m "testing newfile test.txt"    #暂存区和新创建的都会提交
[testing 4e601a1] testing newfile test.txt
 3 files changed, 1 insertion(+)
 create mode 100644 1.txt
 create mode 100644 3.txt
 create mode 100644 test.txt
[root@Gitlab git_data]# git checkout master
Switched to branch 'master'
[root@Gitlab git_data]# ll
total 8
-rw-r--r-- 1 root root 4 May 18 21:25 2.bak
-rw-r--r-- 1 root root 4 May 18 15:44 2.txt
-rw-r--r-- 1 root root 0 May 18 20:02 test1.txt

#合并操作到主干
[root@Gitlab git_data]# git merge testing    #将testing的文件合并到主干分支,可能会提示输入描述信息,相当于git的-m参数
Updating 2eba431..4e601a1
Fast-forward
 1.txt    | 1 +
 3.txt    | 0
 test.txt | 0
 3 files changed, 1 insertion(+)
 create mode 100644 1.txt
 create mode 100644 3.txt
 create mode 100644 test.txt
[root@Gitlab git_data]# ll
total 12
-rw-r--r-- 1 root root 3 May 19 09:08 1.txt
-rw-r--r-- 1 root root 4 May 18 21:25 2.bak
-rw-r--r-- 1 root root 4 May 18 15:44 2.txt
-rw-r--r-- 1 root root 0 May 19 09:08 3.txt
-rw-r--r-- 1 root root 0 May 18 20:02 test1.txt
-rw-r--r-- 1 root root 0 May 19 09:08 test.txt

一、git分支冲突问题

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

1、在master修改2.bak第二行为456
2、切换testing分支,修改2.bak第二行内容为789
3、切换到master,合并testing分支后出现合并失败,手动编辑2.bak,删除特殊符号,留下想要的行
然后再次执行xxx

[root@Gitlab git_data]# echo 456 >> 2.bak 
[root@Gitlab git_data]# git add .
[root@Gitlab git_data]# git commit -m 'modify 2.bak'
[master 8941612] modify 2.bak
 1 file changed, 1 insertion(+)
[root@Gitlab git_data]# git checkout testing 
Switched to branch 'testing'
  oot@Gitlab git_data]# echo 789 >> 2.bak 
[root@Gitlab git_data]# git add .
[root@Gitlab git_data]# git commit -m 'modify 2.bak'
[testing 5a0e7a3] modify 2.bak
 1 file changed, 1 insertion(+)
[root@Gitlab git_data]# git checkout master 
Switched to branch 'master'
[root@Gitlab git_data]# git merge testing
Auto-merging 2.bak
CONFLICT (content): Merge conflict in 2.bak
Automatic merge failed; fix conflicts and then commit the result.                                         
[root@Gitlab git_data]# cat 2.bak
123
<<<<<<< HEAD
456
=======
789
>>>>>>> testing            
[root@Gitlab git_data]# cat 2.bak
123
456
789

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

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

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


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

猜你喜欢

转载自blog.csdn.net/qq_37510195/article/details/130769762