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

如何将含已有Git仓库的MERN应用client与server推送到GitHub?

Got it, let's fix this step by step. You've got a MERN app with separate server and client folders, and client already has its own .git folder from running create-react-app. Here are two reliable approaches to get both folders pushed to a single GitHub repository:

方法1:将Client作为主仓库的子目录(最常用、简单)

This is the go-to solution if you don't need to keep the client folder as a separate standalone repo.

  • First, navigate to your root project folder (the one that contains both server and client). Initialize a new Git repo here:
    git init
    
  • Next, head into the client folder and delete its existing local Git repo (warning: make sure any uncommitted changes in client are either committed locally first or backed up—this will erase its Git history):
    cd client
    # For macOS/Linux
    rm -rf .git
    # For Windows Command Prompt
    rmdir /s .git
    # For Windows PowerShell
    Remove-Item -Recurse -Force .git
    
  • Return to the root folder, stage all files from both server and client:
    cd ..
    git add .
    
  • Commit the initial changes:
    git commit -m "Initial commit: Add full MERN app (server + client)"
    
  • Link your local repo to your GitHub remote repo (replace the URL with your actual GitHub repo address):
    git remote add origin https://github.com/your-username/your-mern-repo.git
    
  • Push everything to GitHub:
    git push -u origin main
    
方法2:使用Git Submodules(保留Client独立仓库)

Use this if you want to maintain the client as a separate standalone repo while still including it in your main MERN repo.

  • First, make sure you've already pushed the client folder's code to its own GitHub repo (create a new repo on GitHub, then run git remote add origin and git push from inside the client folder first).
  • Navigate to your root project folder, initialize a new Git repo:
    git init
    
  • Add the client repo as a submodule (replace the URL with your client repo's GitHub address):
    git submodule add https://github.com/your-username/your-client-repo.git client
    
  • Stage the server folder files:
    git add server/
    
  • Commit the changes to your main repo:
    git commit -m "Add server code and link client as submodule"
    
  • Link your main repo to its GitHub remote:
    git remote add origin https://github.com/your-username/your-main-mern-repo.git
    
  • Push the main repo to GitHub:
    git push -u origin main
    
  • Note: When cloning this main repo later, use git clone --recurse-submodules to pull both the main code and the client submodule. If you already cloned without that, run git submodule update --init afterward.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.07 19:03:09