# Ignoring Files

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

Subject: Version Control | Lesson 7 of 29 | 3 min read
Interactive lesson: https://codewithanimation.in/git/gitignore
Previous: [Inspecting History](https://codewithanimation.in/git/git-log.md) | Next: [What is a Branch?](https://codewithanimation.in/git/what-is-a-branch.md)

## 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.

## Common mistake: Ignoring an already tracked file

The #1 mistake is accidentally committing an API key, then trying to fix it by adding it to `.gitignore`. Git will ignore *future* changes to that file, but the file (and the key) is still in the history! You have to explicitly use `git rm --cached <file>` to untrack it first.

## Tip: Use community templates

You don't need to write `.gitignore` files from scratch. Sites like `gitignore.io` or GitHub's template repository provide perfect starting points tailored to exactly the language and framework you are using.

## Try it: Apply ignore rules to untracked files

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

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/git/gitignore

## Check yourself

### Question 1

What is the purpose of the `.gitignore` file?

- A. To delete unwanted files from your computer.
- B. To tell Git which files and folders it should not track or commit.
- C. To encrypt sensitive files so they are safe to upload.
- D. To ignore error messages in the terminal.

Correct answer: B

The `.gitignore` file acts as a filter, preventing specific files (like `node_modules` or `.env`) from ever making it into your Staging Area or Repository.

- Why not A: `.gitignore` doesn't delete files, it just hides them from Git.
- Why not C: Git doesn't encrypt files. If it's sensitive, don't commit it at all.
- Why not D: It ignores files, not terminal output.

### Question 2

What happens if you add a file to `.gitignore` AFTER you have already committed it to the repository?

- A. Git will immediately delete the file from your computer.
- B. Git will automatically remove it from the repository.
- C. Git will continue tracking the file as if nothing happened.
- D. The file becomes locked and cannot be edited.

Correct answer: C

`.gitignore` only stops Git from tracking *untracked* files. If a file is already in the history, you must run `git rm --cached <filename>` to untrack it before the ignore rules take effect.

- Why not A: Git rarely deletes your working files automatically.
- Why not B: Git won't forget a file it's already tracking unless you tell it to.
- Why not D: You can still edit it, and Git will still track the changes.

### Question 3

How would you write a rule in `.gitignore` to ignore every file ending in `.log`?

- A. ignore .log
- B. *.log
- C. /log/*
- D. all.log

Correct answer: B

The asterisk `*` is a wildcard character that means 'anything'. So `*.log` translates to 'any file name that ends with .log'.

- Why not A: This isn't valid syntax.
- Why not C: This ignores a folder named `log`, not files ending in `.log`.
- Why not D: This would only ignore a specific file named literally `all.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.
