How to Add a .gitignore File

Create a .gitignore file so Git skips dependencies, secrets, and system files, commit it, and untrack files that were already committed by mistake.

Git tracks every file in the repository unless told otherwise, including node_modules, .env secrets, and editor clutter. A .gitignore file lists the patterns Git should skip.

Create the file at the repository root

touch .gitignore

The name is exactly .gitignore, with the leading dot and no extension. Place it in the top-level folder of the repository, next to the .git directory. On Windows Command Prompt, type nul > .gitignore creates it.

Add one pattern per line

Open the file and list what to ignore:

node_modules/
.venv/
.env
.DS_Store
*.log

A trailing slash matches a directory. * is a wildcard, so *.log matches any log file in any folder. Lines starting with # are comments. Patterns match at any depth unless they start with /, which anchors them to the repository root.

Check that it works

git status

Ignored files no longer appear in the untracked list. To ask Git why a specific file is or is not ignored:

git check-ignore -v node_modules/

It prints the file, line, and pattern in .gitignore that matched.

Commit the .gitignore file

git add .gitignore
git commit -m "Add .gitignore"

The file is part of the repository, so everyone who clones it gets the same rules.

Untrack files that were already committed

.gitignore only affects files Git is not already tracking. If .env was committed before the rule existed, remove it from the index while keeping it on disk:

git rm --cached .env

For a directory, add -r, as in git rm -r --cached node_modules/. Then commit. The file stays in your working folder but Git stops tracking it. It remains in earlier commits, so rotate any secret that was pushed.

Ready-made templates. github.com/github/gitignore has maintained .gitignore files for nearly every language and framework. Copy the one for your stack (Node, Python, Go, and so on) as a starting point and add project-specific lines below it.

Ignoring files only on your machine. Patterns in .git/info/exclude work the same way but are never committed, which suits personal editor settings that teammates do not share.

More Coding how-tos