Skip to main content

The Key File .gitignore

When using Git, the most commonly typed everyday command is usually git add . which prepares all files in your folder that have undergone some change to be saved.

However, there are many files and folders that should never be recorded nor uploaded to GitHub:

  1. Heavy Dependencies: Like the insufferable node_modules/ folder in Node/React, which can weigh hundreds of megabytes and is easily installed with a simple command (npm install).
  2. Operating System Files: Like .DS_Store on Mac or Thumbs.db on Windows. They create unnecessary junk for the rest of your team's programmers.
  3. Environment files and credentials (Crucial): Like .env files that contain the real passwords to your databases, private API keys, or session tokens. Adding this to your historical Git compromises your system's security; it is the worst beginner mistake imaginable.
  4. Compiled Files: Log type files (e.g. log/debug.log) or generated .exe, .dist, pre-compiled TypeScript/SCSS code, etc.

How do I ignore these files?

To explicitly tell Git which elements it should turn "blind" to and forget permanently, we use an invisible file in the system located at the root of the project named, and it must be named like this with a dot at the beginning: .gitignore.

Example of a common .gitignore file in React/Next/Node

You create this blank file at the start of your project at the base of the folder, and define specific paths there:

# dependencies
node_modules/

# logs
npm-debug.log*
yarn-debug.log*

# builds generated for production
dist/
out/
build/
.next/

# environment
.env
.env.local

# operating system
.DS_Store
Thumbs.db

Once a file or folder is listed inside .gitignore, no matter how many times you type git add ., that file will never end up in the waiting room or in the cloud, evading serious errors and keeping the local repository and the remote GitHub clean and light.