如何将含已有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
serverandclient). Initialize a new Git repo here:git init - Next, head into the
clientfolder and delete its existing local Git repo (warning: make sure any uncommitted changes inclientare 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
serverandclient: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
clientfolder's code to its own GitHub repo (create a new repo on GitHub, then rungit remote add originandgit pushfrom inside theclientfolder first). - Navigate to your root project folder, initialize a new Git repo:
git init - Add the
clientrepo 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
serverfolder 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-submodulesto pull both the main code and the client submodule. If you already cloned without that, rungit submodule update --initafterward.
内容的提问来源于stack exchange,提问作者user2371290
相关产品推荐
相关产品推荐

