本文主要是介绍git rebase 和 git merge 区别理解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
如果不亲自动手去做,经验永远是别人的,自己什么也没有。
参考链接:https://blog.csdn.net/nrsc272420199/article/details/85555911
为了突出重点,本文用户 A 和 B 的修改设置成是没有冲突的(也即 rebase 和 merge 刚好可以正常进行而无需解决冲突)。
git rebase
现在,用户 A,用户 B 和 远程仓库的代码版本都是最新且保持一致的。
用户 A 先在本地修改了文件 a.txt, commit 以后提交并推送到了远程仓库:
D:\MyProject\Python\GitTest>git add a.txt
D:\MyProject\Python\GitTest>git commit -m "add one line in a.txt by user A"
D:\MyProject\Python\GitTest>git push origin master
用户 B 在本地创建并进入了新的分支 dev,在分支 dev 里边分别对 b.txt 和 c.txt 添加了新内容:
[root@master GitTest]# git checkout -b dev
Switched to a new branch 'dev'
[root@master GitTest]# vi b.txt
[root@master GitTest]# git add b.txt
[root@master GitTest]# git commit -m "add one line in b.txt by user B"
[dev c17e325] add one line in b.txt by user B1 file changed, 1 insertion(+)
[root@master GitTest]# vi c.txt
[root@master GitTest]# git add c.txt
[root@master GitTest]# git commit -m "add one line in c.txt by user B"
[dev 5a31fb1] add one line in c.txt by user B1 file changed, 2 insertions(+), 1 deletion(-)
然后用户 B 切换回主分支,并 pull 拉取主分支最新状态:
[root@master GitTest]# git checkout master
Switched to branch 'master'
[root@master GitTest]# git pull origin master
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 1), reused 3 (delta 1), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:2392863668/GitTest* branch master -> FETCH_HEAD
Updating 26a6eb5..3ec68b5
Fast-forwarda.txt | 1 +1 file changed, 1 insertion(+)
然后用户 B 再次切换回 dev 分支,进行 rebase操作:
[root@master GitTest]# git checkout dev
Switched to branch 'dev'
# 因为用户 A 和 B 修改的不是同一个文件,所以下面 rebase 没有出现冲突
[root@master GitTest]# git rebase master
First, rewinding head to replay your work on top of it...
Applying: add one line in b.txt by user B
Applying: add one line in c.txt by user B
用户 B 再次切换回 master 主分支进行分支 merge,合并以后推送到远程仓库:
[root@master GitTest]# git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 1 commit.(use "git push" to publish your local commits)
[root@master GitTest]# git merge dev
Updating 3ec68b5..5afad54
Fast-forwardb.txt | 1 +c.txt | 3 ++-2 files changed, 3 insertions(+), 1 deletion(-)
[root@master GitTest]# git push origin master
Counting objects: 9, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (6/6), 640 bytes | 0 bytes/s, done.
Total 6 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 1 local object.
To git@github.com:2392863668/GitTest.git3ec68b5..5afad54 master -> master
用户 A 使用 pull 拉取最新状态并查看版本日志:
D:\MyProject\Python\GitTest>git pull origin master
remote: Enumerating objects: 9, done.
remote: Counting objects: 100% (9/9), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 6 (delta 2), reused 6 (delta 2), pack-reused 0
Unpacking objects: 100% (6/6), done.
From github.com:2392863668/GitTest* branch master -> FETCH_HEAD3ec68b5..5afad54 master -> origin/master
Updating 3ec68b5..5afad54
Fast-forwardb.txt | 1 +c.txt | 3 ++-2 files changed, 3 insertions(+), 1 deletion(-)
可以看到,采用 rebase 的方式进行分支合并,整个 master 分支并没有多出一个新的 commit。
git merge
现在,用户 A,用户 B 和 远程仓库的代码版本都是最新且保持一致的。
用户 A 先在本地修改了文件 a.txt, commit 以后提交并推送到了远程仓库:
D:\MyProject\Python\GitTest>git add a.txt
D:\MyProject\Python\GitTest>git commit -m "add one line in a.txt by user A again"
D:\MyProject\Python\GitTest>git push origin master
用户 B 在本地创建并进入了新的分支 dev,在分支 dev 里边分别对 b.txt 和 c.txt 添加了新内容:
[root@master GitTest]# git checkout -b dev
Switched to a new branch 'dev'
[root@master GitTest]# vi b.txt
[root@master GitTest]# git add b.txt
[root@master GitTest]# git commit -m "add one line in b.txt by user B again"
[dev c17e325] add one line in b.txt by user B1 file changed, 1 insertion(+)
[root@master GitTest]# vi c.txt
[root@master GitTest]# git add c.txt
[root@master GitTest]# git commit -m "add one line in c.txt by user B again"
[dev 5a31fb1] add one line in c.txt by user B1 file changed, 2 insertions(+), 1 deletion(-)
然后用户 B 切换回主分支,并 pull 拉取主分支最新状态:
[root@master GitTest]# git checkout master
Switched to branch 'master'
[root@master GitTest]# git pull origin master
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (1/1), done.
remote: Total 3 (delta 2), reused 3 (delta 2), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:2392863668/GitTest* branch master -> FETCH_HEAD
Updating 5afad54..cc12a89
Fast-forwarda.txt | 3 ++-1 file changed, 2 insertions(+), 1 deletion(-)
然后用户 B 直接进行合并操作并填写 merge commit:
[root@master GitTest]# git merge dev
Merge made by the 'recursive' strategy.b.txt | 1 +c.txt | 1 +2 files changed, 2 insertions(+)
[root@master GitTest]# git log
commit 648e3e12da7fc4f1a13d21e3f0c50c94d5950c25
Merge: cc12a89 d2009f7
Author: looking <looking@qq.com>
Date: Sun Jul 26 23:09:53 2020 +0800Merge branch 'dev' to 'master'commit d2009f78a7f83f8ae431caabac79d6a3978ada7a
Author: looking <looking@qq.com>
Date: Sun Jul 26 23:07:32 2020 +0800add one line in c.txt by user B againcommit 4505d98d63671b307aa5fcf093412d9be09012e5
Author: looking <looking@qq.com>
Date: Sun Jul 26 23:06:44 2020 +0800add one line in b.txt by user B again
用户 B 合并分支以后,将主分支推送到远程仓库:
[root@master GitTest]# git push origin master
Counting objects: 12, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (8/8), done.
Writing objects: 100% (8/8), 883 bytes | 0 bytes/s, done.
Total 8 (delta 3), reused 0 (delta 0)
remote: Resolving deltas: 100% (3/3), completed with 1 local object.
To git@github.com:2392863668/GitTest.gitcc12a89..648e3e1 master -> master
用户 A 使用 pull 拉取最新状态并查看版本日志:
D:\MyProject\Python\GitTest>git pull origin master
remote: Enumerating objects: 14, done.
remote: Counting objects: 100% (12/12), done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 8 (delta 3), reused 8 (delta 3), pack-reused 0
Unpacking objects: 100% (8/8), done.
From github.com:2392863668/GitTest* branch master -> FETCH_HEADcc12a89..648e3e1 master -> origin/master
Updating cc12a89..648e3e1
Fast-forwardb.txt | 1 +c.txt | 1 +2 files changed, 2 insertions(+)
可以看到,采用 merge 的方式进行分支合并,整个 master 分支会多出一个新的 commit 和明显的合并操作。
这篇关于git rebase 和 git merge 区别理解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!