English

git如何快速变更base branch

3/22/2024
30

场景展示

这两天工作上遇到了一个常见的git操作,其中有个知识点觉得挺有意义的,特此记录一下。

假设有两个分支:branchA、branchB,branchB需要以branchA为base,那我们如何能做到base分支branchA的每次更改在branchB上都能够快速rebase呢?

问题描述

可能方法有很多,但是今天我们这边就说一下我遇到的问题。

我是这么处理的,在branchB上:

Bash
git rebase origin/branchA

这么操作其实没啥问题,但是... ...但是当branchA上的提交有很多并且再次更新了提交之后,我们再次rebase的时候就得需要解决很多冲突,其实解决完冲突也没啥问题,(再次但是)但是解决完冲突之后发现之前branchB上的所有提交又得重新提交一边,天呐,我那提交的文件有好几十个再加上变更栏里面还有许多是不需要提交的,这样很容易就会出现漏提交或多提交的情况,所以最后直接放弃了这种操作。

(真是失算了,我当时记得如果branchA和branchB没有更改相同的文件的话直接rebase是不会有冲突的啊,同事告诉我的时候我还不信,甚至到解决冲突的时候我还以为我操作出问题了呢!直到最后凭着不信谣,不传谣的精神还是默默的打开git bash在测试项目上操作了一番发现再次rebase的时候即使两个branch没有操作相同的文件也会出现冲突,终于老实了... ...)

那我们该怎么避免这种明明没有更改相同文件却又让我解决冲突的问题呢?

解决问题

我觉得最简单的一种方法如下:

Bash
// 签出banchA
git checkout branchA
// 获取远端branchA的最新更新
git fetch origin branchA
// 最关键的部分来了:
git rebase -i origin/branchA branchB

// 执行完上述命令后进入编辑模式
------------------------------------
pick fb653a0 branchA的commit info
pick 7fcbe1b branchB的commit info

# Rebase ed21178..7fcbe1b onto ed21178 (2 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 [-C | -c] <commit> = like "squash" but keep only the previous
#                    commit's log message, unless -C is used, in which case
#                    keep only this commit's message; -c is same as -C but
#                    opens the editor
# 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
------------------------------------

// 我们drop掉之前rebase的branchA的提交变成下面这样:
------------------------------------
d fb653a0 branchA的commit info
pick 7fcbe1b branchB的commit info

# Rebase ed21178..7fcbe1b onto ed21178 (2 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 [-C | -c] <commit> = like "squash" but keep only the previous
#                    commit's log message, unless -C is used, in which case
#                    keep only this commit's message; -c is same as -C but
#                    opens the editor
# 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
------------------------------------

// 保存退出
Successfully rebased and updated refs/heads/branchB.

这种方法无需解决一些无效冲突就可以直接rebase到想要的branch上去

扩展方案

专家说还有好多种解决方案,其中在当时还学了两种,但是实践中只记住了一种使用cherry-pick命令,使用cherry pick的过程中,我发现对于新建的分支可以直接无伤变基,但是多次提交了之后还是需要解决冲突,也许是我的操作有问题,等有时间再研究研究。。。