You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何按文件维度将Git分支拆分为两个独立分支

按文件拆分Git混合功能分支操作指南

假设你当前所在的混合功能分支名为feature-combined,拆分的基准分支为main,要拆分出两个独立功能分支feature-1、feature-2,操作步骤如下:

前置准备

先给原混合分支打临时备份标签,避免误操作丢失改动:

git tag backup-before-split feature-combined

同步基准分支的最新代码,减少后续合并冲突:

git checkout main
git pull origin main

拆分第一个功能分支

  • 从最新基准分支新建独立功能分支:
git checkout -b feature-1
  • 从原混合分支迁移指定文件/文件夹的改动:
git checkout feature-combined -- path/to/feature1-file1 path/to/feature1-dir/ path/to/feature1-file2

--后可填写任意数量的文件、文件夹路径,所有指定路径的改动会直接同步到当前分支。
如果需要保留对应改动的提交历史,可以用带路径过滤的cherry-pick操作:

# 对每一个涉及feature1改动的提交执行,-n参数表示仅应用改动不自动提交
git cherry-pick -n <commit-id> -- path/to/feature1-file1 path/to/feature1-dir/
  • 验证改动无误后提交、推送:
git add .
git commit -m "feat: 完成feature1全量逻辑开发"
git push origin feature-1

拆分第二个功能分支

重复上述流程即可:

git checkout main
git checkout -b feature-2
git checkout feature-combined -- path/to/feature2-file1 path/to/feature2-dir/
git add .
git commit -m "feat: 完成feature2全量逻辑开发"
git push origin feature-2

校验

分别进入两个功能分支,执行git diff main,确认仅包含对应功能的改动即可。确认拆分无误后可以删除之前打的临时备份标签:

git tag -d backup-before-split

内容的提问来源于stack exchange,提问作者guettli

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.23 21:45:03