Git - 你能用 rebase 做一些你不能用 merge 做的事情吗?
Git - Can you do something with rebase that you can't do with merge?
每次尝试rebase
,我都陷入了无法解决的冲突的无限循环中。在解决x冲突之前,出现了y冲突,然后又出现了x冲突,以此类推。
重点是(显然)merge
,甚至 cherry-pick
提供了与 rebase
相同的功能,不是吗?如果不是,rebase
有什么用?
我只在一种情况下使用rebase
。案例:
- 我有
master
个应用当前版本的分支。
- 我为新功能创建了
feature-branch
- 我在
feature-branch
中做了一些提交
- 我在master分支做了几个热提交
- 现在我想在
feature-branch
中提交,在这种情况下我会 rebase
。
我完成了 feature-branch
,我只是将它合并到 master 中。
git checkout master
git checkout feature-branch
git commit -am "feature commit #1"
git commit -am "feature commit #2"
git commit -am "feature commit #3"
git checkout master
git commit -am "hot fix"
git checkout feature-branch
git rebase master
git commit -am "i'm done"
git checkout master
git merge feature-branch
这让我可以站在 master
分支的顶端,并拥有干净的 git 历史记录。
rebase
改写了历史。它 way 比 merge
或 cherry-pick
多——一个非常先进的工具,需要练习(并且可能很危险,因为它重写了历史……小心git push -f
)。当 rebase
的用法与 merge
类似时,过程就完全不同了。
git fetch origin && git merge origin/master
:
- 从源下载更改
- 在本地分支上合并更改
- 解决冲突并提交
git fetch origin && git rebase origin/master
:
- 从源下载更改
- 将本地分支重置为历史上的共同点(local/remote 存储库共享的提交)
- 快进到原点
- 在新提交之上逐一应用您的提交
- 解决冲突并提交每个本地更改
在这种情况下 rebase
的最大优势是您只需更新本地存储库并在顶部应用您的提交,并且不会有任何 "gross" 合并提交说 "hey, I just merged this stuff in." 缺点是,如果你有大量修改相同文件的提交,你将不得不一遍又一遍地解决冲突,因为没有 "gross" 合并提交。
但是,rebase
是一个非常先进的工具,用于重写历史。您可以修改以前的提交标题、删除过去的提交、将许多提交压缩在一起等。Read up on it!
每次尝试rebase
,我都陷入了无法解决的冲突的无限循环中。在解决x冲突之前,出现了y冲突,然后又出现了x冲突,以此类推。
重点是(显然)merge
,甚至 cherry-pick
提供了与 rebase
相同的功能,不是吗?如果不是,rebase
有什么用?
我只在一种情况下使用rebase
。案例:
- 我有
master
个应用当前版本的分支。 - 我为新功能创建了
feature-branch
- 我在
feature-branch
中做了一些提交
- 我在master分支做了几个热提交
- 现在我想在
feature-branch
中提交,在这种情况下我会rebase
。 我完成了
feature-branch
,我只是将它合并到 master 中。git checkout master git checkout feature-branch git commit -am "feature commit #1" git commit -am "feature commit #2" git commit -am "feature commit #3" git checkout master git commit -am "hot fix" git checkout feature-branch git rebase master git commit -am "i'm done" git checkout master git merge feature-branch
这让我可以站在 master
分支的顶端,并拥有干净的 git 历史记录。
rebase
改写了历史。它 way 比 merge
或 cherry-pick
多——一个非常先进的工具,需要练习(并且可能很危险,因为它重写了历史……小心git push -f
)。当 rebase
的用法与 merge
类似时,过程就完全不同了。
git fetch origin && git merge origin/master
:
- 从源下载更改
- 在本地分支上合并更改
- 解决冲突并提交
git fetch origin && git rebase origin/master
:
- 从源下载更改
- 将本地分支重置为历史上的共同点(local/remote 存储库共享的提交)
- 快进到原点
- 在新提交之上逐一应用您的提交
- 解决冲突并提交每个本地更改
在这种情况下 rebase
的最大优势是您只需更新本地存储库并在顶部应用您的提交,并且不会有任何 "gross" 合并提交说 "hey, I just merged this stuff in." 缺点是,如果你有大量修改相同文件的提交,你将不得不一遍又一遍地解决冲突,因为没有 "gross" 合并提交。
但是,rebase
是一个非常先进的工具,用于重写历史。您可以修改以前的提交标题、删除过去的提交、将许多提交压缩在一起等。Read up on it!