Learning navigation

Code with Animation

Ignoring Files

How to tell Git to completely ignore specific files and folders in your project.

Version Control3 min readConcept 7 of 29

The .gitignore file

Not every file in your project folder should be saved to Git. Some files are generated automatically, some are too large, and some contain secret passwords.

A .gitignore is a simple text file you place in the root of your project. Inside, you write a list of file names or folder names that you want Git to pretend don't exist.

Why it matters

If you commit a file containing API keys or passwords, they are permanently in your history and will be exposed when you push to GitHub.

If you commit the node_modules folder (which contains thousands of third-party library files), your repository will become massive, slow, and impossible for other developers to use.

How it works

Git reads the .gitignore file line by line. You can use exact file names (like .env), folder names (like node_modules/), or wildcards (like *.log to ignore all log files).

Crucially, a .gitignore file only works on untracked files. If you already committed a file, adding it to .gitignore later will not remove it from Git's memory.

Apply ignore rules to untracked files

Compare the files shown before and after these ignore rules. Already tracked files remain tracked.

# .gitignore
.env
node_modules/
*.log

Already tracked

src/index.ts: tracked
package.json: tracked

Untracked files

node_modules/

Ignored by matching rule

.env

Ignored by matching rule

debug.log

Ignored by matching rule

These rules hide matching untracked files from ordinary status output. An already tracked file is still tracked. Local simulation.

Check yourself

Pick an answer to lock it in, then read why. Getting one wrong is part of how it sticks.

  1. 1What is the purpose of the `.gitignore` file?
  2. 2What happens if you add a file to `.gitignore` AFTER you have already committed it to the repository?
  3. 3How would you write a rule in `.gitignore` to ignore every file ending in `.log`?

Remember this

  • Use .gitignore for secrets (.env) and generated code (node_modules).
  • Wildcards like *.log are supported.
  • It only works on files Git isn't already tracking.

Done with this concept?

Mark it complete to track your progress. No login needed.