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

如何将两个独立本地Git仓库合并推送到同一个远程仓库

两个独立本地Git仓库合并推送到同一远程仓库操作方案

方案1:保留两个仓库的完整提交历史(推荐)

操作步骤:

  • 新建空的本地主项目文件夹,初始化Git仓库
# 替换为你想要的项目名
mkdir my-main-project
cd my-main-project
git init
  • 拉取API本地仓库的完整内容和提交历史
# 替换路径为你本地API仓库的实际路径
git remote add -f api-repo /your/local/api/repo/path
# 允许合并无关联历史的仓库,如果你原API仓库的主分支不是main,替换为master或对应分支名
git merge api-repo/main --allow-unrelated-histories
# 创建API子目录,将所有API仓库文件移动到该目录下
mkdir API
# Linux/macOS环境执行,Windows环境手动移动文件,注意排除.git文件夹和刚创建的API文件夹
git mv !(API|.git) API/
git commit -m "导入API仓库完整历史"
  • 用同样的方法拉取UI本地仓库的内容
# 替换路径为你本地UI仓库的实际路径
git remote add -f ui-repo /your/local/ui/repo/path
git merge ui-repo/main --allow-unrelated-histories
mkdir UI
# 移动文件时注意排除.git、API、UI三个目录
git mv !(API|UI|.git) UI/
git commit -m "导入UI仓库完整历史"
  • 绑定目标远程仓库,推送内容
# 替换为你的远程仓库地址
git remote add origin https://xxx/xxx/your-remote-repo.git
git push -u origin main

方案2:不需要保留原有提交历史,操作更简便

操作步骤:

  • 新建空的主项目文件夹,将API和UI两个文件夹整体复制到该文件夹下,删除两个文件夹内部的.git隐藏目录
  • 初始化主项目仓库,提交所有内容
cd my-main-project
git init
git add .
git commit -m "初始化项目,包含API、UI模块"
  • 绑定远程仓库推送
git remote add origin https://xxx/xxx/your-remote-repo.git
git push -u origin main

注意:如果后续仍需单独维护API、UI的独立本地仓库,可在主项目修改对应子目录内容后,手动同步到独立仓库;也可以在独立仓库修改后,重新执行方案1的合并步骤同步到主项目再推送。

内容的提问来源于stack exchange,提问作者Code With Banchi

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.29 18:48:02