未提交feature-23分支变更即切换至feature-24,如何拆分分支变更?
拆分跨分支未提交变更的解决方案
1. 暂存所有未提交修改
不管当前处于哪个分支,先把所有跨分支的未提交变更暂存,防止丢失:
git stash push -m "temp save all cross-branch changes"
2. 处理feature-23分支的专属修改
- 切换到feature-23分支:
git checkout feature-23 - 应用暂存的全部变更:
git stash apply - 通过
git status查看文件,只将属于feature-23的修改添加到暂存区:git add <feature-23专属文件路径> - 提交feature-23的修改:
git commit -m "提交feature-23分支的原始修改" - 将剩余的feature-24相关修改重新暂存:
git stash push -m "暂存feature-24分支的修改"
3. 处理feature-24分支的专属修改
- 切换到feature-24分支:
git checkout feature-24 - 应用暂存的feature-24专属修改:
git stash apply - 提交feature-24的修改:
git add .(或指定对应文件)git commit -m "提交feature-24分支的原始修改"
特殊情况:单个文件包含两个分支的混合修改
如果同一个文件里同时有两个分支的变更,需要逐块拆分:
- 切换到对应分支后,应用暂存的变更,执行:
git add --patch - 命令会逐块展示文件中的修改,输入
y保留当前块到当前分支,输入n跳过该块(留到另一个分支处理) - 提交选中的修改后,将剩余修改重新暂存,再切换到另一分支处理剩余部分
内容的提问来源于stack exchange,提问作者humzaqureshi
相关产品推荐
相关产品推荐

