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

如何将Repo1的branch1内容迁移至Repo2的branch2?

迁移Repo1->branch1到Repo2->branch2的可行方案

方案一:直接推送远程分支(最常用)

适合大多数场景,操作简单,完整保留提交历史:

  • 克隆Repo1到本地并切换到branch1,拉取最新代码:
    git clone <Repo1的仓库URL> && cd Repo1
    git checkout branch1 && git pull origin branch1
    
  • 添加Repo2作为本地仓库的远程源(命名为repo2):
    git remote add repo2 <Repo2的仓库URL>
    
  • 将branch1的内容推送到Repo2的branch2:
    git push repo2 branch1:branch2
    

方案二:镜像仓库推送(适合需保留全量历史/标签的场景)

如果需要迁移分支的完整提交历史、标签等元数据,可使用镜像克隆:

  • 克隆Repo1的镜像仓库:
    git clone --mirror <Repo1的仓库URL>
    cd Repo1.git
    
  • 修改镜像仓库的推送地址为Repo2:
    git remote set-url --push origin <Repo2的仓库URL>
    
  • 推送指定分支到Repo2的branch2:
    git push origin branch1:branch2
    

方案三:补丁文件迁移(适合网络受限场景)

当无法直接连接两个远程仓库时,可通过补丁文件迁移内容:

  • 在Repo1的branch1下生成包含所有提交的补丁文件:
    git checkout branch1
    git format-patch --root --stdout > branch1_full.patch
    
  • 克隆Repo2到本地并切换到空分支branch2:
    git clone <Repo2的仓库URL> && cd Repo2
    git checkout branch2
    
  • 应用补丁文件并推送:
    git am < /path/to/branch1_full.patch
    git push origin branch2
    

方案四:直接复制文件(仅需当前文件状态,无需历史)

如果不需要保留提交历史,只需要最新文件内容:

  • 在Repo1的branch1目录下,复制所有文件(排除.git文件夹)到Repo2的branch2本地目录
  • 在Repo2的branch2下执行提交并推送:
    git add .
    git commit -m "迁移Repo1 branch1的最新文件内容"
    git push origin branch2
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 12:25:33