Ignoring files and large files
Keep secrets, logs and build output out of your repository with a .gitignore file.
- Write .gitignore patterns for secrets, logs and build output
- Explain why build output should not be committed
- Test whether a pattern matches with git check-ignore
- Untrack an already-committed file with git rm --cached
In plain English
A .gitignore file is a list of patterns that tells Git which files and folders to leave out of version control, such as secrets, logs and generated build output. Build output is the compiled or generated files your tools create from your source code, and it can be recreated at any time, so committing it wastes space and drowns real changes in noise. A secret is any private value such as a password or API key that must never be published, because once it is committed it stays in history. A tracked file is one Git is already watching; adding it to .gitignore does nothing until you tell Git to stop tracking it.
Step by step
- 1Create a .gitignore file and list one pattern per line.
- 2Test a pattern with git check-ignore to confirm it matches the right files.
- 3Remember that .gitignore only affects files Git is not already tracking.
- 4Untrack an already-committed file with git rm --cached so it stops being watched.
printf "node_modules/
*.log
.env
" > .gitignore
git check-ignore -v node_modules
git rm --cached .env
git rm --cached -r build
git statusCommon mistakes
- Adding a secret to .gitignore after it was already committed, which does not remove it from history.
- Committing node_modules or build output, which bloats the repository and hides real changes.
- Using the wrong wildcard and ignoring far more files than intended.
- Believing that adding a tracked file to .gitignore will make Git stop watching it.
Ignore and untrack build output
Create a .gitignore that ignores the build folder and all .log files, then stop Git from tracking a build folder that was already committed.
A file is already tracked, and you add it to .gitignore. What else must you do?
Make it yours.
Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.
Lock in what you just learned.
Mark this lesson complete to save your place and move one step closer to the certificate.