如何使用git submodule拆分大的git项目

背景

我开发一个全栈项目,起初为了让AI统一同时开发前端,后端,客户端。将项目放在了同一个目录下用git统一管理。

例如:

myProjects:

--FrontEnd,

--BackEnd,

--Client

后来基础架构搭建完成,我需要将三个项目目录分别模块化。

如何处理,每种方案进行前务必先将项目copy一份哦

方案1、Git Submodule(推荐)

原来的git管理的项目使用一个总库,将FrontEnd单独做一个submodule。

 方案1:Git Submodule(推荐)                                                                                                                             
  # 1. 创建独立的 frontend 仓库                                                                                                                            
  cd /path/to/new/location                                                                                                                                 
  git init frontend-repo                                                                                                                                   
  cp -r /path/to/myProjects/frontend/* .                                                                                 
  git add .                                                                                                                                                
  git commit -m "Initial frontend commit"                                                                                                                  
  git remote add origin <frontend-repo-url>                                                                                                                
  git push -u origin main       
 # 2. 在主项目中移除 frontend 并添加为 submodule                                                                                                          
  cd /path/to/myProjects/                                                                           
  git rm -r frontend                                                                                                                                       
  git commit -m "Remove frontend for submodule conversion"                                                                                                 
  git submodule add <frontend-repo-url> frontend                                                                                                           
  git commit -m "Add frontend as submodule"     

方案2 Git Subtree(更简单)

# 1. 先提取 frontend 历史到独立分支                                                                                                                      
  git subtree split --prefix=frontend -b frontend-only 
 # 2. 创建新仓库并推送                                                                                                                                    
  mkdir ../frontend-repo && cd ../frontend-repo                                                                                                            
  git init                                                                                                                                                 
  git pull ../myProjects frontend-only                                                                                                                     
  git remote add origin <frontend-repo-url>                                                                                                                
  git push -u origin main   
  # 3. 回到主项目,移除并添加为 subtree                                                                                                                    
  cd ../myProjects                                                                                                                                         
  git rm -r frontend                                                                                                                                       
  git commit -m "Remove frontend for subtree"                                                                                                              
  git subtree add --prefix=frontend <frontend-repo-url> main                                                                                               
       

配置不同 Git 账户:

# 在 frontend 子项目中                                                                                                                                   
  cd frontend                                                                                                                                              
  git config user.name "Frontend Developer"                                                                                                                
  git config user.email "frontend@example.com"                                                                                                             

推荐 Submodule,因为它保持独立仓库,更适合不同账户管理。

posted @ 2026-04-29 00:25  wjwdive  阅读(6)  评论(0)    收藏  举报