美文网首页
Git~rebase

Git~rebase

作者: Tico_zl | 来源:发表于2019-12-04 15:54 被阅读0次

git rebase


rebase简介

rebase, 意思为变基,即改变分支的的根支。提到rebase就不得不说说merge,他们两个都可以完成相同的的工作(结果),将两个分支进行合并,但他们工作方式完全不同。

merge和rebase最大的不同,merge会保留所有的提交历史记录,而rebase会删除基变分支的历史记录,如下图所示。

merge工作方式图:

merge.png

rebase工作方式图:

rebase.png

rebase的基本操作

# 
#              C5---C6---C7 dev1
#             /
#       C1---C2---C3---C4 dev
git rebase dev
#                     A'--B'--C' dev1
#                    /
#       D---E---F---G dev

# 在 rebase 的过程中,也许会出现冲突 conflict 。在这种情况, git 会停止 rebase 并会让你去解决冲突。在解决完冲突后,用 git add 命令去更新这些内容。
# 注意,你无需执行 git-commit,只要执行 continue
git rebase --continue
# 这样 git 会继续应用余下的 patch 补丁文件。

# 在任何时候,我们都可以用 --abort 参数来终止 rebase 的行动,并且分支会回到 rebase 开始前的状态。
git rebase —abort

rebase的另一种操作:合并分支

命令:

git rebase -i HEAD~3

上面命令的意思是合并最近的 4 次提交纪录。

# 进入编辑模式
p 9abd07c add bbb
s 16981e9 add ccc
s e0f8d72 add d

# Rebase 7e215b2..e0f8d72 onto 7e215b2 (3 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# .       create a merge commit using the original merge commit's
# .       message (or the oneline, if no original merge commit was
# .       specified). Use -c <commit> to reword the commit message.
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#

pick:保留该commit(缩写:p)
reword:保留该commit,但我需要修改该commit的注释(缩写:r)
edit:保留该commit, 但我要停下来修改该提交(不仅仅修改注释)(缩写:e)
squash:将该commit和前一个commit合并(缩写:s)
fixup:将该commit和前一个commit合并,但我不要保留该提交的注释信息(缩写:f)
exec:执行shell命令(缩写:x)
drop:我要丢弃该commit(缩写:d)

如果你异常退出了 vim 窗口,可以执行

git rebase --edit-todo

这时候会一直处在这个编辑的模式里,我们可以回去继续编辑,修改完保存一下:

git rebase --continue

rebase最佳实践

  • 不要对master分支进行rebase
  • 一般来说,执行rebase的分支都是自己本地的分支,没有推送到远程版本库

相关文章

网友评论

      本文标题:Git~rebase

      本文链接:https://www.haomeiwen.com/subject/jhjlgctx.html