3

git rebase 使用方法

 2 years ago
source link: https://caorong.github.io/2019/07/06/git-rebase/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

最近,自己的代码仓库不再是一个人维护了,所以开始要制定一些规范。

这篇主要记录一下 git rebase 的2种常用使用场景

合并多次提交记录

一般使用场景

在自己的 feature 分支上创建了多次commit,想要合并为一个commit

e.g 合并最近的3次提交

git rebase -i HEAD~4

然后会进入 vi 模式

p 80d6a0d commit 1
p 646c56c commit 2
p 0108961 commit 3
p ad42acc commit 4

# Rebase b1aa3fb..0108961 onto b1aa3fb (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
# 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.

有几个命令需要注意一下:

  • 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
  • x, exec = run command (the rest of the line) using shell
  • d, drop = remove commit

我们一般使用 sf

按照如上命令来修改你的提交纪录:

使用 s 的话:

p 80d6a0d commit 1
s 646c56c commit 2
s 0108961 commit 3
s 0108961 commit 4

合并后的结果如下,会讲多次commit 记录保留,但放到最初的commit记录当中去

commit 672a5da92af62b5c2d941f0161c987ff9777762c (HEAD -> master)
Author: caorong <[email protected]>
Date: Sat Jul 6 21:21:36 2019 +0800

commit 1

commit 2

commit 3

commit 4

使用 f 的话

p 80d6a0d commit 1
f 646c56c commit 2
f 0108961 commit 3
f 0108961 commit 4

合并的结果如下,commit 2,3,4 将被丢弃

commit 672a5da92af62b5c2d941f0161c987ff9777762c (HEAD -> master)
Author: caorong <[email protected]>
Date: Sat Jul 6 21:21:36 2019 +0800

commit 1

当你在 feature 分支,不断开发时,别的同学已经将 他自己的 feature 分支合并到 develop 了

这时,develop 分支已经领先你 feature 分支了。

然后,你的 feature 分支将无法直接合并到 develop。

以前的解决方法是,先吧 develop 分支 merge 到自己的 feature 分支,修复冲突后,再将 feature 分支merge 到 develop 分支

这样可以解决,但是你的 提交记录上会多几条 Merge 记录

其实, 这种情况, 可以用 rebase 解决

当自己的 feature 分支开发完毕,准备合并之前

git rebase develop

他会执行 merge 操作,但不会出现 一次 Merged commit

他的原理?

首先,git 会把 feature 分支里面的每个 commit 取消掉;
其次,把上面的操作临时保存成 patch 文件,存在 .git/rebase 目录下;
然后,把 feature 分支更新到最新的 develop 分支;
最后,把上面保存的 patch 文件应用到 feature 分支上;

rebase 的隐患

rebase 毕竟是一个危险的操作,建议保证仅再自己的分支操作(比如上面的一)。避免造成非常多的冲突, 或者严重情况下,造成丢失历史纪录


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK