git之merge和rebase的区别

时间:2022-06-01 21:18:58

merge合并

# merge操作

第一步:
# 先创建一个目录,在主分支提交3个txt文件
[root@luchuangao]# mkdir oldboy
[root@luchuangao]# git init 初始化
[root@luchuangao]# echo "a" > a.txt
[root@luchuangao]# echo "b" > b.txt 
[root@luchuangao]# echo "c" > c.txt 
[root@luchuangao]# git add a.txt
[root@luchuangao]# git commit -m 'a'
[master(根提交) ac03ef7] a
 1 file changed, 1 insertion(+)
 create mode 100644 a.txt
[root@luchuangao]# git add b.txt
[root@luchuangao]# git commit -m 'b'
[master 65c273b] b
 1 file changed, 1 insertion(+)
 create mode 100644 b.txt
[root@luchuangao]# git add c.txt
[root@luchuangao]# git commit -m 'c'
[master ddb2007] c
 1 file changed, 1 insertion(+)
 create mode 100644 c.txt

第二步:
# 创建并切换到testing分支,并提交2个txt文件
[root@luchuangao]# git checkout -b testing
切换到一个新分支 'testing'
[root@luchuangao]# echo "test1" > test1.txt
[root@luchuangao]# echo "test2" > test2.txt 
[root@luchuangao]# git add test1.txt
[root@luchuangao]# git commit -m 'test1'    
[testing 4d18278] test1
 1 file changed, 1 insertion(+)
 create mode 100644 test1.txt
[root@luchuangao]# git add test2.txt    
[root@luchuangao]# git commit -m 'test2'
[testing 2c7ef37] test2
 1 file changed, 1 insertion(+)
 create mode 100644 test2.txt

# 查看testing分支日志
 [root@proxy-nfs oldboy]# git log
commit 2c7ef374134f0158fab77137717d7ba1c1281a36
Author: luchuangao <luchuangao@126.com>
Date:   Fri Oct 27 20:42:26 2017 +0800

    test2

commit 4d182786a2199f10351dd69e819d6878513c8456
Author: luchuangao <luchuangao@126.com>
Date:   Fri Oct 27 20:42:17 2017 +0800

    test1

commit ddb20076027cca6b310a30468445ce82e96dcbcd
Author: luchuangao <luchuangao@126.com>
Date:   Fri Oct 27 20:41:13 2017 +0800

    c

commit 65c273b1512cbad0c4325d21f1cd6c421d14f2b3
Author: luchuangao <luchuangao@126.com>
Date:   Fri Oct 27 20:41:07 2017 +0800

    b

commit ac03ef772d4e01914d0917676d2f33eca8c0bb11
Author: luchuangao <luchuangao@126.com>
Date:   Fri Oct 27 20:40:57 2017 +0800

    a

第三步:
# 切换至主分支,再在主分支提交2个txt文件
[root@proxy-nfs oldboy]# git checkout master
切换到分支 'master'
[root@proxy-nfs oldboy]# echo "master1" > master1.txt
[root@proxy-nfs oldboy]# echo "master2" > master2.txt 
[root@proxy-nfs oldboy]# git add master1.txt
[root@proxy-nfs oldboy]# git commit -m 'master1'
[master 9353089] master1
 1 file changed, 1 insertion(+)
 create mode 100644 master1.txt
[root@proxy-nfs oldboy]# git add master2.txt    
[root@proxy-nfs oldboy]# git commit -m 'master2'
[master 1cca7b6] master2
 1 file changed, 1 insertion(+)
 create mode 100644 master2.txt

# 查看master分支日志
[root@proxy-nfs oldboy]# git log
commit 1cca7b62bbb5b7f7e62a7d2c55a76618109bbffc
Author: luchuangao <luchuangao@126.com>
Date:   Fri Oct 27 20:43:37 2017 +0800

    master2

commit 9353089557089753359445242c32352fcb6b32f0
Author: luchuangao <luchuangao@126.com>
Date:   Fri Oct 27 20:43:30 2017 +0800

    master1

commit ddb20076027cca6b310a30468445ce82e96dcbcd
Author: luchuangao <luchuangao@126.com>
Date:   Fri Oct 27 20:41:13 2017 +0800

    c

commit 65c273b1512cbad0c4325d21f1cd6c421d14f2b3
Author: luchuangao <luchuangao@126.com>
Date:   Fri Oct 27 20:41:07 2017 +0800

    b

commit ac03ef772d4e01914d0917676d2f33eca8c0bb11
Author: luchuangao <luchuangao@126.com>
Date:   Fri Oct 27 20:40:57 2017 +0800

    a

	
第四步:
# 合并testing,不做修改,直接wq保存
[root@luchuangao oldboy]# git merge testing
Merge branch 'testing'

# 请输入一个提交信息以解释此合并的必要性,尤其是将一个更新后的上游分支
# 合并到主题分支。
#
# 以 '#' 开头的行将被忽略,而且空提交说明将会终止提交。
 
".git/MERGE_MSG" 6L, 238C written
Merge made by the 'recursive' strategy.
 test1.txt | 1 +
 test2.txt | 1 +
 2 files changed, 2 insertions(+)
 create mode 100644 test1.txt
 create mode 100644 test2.txt

# 查看master分支日志,多了一个“Merge branch 'testing'” 日志记录
[root@luchuangao oldboy]# git log
commit ea4c21bd398bd1a766bc8a498df44c8e2dc4993e
Merge: 1cca7b6 2c7ef37
Author: luchuangao <luchuangao@126.com>
Date:   Fri Oct 27 20:51:14 2017 +0800

    Merge branch 'testing'

commit 1cca7b62bbb5b7f7e62a7d2c55a76618109bbffc
Author: luchuangao <luchuangao@126.com>
Date:   Fri Oct 27 20:43:37 2017 +0800

    master2

commit 9353089557089753359445242c32352fcb6b32f0
Author: luchuangao <luchuangao@126.com>
Date:   Fri Oct 27 20:43:30 2017 +0800

    master1

commit 2c7ef374134f0158fab77137717d7ba1c1281a36
Author: luchuangao <luchuangao@126.com>
Date:   Fri Oct 27 20:42:26 2017 +0800

    test2

commit 4d182786a2199f10351dd69e819d6878513c8456
Author: luchuangao <luchuangao@126.com>
Date:   Fri Oct 27 20:42:17 2017 +0800

    test1

commit ddb20076027cca6b310a30468445ce82e96dcbcd
Author: luchuangao <luchuangao@126.com>
Date:   Fri Oct 27 20:41:13 2017 +0800

    c

commit 65c273b1512cbad0c4325d21f1cd6c421d14f2b3
Author: luchuangao <luchuangao@126.com>
Date:   Fri Oct 27 20:41:07 2017 +0800

    b

commit ac03ef772d4e01914d0917676d2f33eca8c0bb11
Author: luchuangao <luchuangao@126.com>
Date:   Fri Oct 27 20:40:57 2017 +0800

    a
[root@luchuangao oldboy]# 

rebase变基

# rebase操作

第一步:
# 先创建一个目录,在主分支提交3个txt文件
[root@luchuangao ~]# cd oldgirl/
[root@luchuangao oldgirl]# git init
初始化空的 Git 版本库于 /root/oldgirl/.git/
[root@luchuangao oldgirl]# echo "a" > a.txt
[root@luchuangao oldgirl]# echo "b" > b.txt 
[root@luchuangao oldgirl]# echo "c" > c.txt 
[root@luchuangao oldgirl]# git add a.txt
[root@luchuangao oldgirl]# git commit -m 'a'
[master(根提交) c54fa4b] a
 1 file changed, 1 insertion(+)
 create mode 100644 a.txt
[root@luchuangao oldgirl]# git add b.txt    
[root@luchuangao oldgirl]# git commit -m 'b'
[master c889368] b
 1 file changed, 1 insertion(+)
 create mode 100644 b.txt
[root@luchuangao oldgirl]# git add c.txt    
[root@luchuangao oldgirl]# git commit -m 'c'
[master 60fe420] c
 1 file changed, 1 insertion(+)
 create mode 100644 c.txt
 
第二步:
# 创建并切换到testing分支,并提交2个txt文件
 [root@luchuangao oldgirl]# git checkout -b testing
切换到一个新分支 'testing'
[root@luchuangao oldgirl]# echo "test1" > test1.txt
[root@luchuangao oldgirl]# echo "test2" > test2.txt 
[root@luchuangao oldgirl]# git add test1.txt
[root@luchuangao oldgirl]# git commit -m 'test1'
[testing 65f10cf] test1
 1 file changed, 1 insertion(+)
 create mode 100644 test1.txt
 [root@luchuangao oldgirl]# git add test2.txt
[root@luchuangao oldgirl]# git commit -m 'test2'   
[testing 747599f] test2
 1 file changed, 1 insertion(+)
 create mode 100644 test2.txt
[root@luchuangao oldgirl]# git log
commit 747599fba4c46673f8aae88828d2ac884e3cb2a4
Author: luchuangao <luchuangao@126.com>
Date:   Fri Oct 27 20:56:21 2017 +0800

    test2

commit 65f10cfc43af5d364a24907564e0eb0898edf2e1
Author: luchuangao <luchuangao@126.com>
Date:   Fri Oct 27 20:56:00 2017 +0800

    test1

commit 60fe4206ed68a3c7a0a2473310f9430e233ee1e9
Author: luchuangao <luchuangao@126.com>
Date:   Fri Oct 27 20:54:21 2017 +0800

    c

commit c889368e57e171ca58dcfc98fa7d742fe22fa362
Author: luchuangao <luchuangao@126.com>
Date:   Fri Oct 27 20:54:16 2017 +0800

    b

commit c54fa4bff7733175ef67b7db6b222464039aafb4
Author: luchuangao <luchuangao@126.com>
Date:   Fri Oct 27 20:54:10 2017 +0800

    a
	
	
第三步:
# 切换至主分支,再在主分支提交2个txt文件	
[root@luchuangao oldgirl]# git checkout master
切换到分支 'master'
[root@luchuangao oldgirl]# echo "master1" > master1.txt
[root@luchuangao oldgirl]# echo "master2" > master2.txt 
[root@luchuangao oldgirl]# git add master1.txt
[root@luchuangao oldgirl]# git commit -m 'master1'
[master ca28eb9] master1
 1 file changed, 1 insertion(+)
 create mode 100644 master1.txt
[root@luchuangao oldgirl]# git add master2.txt    
[root@luchuangao oldgirl]# git commit -m 'master2'
[master a48b977] master2
 1 file changed, 1 insertion(+)
 create mode 100644 master2.txt
[root@luchuangao oldgirl]# git log
commit a48b977c1c0459066cf20bbd28d77021e85ab15c
Author: luchuangao <luchuangao@126.com>
Date:   Fri Oct 27 20:57:25 2017 +0800

    master2

commit ca28eb9251012b0501a2678e42e765dc5fb9b81c
Author: luchuangao <luchuangao@126.com>
Date:   Fri Oct 27 20:57:20 2017 +0800

    master1

commit 60fe4206ed68a3c7a0a2473310f9430e233ee1e9
Author: luchuangao <luchuangao@126.com>
Date:   Fri Oct 27 20:54:21 2017 +0800

    c

commit c889368e57e171ca58dcfc98fa7d742fe22fa362
Author: luchuangao <luchuangao@126.com>
Date:   Fri Oct 27 20:54:16 2017 +0800

    b

commit c54fa4bff7733175ef67b7db6b222464039aafb4
Author: luchuangao <luchuangao@126.com>
Date:   Fri Oct 27 20:54:10 2017 +0800

    a

第四步:
# 合并testing
[root@luchuangao oldgirl]# git rebase testing
首先,重置头指针以便在上面重放您的工作...
正应用:master1
正应用:master2

# 查看master分支日志,发现根本没有合并得日志记录
[root@luchuangao oldgirl]# git log
commit e28b92283b6dff9af42446183e121cc379f79e7b
Author: luchuangao <luchuangao@126.com>
Date:   Fri Oct 27 20:57:25 2017 +0800

    master2

commit 6e49a215c918e3e3ce229bf4aff1bb8608f03d4e
Author: luchuangao <luchuangao@126.com>
Date:   Fri Oct 27 20:57:20 2017 +0800

    master1

commit 747599fba4c46673f8aae88828d2ac884e3cb2a4
Author: luchuangao <luchuangao@126.com>
Date:   Fri Oct 27 20:56:21 2017 +0800

    test2

commit 65f10cfc43af5d364a24907564e0eb0898edf2e1
Author: luchuangao <luchuangao@126.com>
Date:   Fri Oct 27 20:56:00 2017 +0800

    test1

commit 60fe4206ed68a3c7a0a2473310f9430e233ee1e9
Author: luchuangao <luchuangao@126.com>
Date:   Fri Oct 27 20:54:21 2017 +0800

    c

commit c889368e57e171ca58dcfc98fa7d742fe22fa362
Author: luchuangao <luchuangao@126.com>
Date:   Fri Oct 27 20:54:16 2017 +0800

    b

commit c54fa4bff7733175ef67b7db6b222464039aafb4
Author: luchuangao <luchuangao@126.com>
Date:   Fri Oct 27 20:54:10 2017 +0800

    a

merge与rebase的区别

由以上操作第四步,可以很明显看出,merge和rebase得区别。

merge有“Merge branch 'testing'”日志记录,而rebase木有。

merge 合并两个或更多开发历史。

rebase 本地提交转移至更新后的上游分支中。

 

使用场景:

merge可以保存历史记录,记录什么时间合并的,从哪个分支合并过来得,数据不会变动,即使删除testing分支,数据还是存在得。

rebase隐藏了提交细节,好像没开过分支一样。