萌新请教 Git Rebase 问题

2020-08-05 15:37:55 +08:00
 353943780

最近项目新功能在 feature 分支开发中,突然要修复一些 bug,所以开了 fixbug 分支

请问修复之后的 commit 如何 rebase 到 feature 分支保持干净?

1 -> 2 -> 3 -> 6 -> 7 feature 分支
1 -> 2 -> 3 -> 4 -> 5 fixbug 分支

理想中 rebase 之后的 commit

1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7

4320 次点击
所在节点    git
35 条回复
dsdisenc
2020-08-05 15:40:09 +08:00
git checkout feature
git rebase fixbug
这样顺序就是 1234567 了
353943780
2020-08-05 15:41:54 +08:00
@dsdisenc 请问如果是

1 -> 2 -> 3 -> 4 -> 5 feature 分支
1 -> 2 -> 3 -> 6 -> 7 fixbug 分支

效果也是一样的么?
gesse
2020-08-05 15:50:19 +08:00
feature 上的 4-5 和 fixbug 上 6-7 并没有严格的先后顺序

不管是 rebase 还是 merge,其实都是“合并代码”的过程
rproud
2020-08-05 15:50:49 +08:00
来玩个游戏你就会了。https://learngitbranching.js.org
353943780
2020-08-05 16:05:46 +08:00
@rproud 学习了
gesse
2020-08-05 16:15:30 +08:00
rebase 怎么理解呢?

比如
分支 a 是 1-2-3
分支 b 是 1-2-4-5

在分支 b 上 rebase a 分支的意思是:
以 a 分支为"base",然后把“相对于 a 分支不同的 commit (也就是 4-5 )”加到 a 分支的后面,然后合并后( rebase 也要合并,因为在 a 分支上创建 b 分支后,a 分支也有修改的内容)的这个 branch 重新定义为 b,且 a 分支不变
353943780
2020-08-05 16:26:51 +08:00
@gesse

分支 a 是 1-2-3-4-6-8
分支 b 是 1-2-5-7

请问如果分支上 commit 时间并行这样的话,是不是完全合并为单条时间线呢?
hmxxmh
2020-08-05 16:44:44 +08:00
@rproud 这个太棒了
luxinfl
2020-08-05 17:13:11 +08:00
小小的吐个槽,feature 分支的数字应该比 fixbug 分支小才对。。
fan123199
2020-08-05 17:17:10 +08:00
如果 featuea 的 commoit 数已经特别多了,可以考虑用 cherry pick 。不然有冲突就难受了。
353943780
2020-08-05 17:32:05 +08:00
@luxinfl 数字代表时间先后顺序喔,可能是并行开发新功能跟修 bug
353943780
2020-08-05 17:34:28 +08:00
@fan123199 请问如果 feature commit 多,而且 commit 时间点与 fixbug 交错,例如

feature 1-2-3-4-6-8-9-10
fixbug 1-2-5-7

是不是无法 rebase 为单条时间线呢?
JJstyle
2020-08-05 17:48:05 +08:00
用 cherry-pick,简单易用:

```bash
git checkout -b feature_tmp 3

git cherry-pick 4^..5
git cherry-pick 6^..7

git branch -d feature
git branch -m feature
```

虽然操作步骤多一点,但是更易于常人理解!
GeruzoniAnsasu
2020-08-05 17:48:47 +08:00
跟时间没关系

checkout feature, rebase bug ==> 125734689
checkout fixbug, rebase feature ==> 12346891057
JJstyle
2020-08-05 17:54:39 +08:00
#13 cherry-pick 也能只 copy 一个 commit,上面的例子没显示出来,比如:

```bash
git cherry-pick 7
```

这样就把 commit 7 copy 到当前分支了,如果多个就是 a..b,前开后必区间,所以如果 copy a 到 b,且包含 a 和 b,那就用 a^..b
pegasusz
2020-08-05 17:54:51 +08:00
@rproud 这个也太 Q 了
353943780
2020-08-05 17:58:39 +08:00
@GeruzoniAnsasu 大佬,刚刚试了一下,rebase 之后确实跟时间没有关系 👍,但是发现 rebase 之后 push,有提示 pull,但是 pull 之后会自动产生一个 commit 是 merge fixbug 分支的,请问这样的情况怎么处理呢?
353943780
2020-08-05 18:01:26 +08:00
@JJstyle 好的大佬,我试试哈
luxinfl
2020-08-05 18:30:16 +08:00
@353943780 不是说再开发新 feature,然后突然发现有 bug 么。。。
ScepterZ
2020-08-05 18:56:16 +08:00
按理说你的 fixbug 应该要 merge 到 master 吧
mrege 之后
git checkout feature
git rebase master
然后变成
1 2 3 4 5 6new 7 new

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/695853

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX