TechEarl

How to Use .gitignore (with Examples)

A practical guide to .gitignore: pattern syntax, setting up a global gitignore with core.excludesFile, ready-made templates, and the gotcha that trips everyone up: already-tracked files keep showing up.

Ishan KarunaratnePublished ⏱️ 12 min readUpdated
Share thisCopied
Learn .gitignore pattern syntax, set up a global gitignore with core.excludesFile, and fix the top gotcha: a tracked file Git will not ignore.

A .gitignore file tells Git which files to leave untracked: build output, dependencies, secrets, OS junk. Create a plain text file named .gitignore, list one pattern per line (like node_modules/ or *.log), and commit it. The one trap: it only stops Git tracking files not already tracked. A file you already committed keeps showing up until you untrack it.

This page covers the pattern syntax, a global gitignore for your own OS and editor files, the fix for files Git already tracks, how to debug a stubborn pattern, and ready-made templates.

What does .gitignore actually do?

A .gitignore file tells Git which untracked files to ignore. Each line of .gitignore is a pattern, and any untracked file whose path matches a pattern is left out of git status and skipped by git add ., so it is not staged or committed by accident. A .gitignore rule has no effect on a file Git already tracks.

When you run git status, Git lists every file in your working directory that has changed or is new. Most of the time that is what you want. But some files have no business being in version control:

  • Dependency folders you can reinstall (node_modules/, vendor/, venv/).
  • Build artifacts you can regenerate (dist/, build/, *.o).
  • Local config and secrets (.env, config.local.php).
  • Editor and OS cruft (.DS_Store, .idea/, *.swp).

.gitignore is how you tell Git to skip those. If a file's path matches a pattern, Git pretends it does not exist for the purposes of git status and git add .. It will not be staged, it will not be committed, and it will not nag you in the status output.

This matters early. If you are still getting your bearings, the Git for beginners guide is the place to start, and understanding what the staging area is doing makes ignore rules click faster: ignored files simply never reach the staging area.

Creating your first .gitignore

The file lives at the root of your repository, alongside your .git folder. Create it like any text file:

bash
touch .gitignore
git status before and after adding a .gitignore file
Once .gitignore is in place, the ignored files drop out of git status.

Open it and add patterns, one per line. A simple Node.js project might start like this:

text
# Dependencies
node_modules/

# Build output
dist/
build/

# Environment files with secrets
.env
.env.local

# Logs
*.log

# macOS
.DS_Store

Save it, then commit it. The .gitignore file itself is meant to be tracked and shared, so everyone working on the project ignores the same things:

bash
git add .gitignore
git commit -m "Add .gitignore for build output and secrets"

That is the whole basic workflow. The interesting part is the pattern syntax.

.gitignore pattern syntax

Each non-blank, non-comment line is a pattern. Here is what the syntax gives you.

Comments and blank lines

A line starting with # is a comment. Blank lines are ignored and are handy for grouping. To match a file that literally starts with a hash, escape it with a backslash, as in \#weird-filename.

Plain names match anywhere

A bare name with no slash matches that name at any depth in the tree:

text
*.log

This ignores app.log, logs/error.log, and src/debug/trace.log, all of them, because there is no leading or internal slash to anchor it.

A trailing slash means "directory only"

Adding a / at the end restricts the match to directories:

text
build/

This ignores any directory named build and everything inside it, but it would not match a file named build (no extension). Use trailing slashes for folders like node_modules/, dist/, and .cache/.

A leading or internal slash anchors to the repo root

A slash anywhere except the very end anchors the pattern relative to the location of the .gitignore file:

text
/config.local.php
docs/build/

The first line ignores config.local.php only at the repository root, not a config.local.php buried in a subfolder. The second ignores build only inside docs, leaving a top-level build alone.

Wildcards: *, ?, and **

The glob characters behave roughly as they do in a shell:

PatternMatches
*.logAny file ending in .log
temp?temp1, tempA, any single character after temp
*.{jpg,png} is not supportedbrace expansion does not work in gitignore
**/logsA logs directory at any level
logs/**Everything under logs, at any depth
a/**/ba/b, a/x/b, a/x/y/b

Note the third row: gitignore does not support shell-style {jpg,png} brace expansion. Write two separate lines instead. A single * does not cross a directory separator; ** is the one that spans multiple path segments.

Negation: re-include something with !

A leading ! re-includes a file that an earlier pattern excluded. This is how you ignore a whole directory but keep one file in it:

text
# Ignore everything in logs/
logs/*

# ...except the rule that documents the format
!logs/README.md

There is one catch worth memorizing: you cannot re-include a file if its parent directory is itself ignored. Git never looks inside an ignored directory, so a ! on a file deep inside it has nothing to act on. Ignore the directory's contents (logs/*), not the directory itself (logs/), if you want a negation to work.

Set up a global gitignore

A global gitignore is for the files your machine and your tools create that have no business in anyone's repository: .DS_Store on a Mac, Thumbs.db on Windows, editor folders like .vscode/ and .idea/, Vim swap files like *.swp. Set one up once and Git ignores those patterns in every repo you ever clone, without touching a single project's own .gitignore. Two commands do it:

bash
git config --global core.excludesFile ~/.gitignore_global
printf '.DS_Store\nThumbs.db\n.idea/\n.vscode/\n*.swp\n' >> ~/.gitignore_global

The first line tells Git where your personal ignore file lives. The second creates that file (if it does not exist) and seeds it with the usual suspects.

Per-repo vs global: what goes where

Per-repo .gitignoreGlobal gitignore
LocationRoot of the repository (committed)A file you point Git at, e.g. ~/.gitignore_global
Shared with the team?Yes, it is committed and pushedNo, it is personal to your machine
Good forProject-specific output: dist/, vendor/, .env, coverage reportsThings tied to your tools: .DS_Store, Thumbs.db, .idea/, *.swp, *~

The test: would every developer on the team need to ignore this, or just developers using your OS and your editor? Build output and dependencies are properties of the project, so they belong in the committed .gitignore. Editor folders and OS metadata depend on your machine, so they belong in your global file. Committing your personal .idea/ ignore into a shared repo also imposes your editor choice on teammates who do not use it, which is mildly antisocial.

Use a home path, not the current directory

This is the mistake I see most often, and it is quiet because nothing errors. A lot of older write-ups tell you to run touch .gitignore_global and then edit .gitignore_global, both without a path. Those commands create the file in whatever directory you happen to be standing in, while the config points at ~/.gitignore_global in your home directory. You end up with a file in ~/Projects/some-repo/.gitignore_global that Git never reads, and .DS_Store keeps showing up in git status. Always use the ~/ path on both halves:

bash
git config --global core.excludesFile ~/.gitignore_global
touch ~/.gitignore_global

The ~ expands to your home directory (/Users/you on macOS, /home/you on Linux, %USERPROFILE% on Windows). The name and location are your choice as long as the config matches. Git's own default, if you set nothing, is $XDG_CONFIG_HOME/git/ignore (usually ~/.config/git/ignore), which is an equally valid target. Config keys are case-insensitive, so core.excludesfile and core.excludesFile are the same setting; the official docs spell it core.excludesFile.

Verify the global ignore is active

Read the value back:

bash
git config --get core.excludesFile

If it prints nothing, the config did not take. If it prints a path, confirm the file at that path is the one you are editing. Then, inside any repo, ask Git directly:

bash
git check-ignore -v .DS_Store

If the pattern is active, Git prints the file that matched (your global file by name) and the line number. No output almost always means the path mismatch above. And like any ignore file, the global one does nothing for a .DS_Store someone already committed; that needs the git rm --cached fix covered below.

There is also a third, repo-local-but-uncommitted option: .git/info/exclude. It works exactly like .gitignore but lives inside the .git folder and is never committed (the .git/info/exclude file is itself part of the repo's internal layout, the same .git directory whose absence triggers the "not a git repository" error). I reach for it rarely, when I have a scratch file in one clone that I do not want to track and do not want to impose on anyone else.

The number-one gotcha: ignoring a file Git already tracks

This is the question I have answered more times than any other gitignore question, so here it is in full.

You add config.local.php to your .gitignore. You expect it to disappear from git status. It does not. Every change you make to it still shows up. You re-read your pattern, it looks correct, and you start to doubt your sanity.

The pattern is fine. The problem is that .gitignore only affects untracked files. Once a file has been committed, Git is tracking it, and a tracking decision overrides an ignore rule. Git assumes that if a file is already in the repository, you meant to keep it there, so it keeps reporting your edits. (If your goal is simply to discard those local changes instead of committing them, that is a different fix; what follows untracks the file entirely.)

The fix is to stop tracking the file while keeping it on disk:

bash
git rm --cached config.local.php

The --cached flag is the important part: it removes the file from Git's index (it stops being tracked) but leaves the actual file untouched in your working directory. Run git status now and you will see the file staged for deletion and showing up as ignored. Commit the removal:

bash
git commit -m "Stop tracking config.local.php; it is in .gitignore"

From that commit forward, the .gitignore rule takes effect and the file stays out of your way. There is a fuller walkthrough of this exact scenario in removing a file from Git without deleting it locally.

To untrack a whole directory you already committed, add -r:

bash
git rm -r --cached node_modules/

A word of caution for anyone tempted to use this on a secret: untracking a file with git rm --cached removes it going forward, but the file's contents are still sitting in your commit history, recoverable by anyone with the repo. If you accidentally committed an API key or a password, untracking it is not enough. You have to scrub it from history; see how to remove a secret from Git history. And if a public web server is exposing your .git folder, the whole history is downloadable, which is exactly the exposed .git directory attack worth understanding.

Force-checking why a file is (not) ignored

When a pattern is not behaving, do not guess. Ask Git which rule is matching:

bash
git check-ignore -v path/to/file

It prints the exact .gitignore file, line number, and pattern responsible for ignoring that path, or nothing at all if the file is not ignored (in which case the "already tracked" explanation above is your likely culprit). It is the fastest way to debug a stubborn pattern.

Ready-made templates so you do not start from scratch

You almost never need to write a .gitignore by hand. GitHub maintains a large public collection of templates at github/gitignore, one per language and framework: Node.gitignore, Python.gitignore, Java.gitignore, and dozens more. When you create a new repository on GitHub, the "Add .gitignore" dropdown pulls straight from that collection, which is the easiest way to start a fresh project well.

For an existing local project, gitignore.io (now run by Toptal) generates a combined file from any mix of languages, editors, and operating systems you name. Type node, macos, visualstudiocode and it gives you one tidy file covering all three.

A couple of habits that have saved me grief:

  • Add the .gitignore before the first commit. It is far easier than the untracking dance above. If you are starting clean, setting up Git for a new project is the right moment to drop the template in. Adding version control to something that already exists is its own small process, covered in adding Git to an existing project. And if you are working on a project someone else already set up, the .gitignore is usually already there waiting for you when you clone.
  • Ignore the artifacts, commit the recipe. Ignore node_modules/, but commit package-lock.json. Ignore vendor/, but commit composer.lock. The lockfile is how anyone else regenerates the ignored folder exactly.

If your project is WordPress, the ignore strategy is specific enough that I gave it its own treatment in using Git with WordPress: you typically ignore wp-content/uploads/ and core, and track only your themes and plugins.

A sane default .gitignore to copy

Here is a reasonable starting point for a mixed JavaScript and PHP project. Trim it to fit:

text
# Dependencies
node_modules/
vendor/

# Build output
dist/
build/
*.min.js

# Secrets and local config
.env
.env.*
!.env.example
config.local.php

# Logs and caches
*.log
.cache/
.parcel-cache/

# OS and editor files (better in your global ignore)
.DS_Store
Thumbs.db
.idea/
.vscode/

Notice the .env.* plus !.env.example pair: it ignores every environment file except the example template you actually want to commit so teammates know which variables to set. That is negation doing real work.

FAQ

The most common reason is that the file is already tracked by Git. The .gitignore rule only applies to files Git is not yet tracking, so once a file has been committed it keeps showing up. Untrack it with git rm --cached and commit that change, and the ignore rule will take effect. If the file was never committed, run git check-ignore -v on it to see exactly which pattern is or is not matching.

Yes. The per-repository .gitignore is meant to be committed and shared so everyone working on the project ignores the same build output and secrets. Only personal, machine-specific rules like editor or OS files belong in your global ignore instead, which is not committed.

The per-repo .gitignore lives at the root of the repository and is committed, so it travels with the project and applies to everyone. The global ignore is a personal file on your own machine, set with git config core.excludesFile, and is good for things tied to your tools rather than the project, such as editor folders and operating-system junk files.

The most common cause is a path mismatch: you created the file in the current directory instead of your home directory, so core.excludesFile points at a file that does not exist. Run git config --get core.excludesFile to see the configured path, then confirm that exact file holds your patterns. The second most common cause is that the file was already committed, in which case no ignore rule applies until you run git rm --cached.

Both work. Git configuration keys are case-insensitive, so the two spellings refer to the same setting. The official documentation uses core.excludesFile.

Use a negation pattern with an exclamation mark. Ignore the folder contents with a line like logs slash star, then re-include the one file with a line like exclamation logs slash README. The catch is that you cannot re-include a file if its parent directory is itself fully ignored, so ignore the directory contents rather than the directory itself when you want a negation to work.

Once ignore rules are out of the way and only the right files are staged, the next things worth learning are writing good commit messages and branching. Both are short, and both pay off on every project after.

Sources

Authoritative references this article was fact-checked against.

TagsgitignoreGitVersion ControlDevOpsGit Basicsglobal gitignorecore.excludesFile

Found this useful? Pass it on.

Copied

Ishan Karunaratne

Systems and Network Architect · Chief Technology Officer

Systems and network architect and Chief Technology Officer with more than two decades designing, building, and running production software, cloud and network architecture, Linux systems, and the bare metal underneath them, and lately working AI into the stack. A US Army veteran who served in Operation Iraqi Freedom. What I write here is drawn from the full arc of that work, across architecture, engineering, and operations, not any single job.

Keep reading

Related posts

Route WP_Query through Elasticsearch with ElasticPress: setup, ep_integrate, wp-cli index, facets, whether you need it, vs MySQL FULLTEXT, and OpenSearch.

How to Use ElasticPress with WP_Query

Wire ElasticPress to WP_Query so WordPress queries hit Elasticsearch instead of MySQL. Covers installation, indexable post types, ep_integrate, the wp-cli index command, faceted search with aggregations, whether you need ElasticPress at all, how it compares with MySQL FULLTEXT, and why it targets Elasticsearch rather than OpenSearch.

Run find results in parallel with find -print0 | xargs -0 -P N -n 1 cmd. Size -P to your cores and learn when find -exec {} + beats xargs. GNU and BSD.

How to Run find in Parallel with xargs -P

find . -type f -name '*.log' -print0 | xargs -0 -P 4 -n 1 gzip compresses every matched file four at a time. The flags that make it work (-P, -n 1, -0 with -print0), how to size -P, when parallelism helps and when it thrashes the disk, and when plain find -exec {} + beats xargs altogether.