Rebase
Git Rebase 详解
本文主要讲解下
Rebase 基础
相信对于
“git-rebase: Forward-port local commits to the updated upstream head”— git doc
翻译一下,就是讲你在某个分支上的所有提交记录移花接木到另一个分支上。这边需要强调一个概念:reapply,使用
-
在重放之前的提交的时候,
Git 会创建新的提交,也就是说即使你重放的提交与之前的一模一样Git 也会将之当做新的独立的提交进行处理。 -
Git rebase 并不会删除老的提交,也就是说你在对某个分支执行了rebase 操作之后,老的提交仍然会存放在.git 文件夹的objects 目录下。如果你对于Git 是如何存放你的提交不太了解的话可以参考这篇文章:Understanding git for real by exploring the .git directory
基于以上表述,我们可以得出以下相对更准确的
从上图可以看出,在对特征分支进行
使用Rebase 修改本地历史
顾名思义,
$ git log --pretty=oneline
a931ac7c808e2471b22b5bd20f0cad046b1c5d0d c
b76d157d507e819d7511132bdb5a80dd421d854f b
df239176e1a2ffac927d8b496ea00d5488481db5 a
运行
# 使用 Git Rebase,对最后两个提交进行操作
git rebase --interactive HEAD~2
pick b76d157 b
squash a931ac7 c //change pick to squash
# Rebase df23917..a931ac7 onto df23917
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#
然后保存并且关闭编辑器:
# This is a combination of 2 commits.
# The first commit's message is:
b
# This is the 2nd commit message:
c
这样的话就已经完成了合并
$ git log --pretty=oneline
18fd73d3ce748f2a58d1b566c03dd9dafe0b6b4f b and c
df239176e1a2ffac927d8b496ea00d5488481db5 a
Golden Rule of Rebase
“No one shall rebase a shared branch” — Everyone about rebase
估计你也肯定看过这个原则,不过可能表述不一样罢了。本章节就是用实例的角度来探讨下,为啥不能再一个共享的分支上进行
现在
当
然后呢,当
这个消息很正常,没啥特殊的,只是
A--B--C--D' origin/feature // GitHub
A--B--D--E feature // Anna
在进行
这个第
到这里,看到上面这个混乱的流线图,相信你对于