Skip to main content
RapidDev - Software Development Agency
github-for-non-tech

How to Use .gitignore in a GitHub Repository

A .gitignore file tells GitHub which files to skip when tracking changes. You create it in your repository, list file patterns like node_modules/ or .env, and GitHub will ignore those files from that point forward. This keeps your repository clean and prevents sensitive data like API keys from being accidentally uploaded.

What you'll learn

  • What a .gitignore file is and why every project needs one
  • How to create a .gitignore file directly on GitHub's website
  • Common file patterns to ignore for AI-built projects
  • How to use GitHub's built-in .gitignore templates
Book a free consultation
4.9Clutch rating
600+Happy partners
17+Countries served
190+Team members
Beginner7 min read10 minutesAny modern web browser — works on GitHub Free plan and aboveMarch 2026RapidDev Engineering Team
TL;DR

A .gitignore file tells GitHub which files to skip when tracking changes. You create it in your repository, list file patterns like node_modules/ or .env, and GitHub will ignore those files from that point forward. This keeps your repository clean and prevents sensitive data like API keys from being accidentally uploaded.

Why Your Repository Needs a .gitignore File

When you add files to a GitHub repository, Git tracks every single file in the folder — including ones you probably don't want to share. Temporary files, dependency folders (like node_modules/ which can contain thousands of files), local configuration, and most importantly secret files like .env that contain API keys and passwords. A .gitignore file is like a "do not enter" list for Git. You write the names or patterns of files you want Git to skip, and it will pretend they don't exist. This is especially important when working with AI tools like Lovable, V0, Cursor, or Replit. These tools generate projects with dependency folders and configuration files that should never be committed to your repository. Most AI tools create a .gitignore automatically, but if yours didn't — or if you started a blank repository — you'll want to add one yourself. The good news: GitHub has built-in templates for common project types, so you rarely need to write one from scratch.

Prerequisites

  • A GitHub account
  • An existing repository (even an empty one works)
  • Basic understanding of what a repository is

Step-by-step guide

1

Navigate to your repository on GitHub

Open github.com in your browser and click on the repository where you want to add a .gitignore file. You'll see the repository's file list on the main page. Look through the files to check if a .gitignore file already exists — it will be listed alphabetically with other files. If you already see one, you can click on it to view and edit it. If you don't see one, you'll create it in the next step.

Expected result: You're on your repository's main page and have confirmed whether a .gitignore file exists.

2

Create a new .gitignore file using the Add file button

Click the "Add file" dropdown button above the file list (to the right of the branch dropdown). Select "Create new file" from the dropdown. In the filename field at the top of the editor, type exactly: .gitignore (including the dot at the beginning — the dot is important, it makes the file hidden on most operating systems). The editor will now recognize that you're creating a gitignore file and may offer autocomplete suggestions.

Expected result: You see a blank file editor with ".gitignore" as the filename.

3

Add common ignore patterns for AI-built projects

In the editor, type each pattern on its own line. Start with the most important ones: type "node_modules/" on the first line — this ignores the massive dependency folder that can contain 50,000+ files. On the next line, type ".env" — this prevents your secret API keys from being uploaded. Add ".env.local" on the next line for local environment overrides. Then add "dist/" to ignore build output, and ".DS_Store" to ignore macOS system files. Each pattern goes on its own line. Lines starting with # are comments — use them to organize your file. For example, type "# Dependencies" above node_modules/ to label that section.

Expected result: Your .gitignore file has at least 5 common patterns, each on its own line.

4

Use GitHub's template dropdown for additional patterns

While you're editing the .gitignore file, look for a dropdown menu in the editor labeled "Choose .gitignore template" or a similar prompt near the top of the editing area. Click it and type "Node" to find the Node.js template — this is the most relevant template for projects built with Lovable, V0, or any React-based AI tool. Selecting a template will populate the editor with a comprehensive list of patterns used by professional Node.js developers. You can merge these with the patterns you already typed, or replace your file entirely with the template.

Expected result: Your .gitignore now includes comprehensive patterns from the Node.js template.

5

Commit the .gitignore file to your repository

Scroll down below the editor to the "Commit changes" section. In the commit message field, type something descriptive like "Add .gitignore to exclude dependencies and secrets." Leave the "Commit directly to the main branch" option selected. Click the green "Commit changes" button. Your .gitignore file is now active — GitHub will immediately start ignoring any matching files in future commits. Note: if files matching your patterns were already committed before you added the .gitignore, they will still be tracked. The .gitignore only prevents new files from being added.

Expected result: The .gitignore file appears in your repository's file list and is active immediately.

6

Verify that ignored files are not showing up in commits

To confirm your .gitignore is working, try adding a file that matches one of your patterns. For example, if you added ".env" to your .gitignore, create a new file called .env through the GitHub web interface. You'll notice that GitHub still lets you create it manually through the web editor — .gitignore primarily affects tools like GitHub Desktop, Lovable, and other Git clients that push multiple files at once. To fully verify, push a change from GitHub Desktop or your AI tool and check that the ignored files don't appear in the commit's file list on github.com.

Expected result: Files matching your .gitignore patterns do not appear in new commits made from Git clients.

Complete working example

.gitignore
1# Dependencies
2node_modules/
3.pnp
4.pnp.js
5
6# Environment variables (secrets)
7.env
8.env.local
9.env.development.local
10.env.test.local
11.env.production.local
12
13# Build output
14dist/
15build/
16.next/
17
18# IDE and editor files
19.vscode/
20.idea/
21*.swp
22*.swo
23
24# Operating system files
25.DS_Store
26Thumbs.db
27
28# Debug logs
29npm-debug.log*
30yarn-debug.log*
31yarn-error.log*
32
33# Supabase local config
34supabase/.temp/

Common mistakes when using .gitignore in a GitHub Repository

Why it's a problem: Forgetting the dot at the beginning of .gitignore

How to avoid: The file must be named ".gitignore" with a leading dot. A file named "gitignore" (without the dot) won't work.

Why it's a problem: Adding .gitignore after sensitive files were already committed

How to avoid: The .gitignore only prevents future tracking. If .env was already committed, it's still in the repository history. You'll need to delete the file from the repo and rotate any exposed keys immediately.

Why it's a problem: Not including .env in the .gitignore file

How to avoid: Always add .env, .env.local, and any environment variable files. These contain API keys and secrets that should never be in a public repository.

Why it's a problem: Ignoring the entire .github/ folder

How to avoid: Don't add .github/ to your .gitignore. This folder contains your GitHub Actions workflows and issue templates, which you want to keep in your repository.

Best practices

  • Always create a .gitignore file before your first commit — it's easier to prevent tracking than to undo it
  • Include .env and all environment variable files to protect API keys and secrets
  • Use GitHub's built-in templates as a starting point for your project type
  • Add comments (lines starting with #) to organize patterns into logical sections
  • Never ignore the .gitignore file itself — it should always be committed to the repository
  • Review your .gitignore whenever you add a new tool or framework to your project
  • If working with AI tools like Lovable or V0, check whether they auto-generated a .gitignore and fill in any gaps

Still stuck?

Copy one of these prompts to get a personalized, step-by-step explanation.

ChatGPT Prompt

I'm building a React + Supabase app with Lovable and it's connected to GitHub. Generate a comprehensive .gitignore file that covers Node.js dependencies, environment variables, build output, and common IDE files. Add comments explaining each section.

Frequently asked questions

Can I add .gitignore to an existing repository that already has files?

Yes. Adding a .gitignore to an existing repository will prevent matching files from being tracked in future commits. However, files that were already committed will remain in the repository history until manually removed.

Does .gitignore delete files from my computer?

No. The .gitignore file only tells Git to stop tracking certain files. Your local files remain untouched. It simply prevents those files from being uploaded to GitHub.

I accidentally committed my .env file with API keys. What should I do?

First, add .env to your .gitignore and commit that change. Then delete the .env file from the repository by clicking on it and using the trash icon. Most importantly, immediately rotate (change) any API keys that were exposed. If you need help cleaning sensitive data from repository history, RapidDev can assist with that.

Do AI tools like Lovable create .gitignore automatically?

Most AI tools including Lovable, V0, and Replit create a .gitignore file when they initialize a project. However, the default file may not cover everything. Always review it and add patterns specific to your project.

Why is node_modules/ the first thing everyone adds to .gitignore?

The node_modules folder contains all your project's dependencies — often 50,000 or more files. These files are downloaded automatically when you run the project, so there's no reason to store them in your repository. Including them would make your repo massive and slow.

Can I use a wildcard pattern to ignore multiple file types?

Yes. Use an asterisk (*) as a wildcard. For example, '*.log' ignores all files ending in .log, and 'temp-*' ignores all files starting with 'temp-'. You can also use ** to match nested directories, like 'logs/**/*.log'.

RapidDev

Talk to an Expert

Our team has built 600+ apps. Get personalized help with your project.

Book a free consultation

Need help with your project?

Our experts have built 600+ apps and can accelerate your development. Book a free consultation — no strings attached.

Book a free consultation

We put the rapid in RapidDev

Need a dedicated strategic tech and growth partner? Discover what RapidDev can do for your business! Book a call with our team to schedule a free, no-obligation consultation. We'll discuss your project and provide a custom quote at no cost.