How To Push MERN Stack Project on Github
Pushing your MERN Stack project (MongoDB, Express.js, React, and
Node.js) to GitHub is one of the most essential steps in modern web
development. It allows you to showcase your work, collaborate with other
developers, maintain version control, and even deploy your project on
hosting services like Vercel or Netlify. In this in-depth guide, we'll
walk you through how to push a MERN stack project to GitHub
step-by-step, ensuring everything from your backend to frontend is
properly configured.
π§ Understanding the MERN Stack Project Structure
Before you start pushing anything to GitHub, it's essential to
understand how a typical MERN stack project is structured. A common
folder setup looks like this:
my-mern-app/
β
βββ client/ # React frontend
β βββ public/
β βββ src/
β βββ package.json
β
βββ server/ # Express and Node.js backend
β βββ models/
β βββ routes/
β βββ controllers/
β βββ server.js or app.js
β βββ package.json
β
βββ .gitignore
βββ README.md
βββ package.json
In many MERN applications, the client directory is where the React
code resides, and the server or backend directory contains your
Node.js and Express logic. When pushing to GitHub, you'll want to ensure
both are included properly under a single repository unless they are
intended to be separate repos.
π§© Step 1: Create a GitHub Account and New Repository
If you don't already have one, start by creating a GitHub account.
Once you're logged in:
- Go to GitHub.com.
- Click the "+" icon in the top-right corner and select "New
repository." - Give your repository a meaningful name, like
mern-ecommerce-appormern-blog-app. - Choose between Public (visible to everyone) or Private
(restricted access). - Do not initialize with a README if you already have one in your
local project. - Click Create repository.
Now you'll have a new GitHub repository ready to receive your code.
βοΈ Step 2: Initialize Git in Your Local MERN Project
Open your terminal and navigate to your project folder using:
cd path/to/your/mern-project
Once inside your project root folder, initialize git:
git init
This command will create a hidden .git folder that Git uses to track
changes.
π Step 3: Create a .gitignore File
Before committing, you'll want to exclude files and folders that
shouldn't be uploaded to GitHub (like node_modules, .env, or build
files). Create a .gitignore file at the root of your project and add
the following lines:
# Node modules
node_modules/
# Environment variables
.env
# Build folders
client/build/
server/logs/
This step is crucial because pushing unnecessary or sensitive data (like
environment variables) can expose credentials and increase repo size.
π Step 4: Add Your Files and Commit Changes
Once .gitignore is set up, add your files to git and commit them:
git add .
git commit -m "Initial commit - MERN project setup"
This will stage all your files (except those ignored) and record your
first commit.
π Step 5: Connect Local Repository to GitHub
After your repository is created on GitHub, copy its remote URL from the
setup instructions page. It'll look like one of the following:
- HTTPS:
https://github.com/yourusername/mern-app.git - SSH:
git@github.com:yourusername/mern-app.git
Now connect your local repo to GitHub:
git remote add origin https://github.com/yourusername/mern-app.git
To verify the connection:
git remote -v
If you see your GitHub URL listed, you're ready to push.
π‘ Step 6: Push Your Project to GitHub
Finally, upload your local commits to GitHub with:
git push -u origin main
If your default branch is master, replace main with master:
git push -u origin master
After running this command, your entire MERN project will appear in your
GitHub repository. You can confirm by visiting your repo page in the
browser.
π§ Step 7: Pushing Updates (When You Make Changes)
Once your project is on GitHub, you'll frequently make updates. Each
time you modify your code, use the following commands:
git add .
git commit -m "Updated API routes" # Example message
git push
Git will upload only the modified files, keeping your repository
up-to-date.
π Step 8: Protecting Your Secrets (Environment Variables)
Never upload your .env file to GitHub --- it often contains database
URIs, API keys, and other sensitive data. Instead, add .env to.gitignore and use environment configuration systems when deploying
your app (like Vercel, Render, or Heroku).
If you accidentally pushed secrets, revoke and regenerate them
immediately and clean your Git history if needed.
π§° Step 9: Handling Client and Server in the Same Repository
Many developers keep both frontend (client) and backend (server)
inside one repo for simplicity. If that's your approach, ensure that
both are properly configured and can run independently.
Here's a sample structure for a combined repository:
mern-app/
βββ client/
βββ server/
βββ package.json
βββ README.md
Your main package.json can include scripts to run both parts easily:
"scripts": {
"start": "node server/server.js",
"client": "npm start --prefix client",
"dev": "concurrently "npm run server" "npm run client""
}
Make sure you install concurrently:
npm install concurrently
This will allow you to run both client and server simultaneously using
one command:
npm run dev
π§± Step 10: Using Branches for Team Collaboration
When working with multiple developers, using branches helps you
manage code efficiently:
git checkout -b feature/auth-system
After making changes:
git add .
git commit -m "Added authentication system"
git push origin feature/auth-system
This keeps the main branch stable while allowing new features to be
developed independently.
π§© Bonus: Connecting GitHub to Deployment Platforms
Once your project is on GitHub, you can easily deploy it using platforms
like:
- Vercel -- Ideal for React frontends and APIs.
- Render -- Great for hosting Node.js backends.
- Netlify -- Simplifies React app deployment.
- Railway or Heroku -- For full-stack MERN applications.
These platforms can directly connect to your GitHub repo, automatically
deploying your latest commits.
β‘ Troubleshooting Common Git Errors
Here are some common issues you might face when pushing to GitHub:
1. Authentication Failed
If you see:
fatal: Authentication failed
You might be using an expired token or old authentication method. Create
a Personal Access Token from your GitHub settings and use it instead
of your password.
2. Non-fast-forward Error
error: failed to push some refs
This happens when your local and remote branches are out of sync. Fix it
with:
git pull origin main --rebase
git push
3. Large File Error
GitHub limits file size (typically 100 MB). To fix this, remove large
files and add them to .gitignore.
π§ Why GitHub is Crucial for MERN Stack Developers
GitHub acts as your digital portfolio. Whether you're building
personal projects, freelancing, or applying for jobs, employers often
review your GitHub repositories to see your coding skills, commit
frequency, and documentation style.
It's not just a storage space --- it's proof of your experience.
πΌ Hire Professionals for MERN Stack Development
If you're planning to build a high-performance MERN stack application
but need professional help, consider working with
AAMAX. AAMAX is a full-service digital marketing
company that offers expert Web Development, Digital Marketing, and
SEO Services. Their experienced MERN stack developers can help you
create scalable, secure, and modern web applications tailored to your
business goals.
π Conclusion
Pushing a MERN stack project to GitHub is a must-have skill for every
developer. From initializing Git to committing, ignoring sensitive
files, managing branches, and syncing updates --- every step ensures
that your project remains organized, secure, and accessible.
Now that your MERN project is safely hosted on GitHub, you can
collaborate, showcase, and even deploy it to the world --- marking your
step into the professional development ecosystem.
Want to publish a guest post on aamax.co?
Place an order for a guest post or link insertion today.
Place an Order