.gitignore
.gitignore is a list of files Git should pretend not to see: dependencies, build output, logs and secrets.
On this page · 5 min · Intermediate
On this page
- Learning objectives
- Explanation
- Step-by-step screens
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Intermediate
- Updated
- Nov 25, 2026
Learning objectives#
- Write .gitignore patterns
- Use GitHub’s templates
- Stop tracking an already-committed file
- Keep secrets out of Git
Explanation#
Create a file called .gitignore in the repository root. Each line is a pattern: node_modules/ ignores a folder, *.log ignores every log file, .env ignores the secrets file, !keep.log makes an exception.
GitHub has templates for every language (github.com/github/gitignore). Choose one when creating a repository, or copy it later. dotnet new gitignore creates one for .NET projects.
.gitignore only affects untracked files. If you already committed .env, run git rm --cached .env to stop tracking it (the file stays on your disk), commit, and — because it is in the history — change every secret it contained.
Step-by-step screens#
Drawings of github.com, the Git installer, VS Code and a terminal. GitHub changes its layout from time to time; the button names stay the same. The red numbers match the numbered actions under each picture; names on your computer will differ.
Loading pictures…
Code example: A .gitignore for a Node.js web project#
# dependencies
node_modules/
# build output
dist/
build/
# logs
*.log
# secrets and local settings
.env
.env.*
!.env.example
# editor and OS files
.vscode/*
!.vscode/extensions.json
.DS_Store
Thumbs.db
git status no longer lists node_modules, dist, logs or .env; .env.example (with fake values) is still committed so others know which settings exist.
This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.
Step-by-step explanation#
- Folders end with a slash.
- * matches any characters.
- ! re-includes a file that an earlier rule ignored.
- # lines are comments.
Real-world use case#
A project without .gitignore uploaded 40,000 files from node_modules; with it, the repository holds only the 30 files the team wrote.
Common mistakes#
The secret is still in the history. Remove it with git rm --cached, and rotate (change) the secret.
Lock files should be committed so everyone installs the same versions.
Best practices#
- Add .gitignore before the first commit
- Start from a GitHub template
- Commit a .env.example
- Check git status before git add .
Remember this#
.gitignore = files Git ignores.
folder/ *.log .env
Already tracked? git rm --cached.
Practice exercise#
Your task: Stop tracking the already-committed file .env without deleting it from your disk.
Show solution
git rm --cached .env
git commit -m "Stop tracking .env"
Mini quiz#
Which pattern ignores every log file?
Want more? Practise all Git & GitHub MCQs · Git & GitHub interview questions
My notes#
Saved in this browser.