Ignoring files with .gitignore
Keep secrets, downloaded libraries, logs and system files out of your repository with a .gitignore file, learn its patterns, and stop tracking a file you committed by mistake.
🎯 By the end of this lesson you will be able to:
- Explain which files should never be committed
- Write .gitignore patterns for files, folders and file types
- Stop tracking an already-committed file with git rm --cached
Outline
What you'll learn and why it matters
Not every file in your project folder belongs in Git. Passwords and API keys must stay private. Downloaded libraries can be re-installed and are huge. Log files and system files like .DS_Store (Mac) or Thumbs.db (Windows) are just clutter. A .gitignore file tells Git to pretend these files do not exist.
| Never commit | Examples | Why |
|---|---|---|
| Secrets | .env, files with passwords, API keys or tokens | Anyone who sees the repository can use them, even after you delete them later |
| Installed libraries | node_modules/, venv/ | Huge; anyone can reinstall them from a list |
| Generated files | *.log, dist/, build/ | Created automatically; they change all the time |
| Personal and system files | .DS_Store, Thumbs.db, editor settings | Only matter on your computer |
The problem
Asha's project now has a secret key in .env, a downloaded library and some log files. Look at the status:
git status --shortOutput
?? .env ?? debug.log ?? error.log ?? node_modules/
?? means untracked. One careless git add . would commit all of it, including the secret key.
Create a .gitignore file
Create a file named exactly .gitignore (starting with a dot, no other extension) in the top folder of the project, with one pattern per line:
# Secrets: never commit these
.env
# Installed libraries
node_modules/
# Log files
*.log
# System files
.DS_Store
Thumbs.dbprintf '# Secrets: never commit these\n.env\n\n# Installed libraries\nnode_modules/\n\n# Log files\n*.log\n\n# System files\n.DS_Store\nThumbs.db\n' > .gitignore
git status --short
git add .gitignore
git commit -m "Ignore secrets, libraries and logs"Output
?? .gitignore [main 7f7acf1] Ignore secrets, libraries and logs 1 file changed, 12 insertions(+) create mode 100644 .gitignore
Now only .gitignore itself shows up. Commit it, so everyone who works on the project gets the same rules. (In a real project you would type the file in your editor rather than using printf.)
| Pattern | Ignores |
|---|---|
.env | Any file named .env |
node_modules/ | The folder and everything inside it |
*.log | Every file ending in .log |
/todo.txt | todo.txt in the top folder only |
# text | Nothing: lines starting with # are comments |
!keep.log | Exception: do not ignore keep.log, even though *.log matches it |
When a file is already committed
.gitignore only affects untracked files. If a file was committed before you ignored it, Git keeps tracking it. Remove it from Git (but keep it on your disk) with git rm --cached:
printf 'debug.log\n' > .gitignore
git rm --cached -q debug.log
git status --short
git commit -q -m "Stop tracking debug.log"
lsOutput
D debug.log ?? .gitignore debug.log index.html styles.css
D means the file is deleted *from the repository*, but ls shows it is still on disk. From now on Git ignores it.
Practice
Guided exercise
Ignore a temp folder and backups
Asha's folder has a temp/ folder and backup files ending in .bak. Write a .gitignore that ignores both, then show the short status.
- Create
.gitignorewith the linestemp/and*.bak. - Run
git status --short: only.gitignoreshould appear.
Show the solution
printf 'temp/\n*.bak\n' > .gitignore
git status --shortOutput
?? .gitignore
How to get there: The folder pattern ends in / and the wildcard *.bak matches any file ending in .bak. Only the .gitignore file itself is left to commit.
Your turn
Check why a file is ignored
With the .gitignore from the practice above, use git check-ignore -v to find which rule ignores index.html.bak.
Show the solution
git check-ignore -v index.html.bakOutput
.gitignore:2:*.bak index.html.bak
How to get there: check-ignore -v prints the file the rule is in, its line number, the pattern and the path it matched. It is the quickest way to debug a .gitignore that does not behave as you expect.
Common mistakes
Naming the file gitignore or .gitignore.txt
You see: Nothing is ignored
Fix: The name must be exactly .gitignore. On Windows, check file extensions are shown.
Adding a file to .gitignore after committing it
You see: Git still tracks the file
Fix: Run git rm --cached <file>, then commit.
Committing a secret and then just deleting it
You see: The secret stays in the history
Fix: Change or cancel the secret immediately, then remove it.
Not committing the .gitignore itself
You see: Teammates still commit the files you meant to ignore
Fix: Commit .gitignore like any other file.
Recap
- Never commit secrets, installed libraries, generated files or system files.
- List patterns in a .gitignore file at the top of the project and commit it.
- Patterns: names, folders ending in /, wildcards like *.log, comments with #, exceptions with !.
git rm --cached <file>stops tracking a committed file but keeps it on disk.
Key terms
- .gitignore
- A file listing patterns of files and folders Git should not track.
- Wildcard
- A * in a pattern that matches any characters, as in *.log.
- Secret
- A password, key or token that gives access to something and must be kept private.
Check your understanding
Answer all 4 questions, then check your answers to see an explanation for each one.
1. Which pattern ignores every file ending in .log?
2. Why should .env files never be committed?
3. You added a file to .gitignore, but Git still tracks it. Why?
4. You pushed a real API key to GitHub. What should you do first?
Mark this lesson complete
Finished this lesson?
When you have worked through the examples and exercises, mark it complete to save your progress.