Git
Version Control
File Management
Programming
Code Maintenance

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

When working with Git, it is often necessary to ignore certain files from being tracked by the version control system. These could be files containing sensitive information, user-specific IDE settings, or build artifacts that are locally generated and should not be shared with the repository. Ignoring files in Git can be achieved through the use of a .gitignore file or by updating the Git configuration.

Understanding the .gitignore File

A .gitignore file is a text file that tells Git which files or directories to ignore in a project. A .gitignore file should be committed at the root of your repository, although you can have additional .gitignore files in different directories in your repository which will apply only to the files in those directories.

Contents of a .gitignore file:

  • Blank lines can be used for spacing.
  • # marks the line as a comment.
  • Standard glob patterns work, and will be applied recursively throughout the entire working tree.
  • To ignore a specific directory, use a slash / at the end of the directory name.
  • To exclude certain files from an ignored set, you can precede them with an exclamation mark !.

Example of a .gitignore file:

plaintext
1# Ignore all log files
2*.log
3
4# But do not ignore debug log files, even if they have a `.log` extension
5!debug*.log
6
7# Ignore temporary directories
8tmp/
9
10# Ignore all `.txt` files in the doc/ directory only
11doc/*.txt
12
13# Ignore one specific file
14config/settings.json

Git Configuration for Ignoring Files

Aside from the .gitignore file, Git allows you to define ignored files globally on your system or to do so via exclusions in the config file. This is useful when you have certain files you'd like to ignore across all of your projects.

Global .gitignore:

  1. Create a global .gitignore file, for example in your home directory.
  2. Tell Git to use this file as the global gitignore file with the following command:
bash
   git config --global core.excludesfile '~/.gitignore_global'

Excluding files in a repository's config:

  1. Navigate to your Git repository.
  2. Run the following command to add a rule directly to the repository's config file:
bash
   git config --add core.excludesfile '~/repos/myrepo/.git/info/exclude'

Practical Examples and Use Cases

Let’s say you’re developing a Python project and you don’t want to track the __pycache__ directories and *.pyc files created as part of the Python compilation process:

Python .gitignore:

plaintext
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]

Summary Table

FeatureDescription
.gitignore FilePlaced in the Git repo to specify untracked files.
Global .gitignoreA single file for all your repositories, set via Git configuration.
Exclusion ListExclude files only locally for a specific repo by editing the config.
SyntaxUses glob patterns, can negate with !, use / at the end for directories.

Additional Tips

  • Testing .gitignore Rules: You can test whether a file is ignored via Git by using the command git check-ignore -v PATH.
  • Case Sensitivity: Git ignore rules are case-sensitive, which is particularly critical in environments such as Windows.
  • Nested .gitignore Files: If a subdirectory has another .gitignore file, its rules only apply there and add upon rules set in higher directory levels.

Using .gitignore correctly is important for maintaining the cleanliness of your repository by not including unnecessary files that can clutter the project and potentially cause conflicts between contributor environments.


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.