Using Git in Node.js Projects

When you're working on Node.js apps, version control becomes even more important. These projects often have many files, dependencies, and environment variables. Git helps you keep everything organized and safe as you build. In this lesson, you'll learn how to set up Git in your Node project, ignore sensitive or bulky files, and share your code and documentation on GitHub.

Git Setup for Node Projects

To start using Git in a Node.js project, create a folder and initialize a new Node app:

bash
1
2
3
          mkdir my-node-app
cd my-node-app
npm init -y
        

Next, set up Git:

bash
1
          git init
        

This starts tracking changes in your project folder. Now Git will monitor your files and allow you to commit updates.

Committing Code with .env and node_modules Ignored

Node.js projects often include large node_modules folders and sensitive .env files. These should never be committed to Git. You can avoid this by creating a .gitignore file:

bash
1
          touch .gitignore
        

Inside .gitignore, add:

bash
1
2
          node_modules
.env
        

This tells Git to skip those files. Now you can safely commit your code without uploading unnecessary or private data.

bash
1
2
          git add .
git commit -m "Initial commit with project setup"
        

Hosting Code and Docs on GitHub

Once your code is clean and ready, you can push it to GitHub. First, create a new GitHub repository. Then connect your local project to it:

bash
1
2
          git remote add origin https://github.com/your-username/my-node-app.git
git push -u origin main
        

If you have documentation like a README.md, it will be displayed on the GitHub repo’s main page. This makes your project easier to understand for others and more professional if you're sharing your work with employers or the open-source community.

Git and GitHub together make it simple to manage, protect, and showcase your Node.js projects.