1. Create the file

touch .gitignore

That makes an empty .gitignore in the current folder. The leading dot makes it a hidden file, which is normal.

2. Add patterns, one per line

# dependencies
node_modules/

# logs
*.log

# environment files
.env

# build output
dist/

A trailing slash like node_modules/ matches a folder, while *.log matches every file ending in .log. Lines starting with # are comments.

3. Ignore everything except one file

*
!important.txt

The * ignores everything and !important.txt un-ignores a single file. The exclamation mark makes an exception to an earlier rule.

Common mistakes

  • Adding a file that is already tracked. .gitignore only affects untracked files. Run git rm --cached file first to stop tracking it.
  • Misnaming the file. It must be exactly .gitignore, with the dot and no extension.
  • Putting it in the wrong place. Keep it in the project root so it covers the whole repository.

Frequently asked questions

Why is Git still tracking a file I added to .gitignore?

Because it was already committed. Ignoring only works on untracked files. Run git rm --cached path/to/file and commit, then it will be ignored going forward.

Can I have more than one .gitignore?

Yes. You can put a .gitignore in any folder, and its rules apply to that folder and below. A root one is enough for most projects.

New to version control? See our complete guide to learning to code.