Skip to content
$ lynuxpro
← Git

LESSON 6 OF 21 · BEGINNER · 15 MIN

Ignoring files with .gitignore

HOW IT WORKSHow .gitignore filters files

Files in the foldercode, logs, .env.gitignore*.log .env build/Trackedapp.py, README.mdIgnorednever addedAlready tracked?git rm --cached
1/5

Projects contain files that must never be committed: logs, build output, dependencies and secrets.

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 status and 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
SHELLbashutf-8 · 11L
?? .env?? __pycache__/?? app.log?? app.py?? build/
OUTPUTterminalutf-8 · 5L

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
SHELLbashutf-8 · 3L
# secrets.env*.pem# logs and build output*.logbuild/__pycache__/?? .gitignore?? app.py
OUTPUTterminalutf-8 · 10L

Only 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"
SHELLbashutf-8 · 2L

Pattern syntax

PatternMatches
app.loga file or folder named app.log anywhere
*.logevery 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/*.pdfPDFs directly inside docs/
**/temptemp at any depth
logs/**everything inside logs/
!keep.lognegation: do not ignore this, even if an earlier rule did
# commenta comment; blank lines are ignored

Rules are read top to bottom and the last matching rule wins, which is how negation works:

*.log!audit.log
OUTPUTterminalutf-8 · 2L

One 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
SHELLbashutf-8 · 2L
.gitignore:6:*.log	app.log.gitignore:7:build/	build/app.bin
OUTPUTterminalutf-8 · 2L

The 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_Store
OUTPUTterminalutf-8 · 31L

Note 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_global
SHELLbashutf-8 · 2L

Already 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 -sls
SHELLbashutf-8 · 8L
rm 'settings.local'app.log  app.py  build  __pycache__  settings.local
OUTPUTterminalutf-8 · 2L

The 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:

  1. Rotate the secret immediately (new password, new key). This is the only real fix.
  2. Remove it from history with a tool such as git filter-repo or BFG, then force-push and ask everyone to re-clone. Covered in the history-rewriting lesson.
  3. Add secret scanning (for example gitleaks in 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 .gitignore after the file is committed and wondering why nothing changes. Use git rm --cached.
  • Not committing .gitignore, so teammates do not get the rules.
  • Ignoring .env.example too: use !.env.example to keep the template.
  • Ignoring lock files like package-lock.json or .terraform.lock.hcl, which should be committed for reproducible builds.
  • Thinking git rm --cached removes a secret from history. It does not.

Practice

  1. In a new repository, create app.py, debug.log, .env and a dist/ folder with a file, then write a .gitignore so that only app.py and .gitignore remain in git status.
  2. Add a rule that ignores all .log files except audit.log, and verify both with git check-ignore -v.
  3. Commit a file, then stop tracking it with git rm --cached while 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_global
OUTPUTterminalutf-8 · 7L

Key takeaways

  • .gitignore keeps 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 -v explains 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.