Not everything in a project folder belongs in Git. Logs, build output, downloaded dependencies, editor settings and, above all, secrets must stay out of the repository. A .gitignore file tells Git which files to ignore so they never show up in git status and never get committed by a careless git add .. This lesson covers the pattern syntax, what to ignore in DevOps projects, and what to do when something was committed by mistake.
In simple words
- .gitignore: a file listing what Git should never track, such as build output, logs and secrets.
- Pattern: a rule like *.log or build/ that matches many files at once.
- Untrack: stop tracking a file that was committed by mistake, without deleting it from your disk (git rm --cached).
Why ignoring matters
- Security. A committed
.env, private key or cloud credential is a leak. Public repositories are scanned by bots within minutes, and even in private repos the secret lives forever in history. - Noise. Generated files change constantly and bury real changes in
git statusand in diffs. - Size and speed.
node_modules/or a.terraform/folder can hold thousands of files and hundreds of megabytes. - Conflicts. Machine-specific files (IDE settings, local logs) cause pointless merge conflicts.
See the problem
git config --global user.name "Leela Kumar"mkdir ignore-labcd ignore-labgit initprintf 'print("hi")\n' > app.pyprintf 'DB_PASSWORD=s3cret\n' > .envprintf 'started\n' > app.logmkdir -p build __pycache__printf 'binary\n' > build/app.binprintf 'bytecode\n' > __pycache__/app.cpython-313.pycgit status -s?? .env?? __pycache__/?? app.log?? app.py?? build/A git add . now would commit your database password.
Writing a .gitignore
Create a file called .gitignore in the repository root, one pattern per line:
printf '# secrets\n.env\n*.pem\n\n# logs and build output\n*.log\nbuild/\n__pycache__/\n' > .gitignorecat .gitignoregit status -s# secrets.env*.pem# logs and build output*.logbuild/__pycache__/?? .gitignore?? app.pyOnly the files that belong in the repository are left, plus .gitignore itself, which you should commit so the whole team shares the same rules:
git add .git commit -m "Add app and gitignore"Pattern syntax
| Pattern | Matches |
|---|---|
app.log | a file or folder named app.log anywhere |
*.log | every name ending in .log, in any folder |
build/ | a folder named build (and everything inside), anywhere |
/build/ | only the build folder at the repository root |
docs/*.pdf | PDFs directly inside docs/ |
**/temp | temp at any depth |
logs/** | everything inside logs/ |
!keep.log | negation: do not ignore this, even if an earlier rule did |
# comment | a comment; blank lines are ignored |
Rules are read top to bottom and the last matching rule wins, which is how negation works:
*.log!audit.logOne limitation: you cannot re-include a file if its parent folder is ignored. logs/ followed by !logs/keep.txt does not work; use logs/* then !logs/keep.txt.
Which rule is ignoring my file?
When a file mysteriously refuses to show up, ask Git:
git check-ignore -v app.loggit check-ignore -v build/app.bin.gitignore:6:*.log app.log.gitignore:7:build/ build/app.binThe output shows the file, line number and pattern responsible.
If you really need to add an ignored file once, force it with git add -f file.
What to ignore in DevOps projects
# Secrets and local config — never commit.env.env.*!.env.example*.pem*.key*.tfvars # often hold secrets; commit *.tfvars.example insteadkubeconfig*# Terraform.terraform/*.tfstate*.tfstate.*crash.log# Python__pycache__/*.pyc.venv/# Node.jsnode_modules/dist/# Java / Maventarget/# Editors and OS files.idea/.vscode/.DS_StoreNote two subtle points: Terraform state files contain secrets and must live in a remote backend, not in Git; and .terraform.lock.hcl should be committed, because it pins provider versions. Ready-made templates for most languages are on github.com/github/gitignore.
Global ignores for your machine
Files created by your tools (macOS .DS_Store, your editor's swap files) belong in a personal global ignore file, not in every project:
git config --global core.excludesFile ~/.gitignore_globalprintf '.DS_Store\n*.swp\n' >> ~/.gitignore_globalAlready committed? Untrack it
.gitignore only affects untracked files. If a file is already committed, adding it to .gitignore does nothing. Remove it from the index but keep it on disk with git rm --cached:
printf 'local settings\n' > settings.localgit add settings.localgit commit -m "Oops, add local settings"printf 'settings.local\n' >> .gitignoregit rm --cached settings.localgit commit -am "Stop tracking settings.local"git status -slsrm 'settings.local'app.log app.py build __pycache__ settings.localThe file is still on disk, no longer tracked, and ignored from now on.
If a secret was committed
git rm --cached removes the file from future commits, but the secret is still in the history and in every clone. Treat it as leaked:
- Rotate the secret immediately (new password, new key). This is the only real fix.
- Remove it from history with a tool such as
git filter-repoor BFG, then force-push and ask everyone to re-clone. Covered in the history-rewriting lesson. - Add secret scanning (for example
gitleaksin a pre-commit hook and in CI) so it does not happen again.
Real-world scenario: the Terraform state in Git
A team committed terraform.tfstate in their first week, "to share it". The state file contained database passwords in plain text and was readable by every developer and every CI job. Worse, two engineers applying at the same time overwrote each other's state. The fix: move state to a remote backend (S3 with locking), rotate all credentials found in it, purge the file from history, and add *.tfstate* and .terraform/ to .gitignore.
Common mistakes
- Adding to
.gitignoreafter the file is committed and wondering why nothing changes. Usegit rm --cached. - Not committing
.gitignore, so teammates do not get the rules. - Ignoring
.env.exampletoo: use!.env.exampleto keep the template. - Ignoring lock files like
package-lock.jsonor.terraform.lock.hcl, which should be committed for reproducible builds. - Thinking
git rm --cachedremoves a secret from history. It does not.
Practice
- In a new repository, create
app.py,debug.log,.envand adist/folder with a file, then write a.gitignoreso that onlyapp.pyand.gitignoreremain ingit status. - Add a rule that ignores all
.logfiles exceptaudit.log, and verify both withgit check-ignore -v. - Commit a file, then stop tracking it with
git rm --cachedwhile keeping it on disk.
Cheat sheet
.gitignore patterns, one per line, committed*.log build/ /root-only !exception **/any-depthgit status --ignored also list ignored filesgit check-ignore -v file which rule ignores a filegit add -f file add an ignored file anywaygit rm --cached file untrack, keep on diskgit config --global core.excludesFile ~/.gitignore_globalKey takeaways
.gitignorekeeps secrets, logs, build output and dependencies out of the repository; commit it with the project.- Patterns support wildcards, folder-only rules, root anchoring and
!exceptions; the last match wins. git check-ignore -vexplains why a file is ignored.- Ignoring does not affect tracked files; untrack them with
git rm --cached. - A committed secret is leaked: rotate it first, then clean the history.