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.
๐ 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?
| Category | Examples | Why |
|---|---|---|
| Dependencies | node_modules/, venv/, .venv/ | Huge, and recreated with npm install or pip install |
| Build output | dist/, build/, target/, *.class | Generated from source code |
| Test results and reports | test-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
| Pattern | Matches |
|---|---|
debug.log | A file named debug.log in any folder |
*.log | Any file ending in .log |
build/ | Any folder named build (and everything inside) |
/build/ | Only the build folder in the repository root |
docs/*.pdf | PDFs directly inside docs, not in its subfolders |
docs/**/*.pdf | PDFs anywhere inside docs |
**/temp | A temp file or folder at any depth |
!important.log | Exception: do not ignore this file (negation) |
# comment | A 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:
| Location | Scope |
|---|---|
.gitignore in the repo | Everyone working on the project (committed) |
.git/info/exclude | Only this repository on your computer (not committed) |
Global file, set with git config --global core.excludesFile ~/.gitignore_global | All repositories on your computer |
๐ Assignment
- In
git-practice, createnode_modules/test.js,app.log,.envandreport/index.html, and checkgit status. - Write a .gitignore that ignores all four, commit it, and check
git statusagain. - Add
!keep.logas an exception and confirm withgit check-ignore -v. - Commit a file called
secret.txt, then stop tracking it withgit rm --cachedand ignore it. - Set up a global ignore file for your OS and editor files.
- 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
- โฉ๏ธ Undo Changes: restore, reset, revert and amend
- ๐ Remote Repositories: origin, upstream and git remote
- ๐ Git and GitHub Tutorial: full course
๐ฌ 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)”