Ctrl + K
Git8 min read

How .gitignore Works

Understand .gitignore syntax, ignore patterns, tracked files, exceptions and best practices for keeping Git repositories clean.

Published: 2026-08-07

A .gitignore file tells Git which files and directories should be ignored when checking for untracked files. It helps keep repositories clean by excluding generated files, build artifacts, operating system files, dependencies and sensitive local configuration that should not be committed to version control.

Almost every Git project includes a .gitignore file because ignoring unnecessary files reduces repository clutter and prevents accidental commits of temporary or machine-specific content.

What Is .gitignore?

The .gitignore file contains a list of patterns. Whenever Git scans the working directory for untracked files, it compares file paths against these patterns to determine whether they should appear as candidates for staging.

Why Use .gitignore?

Many files are generated automatically during development and do not belong in version control. Ignoring them keeps repositories smaller, cleaner and easier to maintain.

  • Ignore build output.
  • Exclude dependency folders.
  • Hide operating system files.
  • Avoid committing local configuration.
  • Reduce repository noise.

Basic Example

node_modules/
dist/
build/
.env
*.log

How Git Uses .gitignore

When Git looks for untracked files, it checks each path against the ignore rules. Matching files are skipped and do not appear in commands such as 'git status' unless explicitly requested.

Working Directory
        ↓
Git scans files
        ↓
Apply .gitignore rules
        ↓
Ignored files skipped
        ↓
Remaining files shown by git status

Common Ignore Patterns

PatternMeaning
*.logIgnore all log files
node_modules/Ignore dependency directory
dist/Ignore build output
.envIgnore environment file
*.tmpIgnore temporary files

Ignoring Directories

Adding a trailing slash indicates that the rule targets directories. Everything inside the ignored directory is also ignored unless an exception rule explicitly includes specific files.

Wildcard Patterns

Wildcards make ignore rules flexible by matching multiple files that share similar names or extensions.

PatternMatches
*.logAll log files
*.tmpAll temporary files
cache-*Names beginning with cache-
images/*.pngPNG files inside images
💡 Keep your .gitignore file organized by grouping related rules together, such as dependencies, build output, logs and operating system files.
⚠️ Adding a file to .gitignore does not automatically remove it from Git if it has already been committed. Tracked files remain tracked until they are explicitly removed from the Git index.

Ignoring Files vs Tracked Files

A common misunderstanding is that adding a file to .gitignore will stop Git from tracking it. In reality, .gitignore only affects untracked files. Once a file has been committed, Git continues tracking changes until it is removed from the index.

SituationEffect of .gitignore
Untracked fileIgnored if it matches a rule
Tracked fileContinues to be tracked

Removing a Tracked File

If a file was committed before being added to .gitignore, remove it from the Git index while keeping the local copy. After that, the ignore rule will prevent future tracking.

git rm --cached filename

git commit -m "Stop tracking generated file"

Negation Rules

An exclamation mark (!) creates an exception to a previous ignore rule. This allows specific files to be tracked even when their parent directory or matching pattern is ignored.

logs/
!logs/.gitkeep

Global Git Ignore

Git also supports a global ignore file that applies to every repository on your computer. It is useful for ignoring operating system files, editor settings and other machine-specific files that should never be committed.

Project-Specific vs Global Ignore

Project .gitignoreGlobal Ignore
Shared with the repositoryApplies only to your local machine
Committed to GitStored outside the repository
Project-specific rulesPersonal development environment rules

Common Files to Ignore

Although every project is different, certain files and directories are commonly excluded because they can be regenerated automatically or contain local configuration.

  • Dependency directories (for example, node_modules).
  • Compiled binaries and build artifacts.
  • Log files.
  • Temporary files.
  • Environment configuration files containing secrets.
  • Operating system metadata files.

How .gitignore Improves Collaboration

A well-maintained .gitignore file ensures that every contributor works with the same repository structure. By excluding unnecessary files, pull requests become cleaner, merge conflicts are reduced and repositories remain easier to manage.

💡 Review your .gitignore whenever you add new tools, frameworks or build systems. Updating ignore rules early prevents unnecessary files from entering version control.
⚠️ Do not rely on .gitignore to protect sensitive information that has already been committed. Once secrets are pushed to a repository, removing them requires rewriting history or rotating the exposed credentials.

Common Mistakes

A poorly maintained .gitignore file can lead to unnecessary files being committed, larger repositories and accidental exposure of sensitive information. Understanding how Git processes ignore rules helps avoid these common problems.

  • Assuming .gitignore affects files that are already tracked.
  • Forgetting to ignore environment files containing secrets.
  • Ignoring important project files by using patterns that are too broad.
  • Keeping outdated ignore rules after changing frameworks or build tools.
  • Duplicating the same ignore rules multiple times.
  • Storing personal editor settings in the project's shared .gitignore instead of using a global ignore file.

Best Practices

  • Create a .gitignore file at the beginning of every project.
  • Ignore generated files rather than source files.
  • Use a global Git ignore file for operating system and editor-specific files.
  • Review ignore rules whenever project dependencies or build processes change.
  • Keep the file organized by grouping related patterns together.
  • Verify that sensitive configuration files are excluded before making commits.
💡 Treat your .gitignore file as part of the project's documentation. A clear and well-organized set of ignore rules helps every contributor understand which files belong in version control and which should remain local.
⚠️ Ignoring a file does not secure it. If sensitive information such as API keys or passwords has already been committed, removing the file from .gitignore is not enough—you should remove the secret from the repository history when appropriate and rotate the exposed credentials.

Frequently Asked Questions

What does .gitignore do?

.gitignore tells Git which untracked files and directories should be ignored when scanning the working directory. Ignored files do not appear in commands such as git status unless explicitly requested.

Why is Git still tracking a file after I added it to .gitignore?

Because .gitignore only affects untracked files. If a file has already been committed, Git continues tracking it until it is removed from the index using commands such as git rm --cached.

Can I ignore an entire directory?

Yes. Adding a trailing slash to a pattern, such as build/ or node_modules/, tells Git to ignore the directory and its contents.

What is the difference between a project .gitignore and a global Git ignore file?

A project .gitignore is shared with everyone through the repository, while a global ignore file applies only to your local machine and is typically used for editor settings or operating system files.

Should I commit my .gitignore file?

Yes. The project's .gitignore should normally be committed so every contributor uses the same ignore rules and repository structure.

Helpful Git Tools

A Gitignore Builder generates .gitignore files for popular languages, frameworks and development environments, a Docker Ignore Generator creates optimized .dockerignore files for container builds, an .editorconfig Generator produces consistent editor settings for team members, a Git Branch Name Generator helps create standardized branch names, and a Git Commit Generator assists with writing clear and descriptive commit messages.

Conclusion

The .gitignore file is an essential part of almost every Git repository. By excluding generated files, temporary data, dependencies and machine-specific configuration, it keeps repositories clean and focused on source code. Understanding how ignore patterns work, recognizing the difference between tracked and untracked files and maintaining an up-to-date .gitignore helps development teams collaborate more effectively while reducing unnecessary commits and repository clutter.