`

git merge 和 git rebase 小结

    博客分类:
  • git
 
阅读更多

git merge是用来合并两个分支的。


git merge b

      # 将b分支合并到当前分支

同样 git rebase b,也是把 b分支合并到当前分支

-----------------------------------

他们的 原理 如下

 

 

假设你现在基于远程分支"origin",创建一个叫"mywork"的分支。
$ git checkout -b mywork origin
假设远程分支"origin"已经有了2个提交,如图
 
现在我们在这个分支做一些修改,然后生成两个提交(commit).
$ vi file.txt
$ git commit
$ vi otherfile.txt
$ git commit
...
但是与此同时,有些人也在"origin"分支上做了一些修改并且做了提交了. 这就意味着"origin"和"mywork"这两个分支各自"前进"了,它们之间"分叉"了。
 
 
在这里,你可以用"pull"命令把"origin"分支上的修改拉下来并且和你的修改合并; 结果看起来就像一个新的"合并的提交"(merge commit):
 
但是,如果你想让"mywork"分支历史看起来像没有经过任何合并一样,你也许可以用 git rebase:
$ git checkout mywork
$ git rebase origin
这些命令会把你的"mywork"分支里的每个提交(commit)取消掉,并且把它们临时 保存为补丁(patch)(这些补丁放到".git/rebase"目录中),然后把"mywork"分支更新 为最新的"origin"分支,最后把保存的这些补丁应用到"mywork"分支上。
 
当'mywork'分支更新之后,它会指向这些新创建的提交(commit),而那些老的提交会被丢弃。 如果运行垃圾收集命令(pruning garbage collection), 这些被丢弃的提交就会删除. (请查看 git gc)
 
二、解决冲突
rebase的过程中,也许会出现冲突(conflict). 在这种情况,Git会停止rebase并会让你去解决 冲突;在解决完冲突后,用"git-add"命令去更新这些内容的索引(index), 然后,你无需执行 git-commit,只要执行:
git rebase --continue
这样git会继续应用(apply)余下的补丁。
在任何时候,你可以用--abort参数来终止rebase的行动,并且"mywork" 分支会回到rebase开始前的状态。
git rebase --abort
三、git rebasegit merge的区别
现在我们可以看一下用合并(merge)和用rebase所产生的历史的区别:
当我们使用Git log来参看commit时,其commit的顺序也有所不同。
假设C3提交于9:00AM,C5提交于10:00AM,C4提交于11:00AM,C6提交于12:00AM,
对于使用git merge来合并所看到的commit的顺序(从新到旧)是:C7 ,C6,C4,C5,C3,C2,C1
对于使用git rebase来合并所看到的commit的顺序(从新到旧)是:C7 ,C6‘,C5',C4,C3,C2,C1
 因为C6'提交只是C6提交的克隆,C5'提交只是C5提交的克隆,
从用户的角度看使用git rebase来合并后所看到的commit的顺序(从新到旧)是:C7 ,C6,C5,C4,C3,C2,C1
 
分享到:
评论

相关推荐

    git分支操作.txt

    gti详细的分支操作,在git中,可以使用git merge 和git rebase两个命令来进行分支的合并。 git merge 和git rebase在大体上都差不多,下文主要以git merge来例来讲解分支的合并流程。 如果你想了解分支合并的更多...

    详解git merge 与 git rebase的区别

    主要介绍了详解git merge 与 git rebase的区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

    ZhongJinHacker#notes#git_merge和git_rebase总结1

    文件可能会发生冲突,需要解决一下最后结果* 85ef130 (HEAD -> master) Merge branch 'dev'可以看出,merge 有保留d

    前端大厂最新面试题-git rebase_ git merge.docx

    前端大厂最新面试题-git rebase_ git merge.docx

    Git的merge和rebase你真的了解吗?

    Git具有以下主要功能和特点: 版本控制:Git最基本的功能是版本控制,可以记录每个文件的修改历史,包括修改内容、作者、时间等。通过Git,开发者可以轻松回溯到任何一个历史版本,查看修改细节、恢复代码等,有助...

    git 视频教程

    git视频教程.4.6.Git 命令 - git merge、git mergetool.mp4 git视频教程.4.7.Git 命令 - git log、git stash、git tag.mp4 git视频教程.5.1.Git 命令 - git fetch.mp4 git视频教程.5.2.Git 命令 - git pull.mp4 git...

    Sungq1990#blog#git rebase操作1

    merge主要发生在这样几个地方1 两个人同时开发一个分支,在拉取对方代码的时候2 要将代码合并到master的时候git pull origin master

    Git Version Control Cookbook 无水印pdf 0分

    Discover how you can force rebase on some branches and use regular Git merge on other branches Extract metadata from a Git repository Familiarize yourself with Git notes Discover how you can work ...

    Git.Version.Control.Cookbook.1782168451

    Discover how you can force rebase on some branches and use regular Git merge on other branches Extract metadata from a Git repository Familiarize yourself with Git notes Discover how you can work ...

    Git-2.21.0-64-bit.zip

    * A "merge -c" instruction during "git rebase --rebase-merges" should give the user a chance to edit the log message, even when there is otherwise no need to create a new merge and replace the ...

    git-2.22.0-2019-06-07更新.rar

    *更新“git difftool”和“git mergetool”以便组合 {diff,merge}。{tool,guitool}配置变量用作 以合理的顺序相互后备设置。 *“git difftool”的“--dir-diff”模式在“--no-index”中没用 模式; 它们现在...

    11. 高级 1: 不喜欢 merge 的分叉? 用 rebase 吧1

    11. 高级 1: 不喜欢 merge 的分叉? 用 rebase 吧1

    SourceTree:SourceTree 是 Windows 和Mac OS X 下免费的 Git 和 Hg 客户端管理工具

    SourceTree拥有一个精美简洁的界面,大大简化了开发者与代码库之间的Git操作方式,这对于那些不熟悉Git命令的开发者来说非常实用。 SourceTree拥有完整的Git功能: 通过一个简单的用户界面即可使用所有的Git命令 ...

    Git Version Control Cookbook 2nd Edition

    You’ll then move on to discovering the features that Git rebase has to offer and use regular Git merge on other branches. You’ll explore Git notes and learn how to utilize the update, list, and ...

    starter-web:入门网站回购

    入门Web回购 这是我们正在用作演示项目的git项目 ... #Git Rebase文件已针对Git rebase讲座进行了修改我们已经在mybranch中创建了Rebase Github 这个文件修改了ib github ## git stash文件已修改为git stash

    Git学习笔记.pdf

    Git中关于rebase, merge, cherry-pick, rm等的深入讲解

    10高级 1:不喜欢 merge 的分叉?用 rebase 吧(1).md

    上手 1:新公司用 Git 管理代码,怎么快速上手? 学习时长: 15分34秒 4 上手 2:团队工作的基本工作模型 学习时长: 9分32秒 5 进阶 1:HEAD、master 与 branch 学习时长: 15分41秒 6 进阶 2:push 的本质 学习时长: ...

    test-git-merge-cherry-squache

    test-git-merge-cherry-squache

    Git-2.23.0/win64/32/mac 多文件

    *更新“git rebase”的支持部分以删除应该的代码 不再使用。 *开发人员支持,以模拟测试中未满足的先决条件 确保测试时其余测试仍然成功 已跳过先决条件。 *“git update-server-info”学会了不用#重写文件...

Global site tag (gtag.js) - Google Analytics