Ignoring Files in Git
What
A .gitignore file is a special plain-text configuration file located at your project’s root (or subdirectories) that explicitly instructs Git to ignore specific files, directories, or pattern matches, keeping them untracked.
How
To ignore files, create a file literally named .gitignore at the root of your project directory and add paths to the files or directories you want Git to bypass:
# Example .gitignore contents:
node_modules/
.env
secure/
With the config above:
- Any file under a folder named
node_modules/will be ignored. - The
.envfile (containing sensitive local settings) will be ignored. - The entire
secure/folder and all its child files will remain untracked.
When you run command queries like git status, ignored files will not appear under the list of untracked files, preventing you from adding them via git add ..
Why
Many files inside a project directory are local artifacts or security risks. For example, API keys, database credentials, third-party package dependencies (like node_modules), or heavy compiled binaries should never be uploaded to remote repositories. Using a .gitignore protects your project by ensuring sensitive and redundant files are safely filtered out, maintaining a clean and professional repository.
Summary
Create a plain-text .gitignore file at your repository’s root and list target files or directories to permanently exclude them from Git tracking.