Introduction
When working with Git, ignoring certain files is often necessary to keep a repository clean and avoid committing irrelevant files. Most developers are familiar with the .gitignore file, but did you know there are other ways to ignore files? In this article, we'll explore the three levels of file ignoring in Git: .gitignore, .git/info/exclude, and ~/.config/git/ignore.
The .gitignore File
The .gitignore file is arguably the most well-known way to ignore files in Git. It resides in your repository and contains a list of files and directories you wish to exclude from version control. It's an excellent solution for ignoring files specific to a project. For example, you might ignore local configuration files or files automatically generated by your development environment.
Using .gitignore
To use .gitignore, simply create this file at the root of your repository and add the files or patterns you want to ignore. For example:
``` # Ignore all .log files *.log
# Ignore local configuration file config/local.yml ```
The .git/info/exclude File
This file is less known but equally powerful. Located in the .git directory of each repository, .git/info/exclude works similarly to .gitignore, but it is not tracked by Git. It's ideal for ignoring files specific to your work environment that you don't want to share with other contributors.
Usage Example
Suppose you have a notes.txt file that you don't want to version, but it is specific to a project. You can add it to .git/info/exclude:
`` # Personal notes file notes.txt ``
The Global ~/.config/git/ignore File
To ignore files across all your Git repositories on a machine, use the ~/.config/git/ignore file. This file is perfect for ignoring files or directories you never want to version, regardless of the project, such as system files generated by macOS:
`` # Ignore macOS system files .DS_Store ``
Custom Configuration
You can also customize the global ignore file. For example, if you prefer to use a file named .gitignore_global, configure it with:
``shell git config --global core.excludesFile ~/.gitignore_global ``
Checking Which File is Ignoring a Specific File
To check which ignore file is responsible for ignoring a file, use the following command:
``shell git check-ignore -v <filename> ``
This will show you the exact path and line where the file is being ignored.
Conclusion
Understanding the different levels of file ignoring in Git gives you increased flexibility to manage your projects effectively. Whether you want to ignore files globally, per repository, or project-specific, these tools offer you all the necessary options.
Let's discuss your project in 15 minutes.