Git
Gitignore
Version Control
File Management
Software Development

How to ignore certain files in Git

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Git is a widely used version control system that helps developers manage and track changes in their codebases. One common need when working with Git is to ignore certain files either because they are not relevant to the source code (like build artifacts or temporary files) or are too personal to be shared (like configuration files with sensitive data). Ignoring files in Git is a crucial practice to keep your repository clean and your project efficient.

Understanding .gitignore

The .gitignore file is a text file that tells Git which files or directories to ignore in a project. Each line in a .gitignore file specifies a pattern. Git will ignore files and directories matching these patterns.

Creating a .gitignore File

To create a .gitignore file, follow these steps:

  1. Navigate to the root directory of your Git repository.
  2. Create a file named .gitignore.
  3. Open the .gitignore file with a text editor.
  4. Add patterns of files or directories you want to ignore.

Pattern Syntax

In a .gitignore file, you can use various patterns to specify which files or directories to ignore. Here are some key patterns:

  • Ignoring a Specific File: To ignore a specific file, write its name. Example:
 
  secret_config.yml
  • Ignoring All Files with a Specific Extension: Use an asterisk * wildcard. Example:
 
  *.log
  • Ignoring a Specific Directory: Append a slash / to the directory's name. Example:
 
  /node_modules/
  • Ignoring All Instances of a Directory: Example:
 
  build/
  • Excluding a Specific File Pattern: Patterns prefixed with an exclamation mark ! are exceptions to broad patterns. Example:
 
  !important.log
  • Ignoring all files and directories but not subdirectory files: Example:
 
  temp/*
  !temp/config.json

Example of a .gitignore File

plaintext
1# Ignore node_modules directory
2/node_modules/
3
4# Ignore all log files
5*.log
6
7# Ignore personal VSCode settings
8.vscode/
9
10# Ignore all .env files
11*.env
12
13# Do not ignore the security.log file
14!security.log

Global .gitignore

Sometimes, you want to have a common list of ignored files across all projects on your machine, such as OS files or editor-specific files. This can be done using a global .gitignore file.

Setting Up a Global .gitignore

  1. Create a global .gitignore file:
bash
   touch ~/.gitignore_global
  1. Tell Git to use the global .gitignore file:
bash
   git config --global core.excludesfile ~/.gitignore_global
  1. Add patterns to the global .gitignore file.

Common Patterns for Global .gitignore

plaintext
1# macOS specific files
2.DS_Store
3.AppleDouble
4
5# Windows specific files
6Thumbs.db
7ehthumbs.db
8
9# Editor specific settings
10.idea/
11.vscode/

Ignoring Tracked Files

If a file is already tracked by Git, updating the .gitignore file will not untrack it. You need to stop tracking a file using:

  1. Remove the file from the index:
bash
   git rm --cached filename
  1. Add the file to .gitignore.
  2. Commit the change:
bash
   git commit -m "Untrack filename and add to .gitignore"

Common Mistakes

  • Incorrect Path Reference: Ensure the paths in .gitignore are relative to the directory where it is located or is referred globally.
  • Git Still Tracking Ignored Files: If files have already been committed before being added to .gitignore, they will remain tracked until explicitly removed.

Summary Table

ItemDescription
.gitignore fileA text file used to specify files Git should ignore in the project.
* pattern symbolsWildcards such as * are used for pattern matching.
ExceptionsPrefixed with ! to include files that might otherwise be ignored by a wider pattern.
Global .gitignoreA system-wide .gitignore used across multiple projects.
Removing tracked filesUse git rm --cached to stop tracking files already tracked by Git.
Patterns for specific exclusionsPatterns are applied to match files/directories. Ex: .log files or OS-specific files.
Common pitfallsPaths errors or trying to ignore files that are already pushed.

Understanding and utilizing the .gitignore file effectively can simplify project management, prevent accidental commits of private configurations, and streamline repository organization. By strategically ignoring unnecessary files, you maintain clearer and more efficient repositories.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.