merge and rebase


Git Merge 和 Git Rebase 都是用于集成来自不同分支的修改的 Git 命令。

什么是merge

Git Merge 是将一个分支的改动合并到另一个分支的方式。当你执行一个 merge 操作时,如果没有冲突,Git 会自动创建一个新的提交,这个提交有两个父提交,一个是你正在工作的分支的最后一个提交,一个是你要合并进来的分支的最后一个提交。
git merge

什么是rebase

同样可以用来集成不同的分支,但它的工作原理与 merge 不同。Rebase 实际上是取出一系列的提交,然后在另一个分支上"重新执行",在你执行 rebase 的过程中,如果运行到有冲突的提交,Git 会暂停 rebase,并允许你解决冲突,你修复冲突后再提交,然后使用 git rebase --continue 命令来继续执行剩下的部分。这将创建一个线性的提交历史。
git rebase

merge和rebase的区别

Git Merge 保持了历史的所有信息和分支的上下文关系,但这会让提交历史复杂且难以阅读。
Git Rebase 则可以得到一份“干净”的提交历史,所有的更改都像一条线一样递进,但这会改变历史,且一旦你推送你 rebase 过的代码到远程仓库,会给协作者带来麻烦。
merge and rebase
对于团队项目,建议在本地使用 Git Rebase 保持简洁的线性历史记录,等到需要推送到远端仓库,或者需要和别人共享的时候再执行一个 Git Merge。

记住一条原则:已经在公有分支的提交,避免对其进行 Rebase 操作。因为 Rebase 会重写提交历史,这对于已经被其他人拉取使用的分支会产生问题。

操作执行

git merge操作

例如,假设我们想要将 feature 分支的更改合并到 master 分支,首先切换到 master 分支:

git checkout master

运行 git merge 命令,后面跟上你想要合并 的 分支的名称:

git merge feature

git rebase操作

切换到我们想要重新基准的分支,也就是说我们想要移动 feature 分支到 master 分支的末尾

git checkout feature

运行 git rebase 命令,后面跟上你想要基于 的 分支的名称:

git rebase master

冲突解决

遇到冲突时,Git会暂停合并或者rebase操作,并且会在冲突的文件中插入标准冲突指示符,你可以直接编辑这些文件来解决冲突。
冲突部分会显示如下格式:

<<<<<<< HEAD
code on the current branch
=======
code from the other branch (e.g., the branch being merged or rebased)
>>>>>>> branch-name

其中,<<<<<<< HEAD 与 ======= 之间的部分是在当前分支的代码,而 ======= 和 >>>>>>> branch-name 之间的部分是在其他分支上的代码。

解决冲突的步骤
  • 打开冲突文件,找到冲突的地方。
  • 决定保留哪个版本的代码,或者可能需要手动混合两个版本以解决冲突。
  • 删除所有的标记,包括 <<<<<<< , ======= ,和 >>>>>>> 。
  • 保存文件。
  • 使用 git add 命令把已经解决冲突的文件标记为已解决状态:
    git add filename
    
  • 如果你正在进行合并(merge)操作,你可以接着提交已经解决冲突的合并:
    git commit 
    
  • 如果你正在进行rebase操作,应该继续进行剩下的rebase流程:
    git rebase --continue
    #  取消rebase
    git rebase --abort
    
  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Git merge and rebase are both ways to integrate changes from one branch into another branch in Git version control system. Git merge is a process where Git combines the changes of two or more branches into a new commit on the current branch. This creates a merge commit that has two or more parent commits, representing the history of the merged branches. Merge is a simple and straightforward operation that preserves the entire history of both branches. Git rebase, on the other hand, is a process where Git moves the changes of one branch to the tip of another branch, effectively re-writing the history of the branch being rebased. This creates a linear history, as if the changes from the rebased branch were always part of the receiving branch. Rebase is a more advanced operation that requires more care and attention, as it can rewrite history and potentially cause conflicts. The decision to use merge or rebase depends on the specific situation and the desired outcome. Merge is typically used for combining long-lived feature branches or merging changes from upstream repositories. Rebase is typically used for keeping a clean and linear history, or when working on short-lived branches that are not shared with others. In summary, Git merge and rebase are two ways to integrate changes between branches in Git. Merge creates a merge commit that preserves the entire history of both branches, while rebase rewrites the history of the rebased branch to be part of the receiving branch. The choice between merge and rebase depends on the specific situation and desired outcome.

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

safina ~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值