Gitignore
The file of patterns telling Git which files to leave untracked, such as build output, dependencies, editor clutter, and local secrets, along with the traps of files that were already tracked and secrets already committed.
A gitignore file lists patterns for files that Git should leave untracked. Anything that matches won't show up as new in status listings and won't be added by a blanket add command. Every project has files that don't belong in version control: compiled output, downloaded dependencies, logs, caches, editor settings, operating system clutter, and local configuration holding passwords.
The file is named .gitignore and usually sits at the root of the repository, though any directory can have one that applies beneath it. Patterns use Glob syntax:
- A plain name such as a log file name matches it in any directory.
- A trailing slash matches only directories, so a dependencies folder and everything inside it can be ignored in one line.
- A leading slash anchors the pattern to the directory containing the ignore file.
- Two asterisks match any number of nested directories.
- An exclamation mark re-includes something an earlier pattern excluded, though not if a parent directory was already ignored.
There are three layers. The committed .gitignore is shared with everyone. A file inside the repository's own metadata directory holds private patterns for one checkout. A global ignore file, set in the user's configuration, covers personal habits like editor swap files across every project, so each project's file doesn't have to list every tool its contributors might use; see Dotfiles.
The common traps:
- Ignoring doesn't untrack. A file already committed stays tracked even after it is added to the ignore list. It has to be removed from the index first, while leaving the copy on disk.
- Ignoring doesn't erase history. A password committed once and ignored afterwards is still in every clone. The only real fix is to revoke and replace the credential; see Secret Management and Environment Variable.
- Some generated files belong in the repository. A Lockfile is generated, but committing it is what makes installs repeatable.
- Other tools borrow the format with their own rules. Container builds and package publishing use similar files. Without its own ignore file, npm falls back to the Git one, which can leave out built files a package needs; see Artifact Repository.
Large binary assets are a different problem. Ignoring them keeps them out of history, but if the project needs them, they belong in a system built for large files; see Git LFS.
GitHub publishes starter ignore files for common languages, which many projects copy. Ignore patterns also speed up tools beyond Git: search utilities, file watchers, and editors commonly honor them to skip dependency and build directories.
A careful ignore file keeps commits focused on the change at hand and keeps a pre-commit Git Hook from tripping over generated files.
See also2
Commit
A recorded snapshot of a project with a message explaining it.
Version Control & Delivery23 connections
Line Ending
The invisible characters marking where a line of text ends, split three ways since the teletype era between Unix, Windows, and classic Mac OS, and still breaking scripts and diffs.
Systems & Tooling9 connections
Linked from7
- Environment VariableSystems & Tooling
A named value in a process's environment, inherited by its children — the standard configuration channel.
- Git LFSVersion Control & Delivery
A Git extension that stores large binary files on a separate server and keeps small pointer files in the repository, so history stays light enough to clone.
- Git WorktreeVersion Control & Delivery
An additional working directory attached to one repository, each on its own branch.
- GlobSystems & Tooling
Wildcard pattern matching over filenames, similar enough to regular expressions to mislead.
- LockfileVersion Control & Delivery
A generated file recording the exact dependency versions an install resolved to.
- Secret ManagementSystems & Tooling
Storing, distributing, and rotating credentials without embedding them in code or history.
- Version ControlVersion Control & Delivery
A system recording the history of a project's files, enabling branching, merging, and recovery.