Git的分支应用

10 篇文章 0 订阅
10 篇文章 0 订阅
Git 的分支,其实本质上仅仅是指向提交对象的可变指针。 主分支master   

在企业中,master分支是要非常稳定的,不能直接修改master分支里面的内容,使用的时候创建一个普通分支,等完成工作之后,将其合并到master分支上。

[root@git ~/git_test]# git log  --oneline  --decorate
cd9d652 (HEAD, master) modify a.txt  3		#当前的指针指向了master
f88afdb modify a.txt 2
8e84d34 modify a.txt 1
51104b9 rename a.log a.txt
2345032 rename a.txt  a.log
c3e5985 add new files

[root@git ~/git_test]# #显示系统所有的分支	显示当前在哪个分支下面工作	* 的位置,表示在哪一个分支
[root@git ~/git_test]# git branch 
* master

[root@git ~/git_test]# #创建一个普通分支
[root@git ~/git_test]# git branch qls
[root@git ~/git_test]# git branch 
* master
  qls

[root@git ~/git_test]# #切换分支
[root@git ~/git_test]# git checkout qls
Switched to branch 'qls'
[root@git ~/git_test]# git branch 
  master
* qls


#创建一个测试文件
[root@git ~/git_test]# ll
total 4
-rw-r--r-- 1 root root 30 2020-05-11 15:47 a.txt
-rw-r--r-- 1 root root  0 2020-05-11 14:48 b.txt
-rw-r--r-- 1 root root  0 2020-05-11 14:36 c.txt
[root@git ~/git_test]# touch qls.txt
[root@git ~/git_test]# git add .
[root@git ~/git_test]# git commit -m "add new qls.txt qls"
[qls 1520f92] add new qls.txt qls
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 qls.txt

[root@git ~/git_test]# git log  --oneline  --decorate
1520f92 (HEAD, qls) add new qls.txt qls		#显示该条记录是qls分支提交的
cd9d652 (master) modify a.txt  3
f88afdb modify a.txt 2
8e84d34 modify a.txt 1
51104b9 rename a.log a.txt
2345032 rename a.txt  a.log
c3e5985 add new files

[root@git ~/git_test]# #切换到master分支
[root@git ~/git_test]# git checkout  master 
Switched to branch 'master'
[root@git ~/git_test]# git branch 
* master
  qls
[root@git ~/git_test]# ll		#普通分支的提交,在master分支上面没有
total 4
-rw-r--r-- 1 root root 30 2020-05-11 15:47 a.txt
-rw-r--r-- 1 root root  0 2020-05-11 14:48 b.txt
-rw-r--r-- 1 root root  0 2020-05-11 14:36 c.txt

[root@git ~/git_test]# #合并分支
[root@git ~/git_test]# git merge qls
Updating cd9d652..1520f92
Fast-forward
 qls.txt | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 qls.txt
[root@git ~/git_test]# ll
total 4
-rw-r--r-- 1 root root 30 2020-05-11 15:47 a.txt
-rw-r--r-- 1 root root  0 2020-05-11 14:48 b.txt
-rw-r--r-- 1 root root  0 2020-05-11 14:36 c.txt
-rw-r--r-- 1 root root  0 2020-05-12 09:25 qls.txt

[root@git ~/git_test]# git reflog 
1520f92 HEAD@{0}: merge qls: Fast-forward
cd9d652 HEAD@{1}: checkout: moving from qls to master
1520f92 HEAD@{2}: commit: add new qls.txt qls
cd9d652 HEAD@{3}: checkout: moving from master to qls
cd9d652 HEAD@{4}: reset: moving to cd9d652
8e84d34 HEAD@{5}: reset: moving to 8e84d34
cd9d652 HEAD@{6}: commit: modify a.txt 3
f88afdb HEAD@{7}: commit: modify a.txt 2
8e84d34 HEAD@{8}: commit: modify a.txt 1
51104b9 HEAD@{9}: commit: rename a.log a.txt
2345032 HEAD@{10}: commit: rename a.txt a.log
c3e5985 HEAD@{11}: commit (initial): add new files


#master身份创建文件

[root@git ~/git_test]# git branch 
* master
  qls
[root@git ~/git_test]# touch  d.txt
[root@git ~/git_test]# git add .
[root@git ~/git_test]# git commit -m "add new d.txt master"
[master 76cd6d4] add new d.txt master
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 d.txt
[root@git ~/git_test]# ll
total 4
-rw-r--r-- 1 root root 30 2020-05-11 15:47 a.txt
-rw-r--r-- 1 root root  0 2020-05-11 14:48 b.txt
-rw-r--r-- 1 root root  0 2020-05-11 14:36 c.txt
-rw-r--r-- 1 root root  0 2020-05-12 09:28 d.txt
-rw-r--r-- 1 root root  0 2020-05-12 09:25 qls.txt


[root@git ~/git_test]# #切换到普通分支
[root@git ~/git_test]# git checkout  qls
Switched to branch 'qls'
[root@git ~/git_test]# ll
total 4
-rw-r--r-- 1 root root 30 2020-05-11 15:47 a.txt
-rw-r--r-- 1 root root  0 2020-05-11 14:48 b.txt
-rw-r--r-- 1 root root  0 2020-05-11 14:36 c.txt
-rw-r--r-- 1 root root  0 2020-05-12 09:25 qls.txt
[root@git ~/git_test]# git merge master 			#合并主分支的内容
Updating 1520f92..76cd6d4
Fast-forward
 d.txt | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 d.txt
[root@git ~/git_test]# ll
total 4
-rw-r--r-- 1 root root 30 2020-05-11 15:47 a.txt
-rw-r--r-- 1 root root  0 2020-05-11 14:48 b.txt
-rw-r--r-- 1 root root  0 2020-05-11 14:36 c.txt
-rw-r--r-- 1 root root  0 2020-05-12 09:30 d.txt
-rw-r--r-- 1 root root  0 2020-05-12 09:25 qls.txt

#合并冲突

[root@git ~/git_test]# git branch 
  master
* qls
[root@git ~/git_test]# echo "qls" > c.txt 
[root@git ~/git_test]# cat c.txt
qls
[root@git ~/git_test]# git commit -am "modify c.txt qls"
[qls a49ccaf] modify c.txt qls
 1 file changed, 1 insertion(+)


[root@git ~/git_test]# git checkout  master 
Switched to branch 'master'
[root@git ~/git_test]# git branch 
* master
  qls
[root@git ~/git_test]# cat c.txt 
[root@git ~/git_test]# echo "master" > c.txt 
[root@git ~/git_test]# git commit -am "modify c.txt master"
[master 428aecf] modify c.txt master
 1 file changed, 1 insertion(+)
[root@git ~/git_test]# cat c.txt 
master

[root@git ~/git_test]# git merge  qls		#合并的时候发生冲突
Auto-merging c.txt
CONFLICT (content): Merge conflict in c.txt
Automatic merge failed; fix conflicts and then commit the result.

#手动修改合并冲突的文件
[root@git ~/git_test]# vim c.txt 
[root@git ~/git_test]# cat c.txt
master
qls
[root@git ~/git_test]# git commit -am "modify c.txt merge master and qls" 
[master 426d222] modify c.txt merge master and qls
[root@git ~/git_test]# cat c.txt 
master
qls

[root@git ~/git_test]# git log  --oneline  --decorate
426d222 (HEAD, master) modify c.txt merge master and qls
428aecf modify c.txt master
a49ccaf (qls) modify c.txt qls
76cd6d4 add new d.txt master
1520f92 add new qls.txt qls
cd9d652 modify a.txt  3
f88afdb modify a.txt 2
8e84d34 modify a.txt 1
51104b9 rename a.log a.txt
2345032 rename a.txt  a.log
c3e5985 add new files
[root@git ~/git_test]# git reflog 
426d222 HEAD@{0}: commit (merge): modify c.txt merge master and qls
428aecf HEAD@{1}: commit: modify c.txt master
76cd6d4 HEAD@{2}: checkout: moving from qls to master
a49ccaf HEAD@{3}: commit: modify c.txt qls
76cd6d4 HEAD@{4}: merge master: Fast-forward
1520f92 HEAD@{5}: checkout: moving from master to qls
76cd6d4 HEAD@{6}: commit: add new d.txt master
1520f92 HEAD@{7}: merge qls: Fast-forward
cd9d652 HEAD@{8}: checkout: moving from qls to master
1520f92 HEAD@{9}: commit: add new qls.txt qls
cd9d652 HEAD@{10}: checkout: moving from master to qls
cd9d652 HEAD@{11}: reset: moving to cd9d652
8e84d34 HEAD@{12}: reset: moving to 8e84d34
cd9d652 HEAD@{13}: commit: modify a.txt 3
f88afdb HEAD@{14}: commit: modify a.txt 2
8e84d34 HEAD@{15}: commit: modify a.txt 1
51104b9 HEAD@{16}: commit: rename a.log a.txt
2345032 HEAD@{17}: commit: rename a.txt a.log
c3e5985 HEAD@{18}: commit (initial): add new files

#删除分支

[root@git ~/git_test]# git branch 
* master
  qls
[root@git ~/git_test]# git branch  -d qls
Deleted branch qls (was a49ccaf).
[root@git ~/git_test]# git branch 
* master
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值