.gitignore Explained: Ignore Files in Git the Right Way (with Templates)

Some files should never be in Git: dependencies, test reports, logs and especially secrets. In this lesson you will write a proper .gitignore, understand its pattern syntax, use ready-made templates and fix the classic “.gitignore is not working” problem.

Advertisement

๐Ÿ“– What You’ll Learn: what .gitignore does, what to ignore, creating and committing a .gitignore, pattern syntax and negation, templates for Node, Python and Playwright, untracking already committed files, check-ignore, and personal and global ignore rules.


๐Ÿ™ˆ What Is .gitignore?

Not every file in your project folder belongs in Git. Dependencies, build output, logs, test reports and secrets should stay out of the repository. A .gitignore file is a plain text file, usually in the repository root, that lists patterns of files and folders Git should ignore.

Ignored files do not show up as “untracked” in git status, and git add . skips them.


๐Ÿšซ What Should You Ignore?

CategoryExamplesWhy
Dependenciesnode_modules/, venv/, .venv/Huge, and recreated with npm install or pip install
Build outputdist/, build/, target/, *.classGenerated from source code
Test results and reportstest-results/, playwright-report/, allure-results/, coverage/Generated on every run
Secrets.env, *.pem, credentials.json, playwright/.auth/Must never be shared publicly
Logs and temp files*.log, *.tmp, .cache/Noise
OS and editor files.DS_Store, Thumbs.db, .idea/, .vscode/ (optional)Personal, not project files

โœ๏ธ Create Your First .gitignore

In the repository root:

# Dependencies
node_modules/

# Environment variables and secrets
.env
.env.*
!.env.example

# Test output
test-results/
playwright-report/
allure-results/

# Logs
*.log

# OS files
.DS_Store
Thumbs.db

Commit the .gitignore file itself so the whole team shares the same rules:

git add .gitignore
git commit -m "Add .gitignore for dependencies, secrets and test output"

๐Ÿงฉ Pattern Syntax

PatternMatches
debug.logA file named debug.log in any folder
*.logAny file ending in .log
build/Any folder named build (and everything inside)
/build/Only the build folder in the repository root
docs/*.pdfPDFs directly inside docs, not in its subfolders
docs/**/*.pdfPDFs anywhere inside docs
**/tempA temp file or folder at any depth
!important.logException: do not ignore this file (negation)
# commentA comment line

โš ๏ธ Common mistake: A negation (!file) cannot re-include a file if its parent folder is ignored. Ignore the folder’s contents (logs/*) instead of the folder itself (logs/) when you need exceptions.


๐Ÿงฐ Ready-Made Templates

You rarely write a .gitignore from scratch:

  • When creating a repository on GitHub, choose a template from the Add .gitignore dropdown (Node, Python, Java, VisualStudio and many more).
  • GitHub maintains the official collection in the github/gitignore repository.
  • Generator websites such as gitignore.io combine templates for your language, framework and editor.

Example: Playwright with TypeScript project

node_modules/
/test-results/
/playwright-report/
/blob-report/
/playwright/.cache/
/playwright/.auth/
.env

This matches what npm init playwright@latest generates, plus the folders for saved login sessions and secrets. See Install Playwright.

Example: Python project

__pycache__/
*.py[cod]
.venv/
venv/
.pytest_cache/
.env
reports/

๐Ÿงน Ignoring a File That Is Already Tracked

Adding a file to .gitignore does not affect files Git already tracks. Stop tracking it first:

git rm --cached .env                 # stop tracking, keep the file on disk
git rm -r --cached node_modules      # a whole folder
git commit -m "Stop tracking .env and node_modules"

โš ๏ธ Common mistake: If you committed a secret (password, token, API key) and pushed it, removing it from the latest commit is not enough: it is still in the history. Revoke and rotate the secret immediately, then clean history if needed. GitHub’s secret scanning can detect many leaked tokens: see Secrets, Dependabot and code scanning.


๐Ÿ”Ž Debugging: Why Is (or Isn’t) a File Ignored?

git check-ignore -v playwright-report/index.html
.gitignore:3:/playwright-report/	playwright-report/index.html

The output shows which file and line caused the match. List everything currently ignored with git status --ignored.


๐ŸŒ Personal Ignore Rules

Rules that only matter to you (your editor or OS) can live outside the project:

LocationScope
.gitignore in the repoEveryone working on the project (committed)
.git/info/excludeOnly this repository on your computer (not committed)
Global file, set with git config --global core.excludesFile ~/.gitignore_globalAll repositories on your computer

๐Ÿ“ Assignment

  1. In git-practice, create node_modules/test.js, app.log, .env and report/index.html, and check git status.
  2. Write a .gitignore that ignores all four, commit it, and check git status again.
  3. Add !keep.log as an exception and confirm with git check-ignore -v.
  4. Commit a file called secret.txt, then stop tracking it with git rm --cached and ignore it.
  5. Set up a global ignore file for your OS and editor files.
  6. Bonus: generate a .gitignore for Node, Python and VS Code from a template and compare it with yours.

โ“ FAQ

๐Ÿค” What is .gitignore in Git?

A text file that lists files and folders Git should not track, such as dependencies, build output, logs and secrets.

๐Ÿค” Why is .gitignore not working?

Usually because the file is already tracked. Run git rm --cached <file> and commit. Also check the pattern with git check-ignore -v <file>.

๐Ÿค” Should I commit the .gitignore file?

Yes. Committing it shares the same ignore rules with everyone on the project.

๐Ÿค” Should I ignore the .env file?

Yes. .env files usually contain secrets. Commit a .env.example with placeholder values instead.

๐Ÿค” How do I ignore a folder in Git?

Add the folder name with a trailing slash, for example node_modules/, to .gitignore.


๐ŸŽฏ Next Steps

๐ŸŽฌ Learn Git and GitHub step by step: watch the series on YouTube!

Subscribe to SqaToolsByDY • Practice on sqatools.in

6 thoughts on “.gitignore Explained: Ignore Files in Git the Right Way (with Templates)”

Leave a Comment