Git详解

测试准备:

两台服务器:

192.168.58.139   git

192.168.58.140   gitlib

1.1 Git简介

官网:Git

git是一个分布式版本控制软件,最初由林纳斯·托瓦兹(Linus Torvalds)创作,于2005年以GPL发布。最初目的是为更好地管理Linux内核开发而设计。

1.2 Git历史

自2002年开始,林纳斯·托瓦兹决定使用BitKeeper作为Linux内核主要的版本控制系统用以维护代码。因为BitKeeper为专有软件,这个决定在社区中长期遭受质疑。在Linux社区中,特别是理查德·斯托曼与自由软件基金会的成员,主张应该使用开放源代码的软件来作为Linux核心的版本控制系统。林纳斯·托瓦兹曾考虑过采用现成软件作为版本控制系统(例如Monotone),但这些软件都存在一些问题,特别是性能不佳。现成的方案,如CVS的架构,受到林纳斯·托瓦兹的批评。

2005年,安德鲁·垂鸠写了一个简单程序,可以连接BitKeeper的存储库,BitKeeper著作权拥有者拉里·麦沃伊认为安德鲁·垂鸠对BitKeeper内部使用的协议进行逆向工程,决定收回无偿使用BitKeeper的授权。Linux内核开发团队与BitMover公司进行蹉商,但无法解决他们之间的歧见。林纳斯·托瓦兹决定自行开发版本控制系统替代BitKeeper,以十天的时间,编写出第一个git版本

1.3 安装git

1.3.1 环境说明
[root@git ~]# rpm -qa centos-release
centos-release-7-4.1708.el7.centos.x86_64
[root@git ~]# uname -a
Linux gitlab 3.10.0-693.el7.x86_64 #1 SMP Tue Aug 22 21:09:27 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
[root@git ~]# getenforce 
Disabled
[root@git ~]# systemctl status firewalld.service 
● 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)
1.3.2 Yum安装Git
[root@git ~]# yum install git -y
[root@git ~]# rpm -qa git
git-1.8.3.1-11.el7.x86_64
1.3.3 编译安装

Git下载地址: Releases · git/git · GitHub

# 安装依赖关系
[root@git ~]# yum install curl-devel expat-devel gettext-devel  openssl-devel zlib-devel autoconf gcc perl-ExtUtils-MakeMaker
# 编译安装 
[root@git ~]# tar -zxf git-2.0.0.tar.gz
[root@git ~]# cd git-2.0.0
[root@git ~]# ./configure --prefix=/usr/local/git # 没有文件可以略过
[root@git ~]# make  
[root@git ~]# make install  

1.4 初次运行 Git 前的配置

配置git

命令集:

git config --global user.name "username"  #配置git使用用户
git config --global user.email "email@mail.com"  #配置git使用邮箱
git config --global color.ui true  #语法高亮
git config --list # 查看全局配置

配置过程

[root@git ~]# git config --global user.name "mahong"  #配置git使用用户
[root@git ~]# git config --global user.email "mahong@aliyun.com"  #配置git使用邮箱
[root@git ~]# git config --global color.ui true  #语法高亮
[root@git ~]# git config --list # 查看全局配置
user.name=mahong
user.mail=mahong@aliyun.com
color.ui=true

[root@gitlib ~]# git config --global user.name "yanghanxiang"
[root@gitlib ~]# git config --global user.email "yanghanxiang@aliyun.com"
[root@gitlib ~]# git config --global color.ui true
[root@gitlib ~]# git config --list
user.name=yanghanxiang
user.email=yanghanxiang@aliyun.com
color.ui=true

生成的配置文件:

[root@git ~]# cd
[root@git ~]# cat .gitconfig 
[user]
        name = mahong
        email = mahong@aliyun.com
[color]
    ui = true

[root@gitlib ~]# cat /root/.gitconfig 
[user]
        name = yanghanxiang
        email = yanghanxiang@aliyun.com
[color]
        ui = true

1.5 获取 Git 仓库(初始化仓库)

1.5.1 创建裸库
[root@git ~]# useradd git
[root@git ~]# passwd git
[root@git ~]# mkdir /git/
[root@git ~]# cd /git/
[root@git git]# git init --bare shell.git
Initialized empty Git repository in /git/shell.git/
[root@git git]# chown -R git.git shell.git
1.5.2 创建本地库
[root@gitlib ~]# git clone git@192.168.58.139:/git/shell.git
正克隆到 'shell'...
The authenticity of host '192.168.58.139 (192.168.58.139)' can't be established.
ECDSA key fingerprint is SHA256:8cCuU4k7z9yHbqNeV4J/rQwabYVdWpmkctS4gJNVsfo.
ECDSA key fingerprint is MD5:49:b8:73:02:22:e6:73:9d:26:16:16:4e:f8:92:78:c5.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.58.139' (ECDSA) to the list of known hosts.
git@192.168.58.139's password: 
warning: 您似乎克隆了一个空版本库。
[root@gitlib ~]# ls
anaconda-ks.cfg  ip_fixed.sh  shell  yum-server.sh

[root@gitlib ~]# cd shell/
[root@gitlib shell]# ls
[root@gitlib shell]# echo "henhao henhao" > test.txt 
[root@gitlib shell]# ls
test.txt

[root@gitlib shell]# git add test.txt     #推送到内存
[root@gitlib shell]# git commit -m 'first commit'  #到本地
[master 8cb4caf] first commit
 1 file changed, 1 insertion(+)

[root@gitlib shell]# git push origin master   #到裸库,即git中
git@192.168.58.139's password: 
Counting objects: 6, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (6/6), 453 bytes | 0 bytes/s, done.
Total 6 (delta 0), reused 0 (delta 0)
To git@192.168.58.139:/git/shell.git
 * [new branch]      master -> master

[root@gitlib shell]# ls /root/shell/.git/objects
31  5c  8c  94  dd  ea  info  pack

1.6 Git命令常规操作

常用命令说明

| **命令**      | **命令说明**                                 |
| ------------- | --------------------------------------------|
| #**add**      | 添加文件内容至索引                           |
| **bisect**    | 通过二分查找定位引入 bug 的变更              |
| #**branch**   | 列出、创建或删除分支                         |
| #**checkout** | 检出一个分支或路径到工作区                   |
| #**clone**    | 克隆一个版本库到一个新目录                   |
| #**commit**   | 记录变更到版本库                             |
| #**diff**     | 显示提交之间、提交和工作区之间等的差异       |
| **fetch**     | 从另外一个版本库下载对象和引用               |
| **grep**      | 输出和模式匹配的行                           |
| #**init**     | 创建一个空的                                 |
| #**log**      | 显示提交日志                                 |
| #**merge**    | 合并两个或更多开发历史                       |
| #**mv**       | 移动或重命名一个文件、目录或符号链接         |
| #**pull**     | 获取并合并另外的版本库或一个本地分支         |
| #**push**     | 更新远程引用和相关的对象                     |
| **rebase**    | 本地提交转移至更新后的上游分支中             |
| #**reset**    | 重置当前HEAD到指定状态                       |
| #**rm**       | 从工作区和索引中删除文件                     |
| **show**      | 显示各种类型的对象                           |
| #**status**   | 显示工作区状态                               |
| #**tag**      | 创建、列出、删除或校验一个GPG签名的 tag 对象  |

常用操作示意图

文件的状态变化周期

1.6.1 创建文件
[root@gitlib shell]# touch README
[root@gitlib shell]# git status
# 位于分支 master
# 未跟踪的文件:
#   (使用 "git add <file>..." 以包含要提交的内容)
#
#       README
提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)

添加文件跟踪

[root@gitlib shell]# git add .
[root@gitlib shell]# git status
# 位于分支 master
# 要提交的变更:
#   (使用 "git reset HEAD <file>..." 撤出暂存区)
#
#       新文件:    README
#

文件会添加到.git的隐藏目录

[root@gitlib shell]# tree .git/
.git/
├── branches
├── COMMIT_EDITMSG
├── 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
├── index
├── info
│   └── exclude
├── logs
│   ├── HEAD
│   └── refs
│       ├── heads
│       │   ├── master
│       │   └── slave
│       └── remotes
│           └── origin
│               └── master
├── objects
│   ├── 31
│   │   └── 79b74be61ae10f0a78d57a78b91a02ce12367d
│   ├── 5c
│   │   └── c1d650927b7b42be4419b00bf302f3198be0b6
│   ├── 8c
│   │   └── b4caf4340578bcf65e426033b3134c1a7bc9b8
│   ├── 94
│   │   └── 6c72a1b6b2e6d56054a586145042d1e4f3106f
│   ├── dd
│   │   └── 2f9f4e74a6dd61ba1d39f9e2cd4fbd6429960b
│   ├── e6
│   │   └── 9de29bb2d1d6434b8b29ae775ad8c2e48c5391
│   ├── ea
│   │   └── 78a47130ef24046dd7358a915a2beed9cd0d28
│   ├── info
│   └── pack
└── refs
    ├── heads
    │   ├── master
    │   └── slave
    ├── remotes
    │   └── origin
    │       └── master
    └── tags

23 directories, 29 files

由工作区提交到本地仓库

[root@gitlib shell]# git commit  -m '123 commit' 
[master 948eb2b] 123 commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 README
[root@gitlib shell]# git status
# 位于分支 master
# 您的分支领先 'origin/master' 共 1 个提交。
#   (使用 "git push" 来发布您的本地提交)
#
无文件要提交,干净的工作区

提交后的git目录状态(94

[root@gitlib shell]# tree  .git/
.git/
├── branches
├── COMMIT_EDITMSG
├── 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
├── index
├── info
│   └── exclude
├── logs
│   ├── HEAD
│   └── refs
│       ├── heads
│       │   ├── master
│       │   └── slave
│       └── remotes
│           └── origin
│               └── master
├── objects
│   ├── 22
│   │   └── 53cc7f6c01e444922203ec073791066aa2cef2
│   ├── 31
│   │   └── 79b74be61ae10f0a78d57a78b91a02ce12367d
│   ├── 5c
│   │   └── c1d650927b7b42be4419b00bf302f3198be0b6
│   ├── 8c
│   │   └── b4caf4340578bcf65e426033b3134c1a7bc9b8
│   ├── 94
│   │   ├── 6c72a1b6b2e6d56054a586145042d1e4f3106f
│   │   └── 8eb2be8143e1f3bcc5f651c7ae6362616c3da8
│   ├── dd
│   │   └── 2f9f4e74a6dd61ba1d39f9e2cd4fbd6429960b
│   ├── e6
│   │   └── 9de29bb2d1d6434b8b29ae775ad8c2e48c5391
│   ├── ea
│   │   └── 78a47130ef24046dd7358a915a2beed9cd0d28
│   ├── info
│   └── pack
└── refs
    ├── heads
    │   ├── master
    │   └── slave
    ├── remotes
    │   └── origin
    │       └── master
    └── tags

24 directories, 31 files
1.6.2 添加新文件
git add  * 添加到暂存区域
git commit  提交git仓库 -m 后面接上注释信息,内容关于本次提交的说明,方便自己或他人查看

修改或删除原有文件

git add  *
git commit

简便方法

git commit -a  -m "注释信息"

-a 表示直接提交

1.6.3 删除git内的文件

命令说明:

# 没有添加到暂存区的数据直接rm删除即可。
rm database

# 已经添加到暂存区数据:

git rm --cached database 
#→将文件从git暂存区域的追踪列表移除(并不会删除当前工作目录内的数据文件)

git rm -f database
#→将文件数据从git暂存区和工作目录一起删除

命令实践:

[root@gitlib shell]# touch 123
[root@gitlib shell]# git status
# 位于分支 master
# 您的分支领先 'origin/master' 共 1 个提交。
#   (使用 "git push" 来发布您的本地提交)
#
# 未跟踪的文件:
#   (使用 "git add <file>..." 以包含要提交的内容)
#
#       123
提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)

将文件添加到暂存区域

[root@gitlib shell]# git add 123
[root@gitlib shell]# git status
# 位于分支 master
# 您的分支领先 'origin/master' 共 1 个提交。
#   (使用 "git push" 来发布您的本地提交)
#
# 要提交的变更:
#   (使用 "git reset HEAD <file>..." 撤出暂存区)
#
#       新文件:    123
#

删除文件

[root@gitlib shell]# rm 123 -f
[root@gitlib shell]# ls
README  test.txt
[root@gitlib shell]# git status
# 位于分支 master
# 您的分支领先 'origin/master' 共 1 个提交。
#   (使用 "git push" 来发布您的本地提交)
#
# 要提交的变更:
#   (使用 "git reset HEAD <file>..." 撤出暂存区)
#
#       新文件:    123
#
# 尚未暂存以备提交的变更:
#   (使用 "git add/rm <file>..." 更新要提交的内容)
#   (使用 "git checkout -- <file>..." 丢弃工作区的改动)
#
#       删除:      123
#

重置( 将暂存区中的文件取消暂存,但保留工作目录中的更改 )

[root@gitlib shell]# git reset HEAD  .
[root@gitlib shell]# ls
README  test.txt
[root@gitlib shell]# git status
# 位于分支 master
# 您的分支领先 'origin/master' 共 1 个提交。
#   (使用 "git push" 来发布您的本地提交)
#
无文件要提交,干净的工作区
1.6.4 重命名暂存区数据
#没有添加到暂存区的数据直接mv/rename改名即可。
[root@gitlib shell]# touch 111
[root@gitlib shell]# ls
111  README  test.txt
[root@gitlib shell]# mv 111 222
[root@gitlib shell]# ls
222  README  test.txt

#已经添加到暂存区数据:
# git mv README NOTICE

[root@gitlib shell]# ls
222  README  test.txt
[root@gitlib shell]# git mv README notice
[root@gitlib shell]# ls
222  notice  test.txt
[root@gitlib shell]# git status
# 位于分支 master
# 您的分支领先 'origin/master' 共 1 个提交。
#   (使用 "git push" 来发布您的本地提交)
#
# 要提交的变更:
#   (使用 "git reset HEAD <file>..." 撤出暂存区)
#
#       重命名:    README -> notice
#
1.6.5 查看历史记录
• git log   #→查看提交历史记录

• git log -2   #→查看最近几条记录

• git log -p -1  #→-p显示每次提交的内容差异,例如仅查看最近一次差异

• git log --stat -2 #→--stat简要显示数据增改行数,这样能够看到提交中修改过的内容,对文件添加或移动的行数,并在最后列出所有增减行的概要信息

• git log --pretty=oneline #→--pretty根据不同的格式展示提交的历史信息

• git log --pretty=fuller -2 #→以更详细的模式输出提交的历史记录

• git log --pretty=fomat:"%h %cn"  #→查看当前所有提交记录的简短SHA-1哈希字串与提交着的姓名。

使用format参数来指定具体的输出格式

 命令实践

[root@gitlib shell]# git log
commit 948eb2be8143e1f3bcc5f651c7ae6362616c3da8
Author: yanghanxiang <yanghanxiang@aliyun.com>
Date:   Wed Nov 1 19:04:07 2023 +0800

    123 commit

commit 8cb4caf4340578bcf65e426033b3134c1a7bc9b8
Author: yanghanxiang <yanghanxiang@aliyun.com>
Date:   Wed Nov 1 16:41:30 2023 +0800

    first commit

commit 3179b74be61ae10f0a78d57a78b91a02ce12367d
Author: root <root@localhost.localdomain>
Date:   Wed Nov 1 16:37:39 2023 +0800

    first commit

git log -2

[root@gitlib shell]# git log -2
commit ed611aaba0709569c56849b2b1e9d57c5f9a655a
Author: yanghanxiang <yanghanxiang@aliyun.com>
Date:   Wed Nov 1 19:47:48 2023 +0800

    second commit

commit 948eb2be8143e1f3bcc5f651c7ae6362616c3da8
Author: yanghanxiang <yanghanxiang@aliyun.com>
Date:   Wed Nov 1 19:04:07 2023 +0800

    123 commit

git log -p -1

[root@gitlib shell]# echo "336699" >> test.txt 
[root@gitlib shell]# git add test.txt 
[root@gitlib shell]# git commit -m "second commit"
[master ed611aa] second commit
 2 files changed, 1 insertion(+)
 create mode 100644 333

[root@gitlib shell]# git log -p -1
commit ed611aaba0709569c56849b2b1e9d57c5f9a655a
Author: yanghanxiang <yanghanxiang@aliyun.com>
Date:   Wed Nov 1 19:47:48 2023 +0800

    second commit

diff --git a/333 b/333
new file mode 100644
index 0000000..e69de29
diff --git a/test.txt b/test.txt
index 946c72a..bbbffd4 100644
--- a/test.txt
+++ b/test.txt
@@ -1,2 +1,3 @@
 henhao henhao
 sdfsdfgsdvgdfvdvf
+336699
1.6.6 还原历史数据
Git服务程序中有一个叫做HEAD的版本指针,当用户申请还原数据时,其实就是将HEAD指针指向到某个特定的
提交版本,但是因为Git是分布式版本控制系统,为了避免历史记录冲突,故使用了SHA-1计算出十六进制的哈
希字串来区分每个提交版本,另外默认的HEAD版本指针会指向到最近的一次提交版本记录,而上一个提交版本
会叫HEAD^,上上一个版本则会叫做HEAD^^,当然一般会用HEAD~5来表示往上数第五个提交版本。

git reset --hard   hash

git reset --hard HEAD^  #→还原历史提交版本上一次

git reset --hard 3de15d4 #→找到历史还原点的SHA-1值后,就可以还原(值不写全,系统会自动匹配)

测试命令

[root@gitlib shell]# git log
commit ed611aaba0709569c56849b2b1e9d57c5f9a655a
Author: yanghanxiang <yanghanxiang@aliyun.com>
Date:   Wed Nov 1 19:47:48 2023 +0800

    second commit

commit 948eb2be8143e1f3bcc5f651c7ae6362616c3da8
Author: yanghanxiang <yanghanxiang@aliyun.com>
Date:   Wed Nov 1 19:04:07 2023 +0800

    123 commit

commit 8cb4caf4340578bcf65e426033b3134c1a7bc9b8
Author: yanghanxiang <yanghanxiang@aliyun.com>
Date:   Wed Nov 1 16:41:30 2023 +0800

    first commit

commit 3179b74be61ae10f0a78d57a78b91a02ce12367d
Author: root <root@localhost.localdomain>
Date:   Wed Nov 1 16:37:39 2023 +0800

#还原数据
[root@gitlib shell]# git reset --hard 948e
HEAD 现在位于 948eb2b 123 commit
[root@gitlib shell]# ls
README  test.txt

#再还原数据
[root@gitlib shell]# git reset --hard ed611
HEAD 现在位于 ed611aa second commit
[root@gitlib shell]# ls
333  README  test.txt
1.6.7 还原未来数据

什么是未来数据?就是你还原到历史数据了,但是你后悔了,想撤销更改,但是git log已经找不到这个版本了。

git reflog #→查看未来历史更新点

测试命令

[root@gitlib shell]# git reflog
ed611aa HEAD@{0}: reset: moving to ed611
948eb2b HEAD@{1}: reset: moving to 948e
ed611aa HEAD@{2}: commit: second commit
948eb2b HEAD@{3}: commit: 123 commit
8cb4caf HEAD@{4}: checkout: moving from slave to master
8cb4caf HEAD@{5}: checkout: moving from master to slave
8cb4caf HEAD@{6}: commit: first commit
3179b74 HEAD@{7}: commit (initial): first commit
[root@gitlib shell]# 
1.6.8 标签使用
前面回滚使用的是一串字符串,又长又难记。

git tag v1.0   #→当前提交内容打一个标签(方便快速回滚),每次提交都可以打个tag。

git tag          #→查看当前所有的标签

git show v1.0   #→查看当前1.0版本的详细信息

git tag v1.2 -m "version 1.2 release is test"  #→创建带有说明的标签,-a指定标签名字,-m指定说明文字

git tag -d v1.0   #→我们为同一个提交版本设置了两次标签,删除之前的v1.0

测试命令

[root@gitlib shell]# git reset --hard 3179b7
HEAD 现在位于 3179b74 first commit
[root@gitlib shell]# ls
test.txt
[root@gitlib shell]# git reset --hard ed611
HEAD 现在位于 ed611aa second commit
[root@gitlib shell]# ls
333  README  test.txt

[root@gitlib shell]# git tag  v20231117
[root@gitlib shell]# git tag
v20231117
1.6.9 对比数据

git diff可以对比当前文件与仓库已保存文件的区别,知道了对README作了什么修改

后,再把它提交到仓库就放⼼多了。

git diff README
git diff --name-only HEAD HEAD^
git diff --name-only head_id head_id2

测试命令:

[root@gitlib shell]# echo "998877445622"  >> test.txt 
[root@gitlib shell]# git diff test.txt 
diff --git a/test.txt b/test.txt
index bbbffd4..7291a84 100644
--- a/test.txt
+++ b/test.txt
@@ -1,3 +1,4 @@
 henhao henhao
 sdfsdfgsdvgdfvdvf
 336699
+998877445622

[root@gitlib shell]# git diff --name-only 3179b7 ed611
333
README
test.txt

1.7 分支结构

        在实际的项目开发中,尽量保证master分支稳定,仅用于发布新版本,平时不要随便直接修改里面的数据文件。

        那在哪干活呢?干活都在dev分支上。每个人从dev分支创建自己个人分支,开发完合并到dev分支,最后dev分支合并到master分支。所以团队的合作分支看起来会像下图那样。

1.7.1 分支切换
[root@gitlib shell]# git branch mahong   #创建分支
[root@gitlib shell]# git branch          #查看分支
  mahong
* master    # *代表目前所在分支
  slave
[root@gitlib shell]# git checkout mahong
M       test.txt
切换到分支 'mahong'
[root@gitlib shell]# git branch 
* mahong
  master
  slave

在新建分支 马宏 中进行修改

[root@gitlib shell]# cat README 
mahong is a good boy
[root@gitlib shell]# echo "111111111111" >> README 
[root@gitlib shell]# git add README 
[root@gitlib shell]# git commit -m "README111"
[mahong be811e6] README111
 1 file changed, 1 insertion(+)
[root@gitlib shell]# git status
# 位于分支 mahong
无文件要提交,干净的工作区

回到master分支

[root@gitlib shell]# git checkout master
切换到分支 'master'
您的分支领先 'origin/master' 共 2 个提交。
  (使用 "git push" 来发布您的本地提交)
[root@gitlib shell]# cat README 
[root@gitlib shell]# git log  -1
commit ed611aaba0709569c56849b2b1e9d57c5f9a655a
Author: yanghanxiang <yanghanxiang@aliyun.com>
Date:   Wed Nov 1 19:47:48 2023 +0800

    second commit

合并代码

[root@gitlib shell]# git merge mahong
更新 ed611aa..be811e6
Fast-forward
 README   | 2 ++
 test.txt | 1 +
 2 files changed, 3 insertions(+)
[root@gitlib shell]# git status
# 位于分支 master
# 您的分支领先 'origin/master' 共 5 个提交。
#   (使用 "git push" 来发布您的本地提交)
#
无文件要提交,干净的工作区
[root@gitlib shell]# cat README 
mahong is a good boy
111111111111
1.7.2 合并失败解决

模拟冲突,在文件的同一行做不同修改  

在master 分支进行修改

[root@gitlab shell]# cat README 
This is git_data readme
1901
[root@gitlab shell]#  echo '1901-git' > README 
[root@gitlab shell]#  git commit -m 'newrain 1901-git'
[master 4e6c548] newrain 1901-git
 1 file changed, 1 insertion(+), 2 deletions(-)

切换到mahong分支

[root@gitlab shell]# git checkout mahong
Switched to branch 'mahong'
[root@gitlab shell]# cat README 
This is git_data readme
1901
[root@gitlab shell]# echo 'mahong' >> README 
[root@gitlab shell]# git commit -m '1901-git-check'
# On branch mahong
# 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:   README
#
no changes added to commit (use "git add" and/or "git commit -a")

回到master分区,进行合并,出现冲突

[root@gitlab shell]# git checkout master 
切换到分支 'master'
[root@gitlab shell]# git merge linux
自动合并 README
冲突(内容):合并冲突于 README
自动合并失败,修正冲突然后提交修正的结果。

解决冲突

[root@gitlab shell]# vim README 
This is git_data readme
1901
newrain
meger test ti
meger test master

手工解决冲突

[root@gitlab shell]# git commit -a -m "merge-ti-test"
[master 2594b2380] merge-ti-test
1.7.3 删除分支

因为之前已经合并了mahong分支,所以现在看到它在列表中。 在这个列表中分支名字前没有 * 号的分支通常可以使用 git branch -d 删除掉;你已经将它们的工作整合到了另一个分支,所以并不会失去任何东西。

查看所有包含未合并工作的分支,可以运行 git branch --no-merged**:

git branch --no-merged
  testing

这里显示了其他分支。 因为它包含了还未合并的工作,尝试使用 git branch -d 命令删除它时会失败:

git branch -d testing
error: The branch 'testing' is not fully merged.
If you are sure you want to delete it, run 'git branch -D testing'. 

 如果真的想要删除分支并丢掉那些工作,如同帮助信息里所指出的,可以使用 -D 选项强制删除它。

  • 18
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值