Git详解

1.Git简介

官网:Git

Git 官方中文手册 Git - Book

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

1.1 Git历史

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

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

2.Git安装

2.1环境说明

[root@localhost ~]# rpm -qa centos-release
centos-release-7-9.2009.0.el7.centos.x86_64
[root@localhost ~]# uname -a
Linux localhost.localdomain 3.10.0-1160.el7.x86_64 #1 SMP Mon Oct 19 16:18:59 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
[root@localhost ~]# getenforce 
Permissive
[root@localhost ~]# 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)

本文使用的linux系统均为该系统

本文使用的windows系统为 Microsoft Windows

192.168.43.129

本地库

git

192.168.43.139

远端库(裸库)

localhost

两台机器均要关闭防火墙和selinux

使用Git时需要获取帮助,可以使用一下三种命令

git help <verb>
git <verb> --help
man git-<verb>
#例如,要想获得配置命令的手册,执行
git help config

2.2安装

yum安装git
[root@localhost ~]# yum -y install git
编译安装git

编译安装可以安装较新版本的git

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  

2.3初次运行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@localhost ~]# git config --global user.name "yanghanxiang"
[root@localhost ~]# git config --global user.email "yanghanxiang@aliyun.com"
[root@localhost ~]# git config --global color.ui true
[root@localhost ~]# git config --list
user.name=yanghanxiang
user.email=yanghanxiang@aliyun.com
color.ui=true

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

#生成的配置文件
[root@git ~]# cd
[root@git ~]# cat .gitconfig
[user]
        name = leyi
        email = 2832708006@qq.com
[color]
    ui = true

2.4获取git仓库(初始化仓库)

创建裸库(远端)
[root@localhost ~]# useradd git
[root@localhost ~]# passwd git
[root@localhost ~]# mkdir /git-root/
[root@localhost ~]# cd /git-root/
[root@localhost ~]# git init --bare shell.git
Initialized empty Git repository in /git-root/shell.git/
[root@localhost ~]# chown -R git.git shell.git
创建本地库
[root@git ~]# git clone git@192.168.43.139:/git-root/shell.git
正克隆到 'shell'...
The authenticity of host '192.168.43.139 (192.168.43.139)' can't be established.
ECDSA key fingerprint is SHA256:Nq5mvWUl9pwyj9XCPm6K7qQoiS8q49M5yjDuoB3O3es.
ECDSA key fingerprint is MD5:a5:e3:81:c1:b8:4c:c5:13:f5:a0:1c:a4:c3:6b:6e:74.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.43.139' (ECDSA) to the list of known hosts.
git@192.168.43.139's password: 
warning: 您似乎克隆了一个空版本库。

#创建测试文件并将其推动到远端库
[root@git ~]# ls
anaconda-ks.cfg  my_system_initialize_1.sh  shell
[root@git ~]# cd shell/
[root@git shell]# ls
[root@git shell]# vim test1   #创建一个测试文件
[root@git shell]# cat test1
I guess Peter Pan was right

[root@git shell]# git add test1  #将文件由工作目录推送到暂存区
[root@git shell]# git commit -m 'first commit'  #将文件由暂存区推送到本地库
[master(根提交) ea1ee99] first commit
 1 file changed, 1 insertion(+)
 create mode 100644 test1
[root@git shell]# git push origin master   #将文件由本地库推送到远端库
git@192.168.43.139's password: 
Counting objects: 3, done.
Writing objects: 100% (3/3), 229 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@192.168.43.139:/git-root/shell.git
 * [new branch]      master -> master

2.5Git命令常规操作

常用命令说明

命令

命令说明

#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 对象

常用操作示意图

文件的状态变化周期

创建文件
[root@git shell]# touch test2
[root@git shell]# vim test2
[root@git shell]# cat test2  #随意往里面写点东西
I guess Peter Pan was right
Growing up's a waste of time
[root@git shell]# git status  #查看一下它的状态
# 位于分支 master
# 未跟踪的文件:
#   (使用 "git add <file>..." 以包含要提交的内容)
#
#       test2
提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)
[root@git shell]# ll
总用量 8
-rw-r--r--. 1 root root 28 10月 19 14:39 test1
-rw-r--r--. 1 root root 57 10月 19 14:51 test2

添加文件跟踪(git add 文件名)

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

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

[root@git 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
│       └── remotes
│           └── origin
│               └── master
├── objects
│   ├── 3b
│   │   └── 9179b3aa80421a38aab3c59bad8b85b651dd80
│   ├── 40
│   │   └── 67515cba2baa9b5eeb8fdd2040b8a2fc927606
│   ├── bd
│   │   └── 61feccff453cee9b3bad1ef07798c5628f42a6
│   ├── ea
│   │   └── 1ee99c2493ddff3479d5d1a48f71f4792fb02f
│   ├── info
│   └── pack
└── refs
    ├── heads
    │   └── master
    ├── remotes
    │   └── origin
    │       └── master
    └── tags

20 directories, 24 files

#使用以下命令可以查看文件的内容
[root@git shell]# git cat-file -p 4067515cba    
I guess Peter Pan was right
Growing up's a waste of time

”4067515cba“ 40为目录名,67515cba为目录下边文件名的前一部分

由工作区提交到本地库

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

由本地仓库提交到远程仓库(不指定文件名)

[root@git shell]# git push origin master
git@192.168.43.139's password: 
Counting objects: 4, done.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 312 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@192.168.43.139:/git-root/shell.git
   ea1ee99..6434b48  master -> master
   
   #查看git的状态
[root@git shell]# git status
# 位于分支 master
无文件要提交,干净的工作区

提交后的git目录状态

[root@git 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
│       └── remotes
│           └── origin
│               └── master
├── objects
│   ├── 3b
│   │   └── 9179b3aa80421a38aab3c59bad8b85b651dd80
│   ├── 40
│   │   └── 67515cba2baa9b5eeb8fdd2040b8a2fc927606
│   ├── 64
│   │   └── 34b488beb0f768456bd361f2359417d80c861a
│   ├── bd
│   │   └── 61feccff453cee9b3bad1ef07798c5628f42a6
│   ├── c5
│   │   └── b6fecb03d9d55182600adc942c1f031e3eb6ef
│   ├── ea
│   │   └── 1ee99c2493ddff3479d5d1a48f71f4792fb02f
│   ├── info
│   └── pack
└── refs
    ├── heads
    │   └── master
    ├── remotes
    │   └── origin
    │       └── master
    └── tags

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

常规方法

git add  *
git commit

简便方法

git commit -a  -m "注释信息"
-a 表示直接提交

Tell the command to automatically stage files that have been modified and deleted, but new files you have not told Git about are
not affected.
删除git内的文件

命令说明

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

已经添加到暂存区数据: git rm --cached 文件名 将文件从git暂存区域的追踪列表移除(并不会删除当前工作目录内的数据文件)

[root@git shell]# echo "test rm " >> test3
[root@git shell]# git add test3
[root@git shell]# git status
# 位于分支 master
# 要提交的变更:
#   (使用 "git reset HEAD <file>..." 撤出暂存区)
#
#       新文件:    test3
#
[root@git shell]# git rm --cached test3
rm 'test3'
[root@git shell]# git status
# 位于分支 master
# 未跟踪的文件:
#   (使用 "git add <file>..." 以包含要提交的内容)
#
#       test3
提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)

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

[root@git shell]# ll
总用量 12
-rw-r--r--. 1 root root 28 10月 19 14:39 test1
-rw-r--r--. 1 root root 57 10月 19 14:51 test2
-rw-r--r--. 1 root root  9 10月 19 16:02 test3
[root@git shell]# git add test3   #提交文件到暂存区
[root@git shell]# git status     #查看一下状态,此时文件还在暂存区
# 位于分支 master
# 要提交的变更:
#   (使用 "git reset HEAD <file>..." 撤出暂存区)
#
#       新文件:    test3
#

[root@git shell]# git rm -f test3
rm 'test3'
[root@git shell]# ll 
总用量 8
-rw-r--r--. 1 root root 28 10月 19 14:39 test1
-rw-r--r--. 1 root root 57 10月 19 14:51 test2
[root@git shell]# git status
# 位于分支 master
无文件要提交,干净的工作区

暂存区和工作目录已经没有文件了

将文件由暂存区域撤出 git reset HEAD 文件名

[root@git shell]# echo "hahaha" >> test4
[root@git shell]# git add test4
[root@git shell]# git status
# 位于分支 master
# 要提交的变更:
#   (使用 "git reset HEAD <file>..." 撤出暂存区)
#
#       新文件:    test4
#
[root@git shell]# git reset HEAD test4
[root@git shell]# git status
# 位于分支 master
# 未跟踪的文件:
#   (使用 "git add <file>..." 以包含要提交的内容)
#
#       test4
提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)
重命名暂存区域数据

• 没有添加到暂存区的数据直接mv/rename改名即可。

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

git mv test4 test5   #(工作目录与暂存区均改)
[root@git shell]# git add test4
[root@git shell]# git status 
# 位于分支 master
# 要提交的变更:
#   (使用 "git reset HEAD <file>..." 撤出暂存区)
#
#       新文件:    test4
#
[root@git shell]# git mv test4 test5
[root@git shell]# git status 
# 位于分支 master
# 要提交的变更:
#   (使用 "git reset HEAD <file>..." 撤出暂存区)
#
#       新文件:    test5
#
[root@git shell]# ll
总用量 12
-rw-r--r--. 1 root root 28 10月 19 14:39 test1
-rw-r--r--. 1 root root 57 10月 19 14:51 test2
-rw-r--r--. 1 root root  7 10月 19 16:10 test5
查看历史记录

• 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参数来指定具体的输出格式

格式

说明

%s

提交说明。

%cd

提交日期。

%an

作者的名字。

%cn

提交者的姓名。

%ce

提交者的电子邮件。

%H

提交对象的完整SHA-1哈希字串。

%h

提交对象的简短SHA-1哈希字串。

%T

树对象的完整SHA-1哈希字串。

%t

树对象的简短SHA-1哈希字串。

%P

父对象的完整SHA-1哈希字串。

%p

父对象的简短SHA-1哈希字串。

%ad

作者的修订时间。

[root@git shell]# git log
commit d04f3a910b21bb385bf5415d00b38578202bc394
Author: leyi <2832708006@qq.com>
Date:   Thu Oct 19 16:20:08 2023 +0800

    echo2

commit 5d5e4a73653a27e4847b4356cbd6038a0c6439dd
Author: leyi <2832708006@qq.com>
Date:   Thu Oct 19 16:19:19 2023 +0800

    echo

commit 0f9b59f023e6dd0c5d9126f0a154524371f7025e
Author: leyi <2832708006@qq.com>
Date:   Thu Oct 19 16:18:20 2023 +0800

    test

commit 6434b488beb0f768456bd361f2359417d80c861a
Author: leyi <2832708006@qq.com>
Date:   Thu Oct 19 15:40:19 2023 +0800

    first commit

commit ea1ee99c2493ddff3479d5d1a48f71f4792fb02f
Author: leyi <2832708006@qq.com>
Date:   Thu Oct 19 14:40:24 2023 +0800
还原历史数据

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@git shell]# git log
commit d04f3a910b21bb385bf5415d00b38578202bc394
Author: leyi <2832708006@qq.com>
Date:   Thu Oct 19 16:20:08 2023 +0800

    echo2

commit 5d5e4a73653a27e4847b4356cbd6038a0c6439dd
Author: leyi <2832708006@qq.com>
Date:   Thu Oct 19 16:19:19 2023 +0800

    echo

commit 0f9b59f023e6dd0c5d9126f0a154524371f7025e
Author: leyi <2832708006@qq.com>
Date:   Thu Oct 19 16:18:20 2023 +0800

    test

commit 6434b488beb0f768456bd361f2359417d80c861a
Author: leyi <2832708006@qq.com>
Date:   Thu Oct 19 15:40:19 2023 +0800

    first commit

commit ea1ee99c2493ddff3479d5d1a48f71f4792fb02f
Author: leyi <2832708006@qq.com>
Date:   Thu Oct 19 14:40:24 2023 +0800

还原数据

[root@git shell]# git reset --hard 5d5e
HEAD 现在位于 5d5e4a7 echo
我们将数据还原到了“echo”的这一版
还原未来数据

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

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

测试命令

[root@git shell]# git reflog
5d5e4a7 HEAD@{0}: reset: moving to 5d5e
d04f3a9 HEAD@{1}: commit: echo2
5d5e4a7 HEAD@{2}: commit: echo
0f9b59f HEAD@{3}: commit: test
6434b48 HEAD@{4}: commit: first commit
ea1ee99 HEAD@{5}: commit (initial): first commit

[root@git shell]# git reset --hard d04f3a9 
HEAD 现在位于 d04f3a9 echo2
数据本来是在echo的那一版本,现在还原到了echo2的这一版
标签使用

前面回滚使用的是一串字符串,又长又难记。

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@git shell]# git tag v1.0   #给当前的内容打上标签
[root@git shell]# git tag   #查看当前所有标签
v1.0  
[root@git shell]# git show v1.0   #查看标签的详细信息
commit d04f3a910b21bb385bf5415d00b38578202bc394
Author: leyi <2832708006@qq.com>
Date:   Thu Oct 19 16:20:08 2023 +0800

    echo2

diff --git a/test2 b/test2
index 3000c23..ab8ed07 100644
--- a/test2
+++ b/test2
@@ -1,3 +1,4 @@
 I guess Peter Pan was right
 Growing up's a waste of time
 So I think I'll fly away
+Set a course for brighter days
对比数据

git diff可以对比当前文件与仓库已保存文件的区别,知道了对文件做了什么修改后,再把它提交到仓库就放⼼多了。

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

2.6分支结构

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

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

分支切换
[root@git shell]# git branch leyi  #创建新分支
[root@git shell]# git branch  #查看分支
  leyi
* master
[root@git shell]# git checkout leyi  #切换分支
切换到分支 'leyi'
[root@git shell]# git branch   #此时再查看,已经切换到leyi分支上了
* leyi
  master

在leyi分支进行修改

[root@git shell]# cat test2
I guess Peter Pan was right
Growing up's a waste of time
So I think I'll fly away
Set a course for brighter days
[root@git shell]# echo "Set a course for brighter days" >> test2
[root@git shell]# git add .
[root@git shell]# git commit -m "Set a course for brighter days"
[leyi a5a68cd] Set a course for brighter days
 1 file changed, 1 insertion(+)
[root@git shell]# git status
# 位于分支 leyi
无文件要提交,干净的工作区

回到master分支

[root@git shell]# git checkout  master
切换到分支 'master'
您的分支领先 'origin/master' 共 3 个提交。
  (使用 "git push" 来发布您的本地提交)
[root@git shell]# cat test2
I guess Peter Pan was right
Growing up's a waste of time
So I think I'll fly away
Set a course for brighter days
[root@git shell]# git log -1
commit d04f3a910b21bb385bf5415d00b38578202bc394
Author: leyi <2832708006@qq.com>
Date:   Thu Oct 19 16:20:08 2023 +0800

    echo2
合并代码
[root@git shell]# git merge leyi
更新 d04f3a9..a5a68cd
Fast-forward
 test2 | 1 +
 1 file changed, 1 insertion(+)
[root@git shell]# git status
# 位于分支 master
# 您的分支领先 'origin/master' 共 4 个提交。
#   (使用 "git push" 来发布您的本地提交)
#
无文件要提交,干净的工作区
[root@git shell]# cat test2
I guess Peter Pan was right
Growing up's a waste of time
So I think I'll fly away
Set a course for brighter days
Set a course for brighter days
合并失败解决

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

在master分支进行修改

[root@git shell]# cat test2
I guess Peter Pan was right
Growing up's a waste of time
So I think I'll fly away
Set a course for brighter days
Set a course for brighter days
[root@git shell]# echo "master" >> test2
[root@git shell]# git commit -m 'master'
[master 4e6c548] master
 1 file changed, 1 insertion(+), 2 deletions(-)

切换到leyi分支

[root@git shell]# git checkout leyi
Switched to branch 'leyi'
[root@git shell]# cat test2 
I guess Peter Pan was right
Growing up's a waste of time
So I think I'll fly away
Set a course for brighter days
Set a course for brighter days
[root@git shell]# echo 'leyi' >> test2 
[root@git shell]# git commit -m 'leyi'
# On branch leyi
# 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:  test2
#
no changes added to commit (use "git add" and/or "git commit -a")

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

[root@git shell]#  git checkout master 
切换到分支 'master'
[root@git shell]# git merge leyi
自动合并 test2
冲突(内容):合并冲突于 test2
自动合并失败,修正冲突然后提交修正的结果。
解决冲突
手工解决
[root@git shell]# git commit -a -m "merge-ti-test"
[master 2594b2380] merge-ti-test
删除分支

因为之前已经合并了leyi分支,所以现在看到它在列表中。 在这个列表中分支名字前没有 * 号的分支通常可以使用 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 选项强制删除它。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值