Ignoring Files in Git
Projects often contain files that we don't want to store in the Git system. These may include temporary files generated by your code, cache files, or password files.
We can force Git to ignore such folders and files. This is done with a special file .gitignore, placed in your project folder.
In this file, on each line you need to write the path to the file or folder that you want to ignore. Let's look at examples of what syntax is allowed here.
Example
A slash before a file name matches a file in the root of the repository:
/debug.log
For example, the following files will be ignored:
- debug.log
But not:
- logs/debug.log
Example
Without a leading slash, files in subfolders will also be ignored:
debug.log
For example, the following files will be ignored:
- debug.log
- logs/debug.log
Example
Without a slash at the end, both files without an extension and folders with this name will be ignored:
logs
For example, the following files will be ignored:
- logs
- logs/debug.log
- logs/latest/foo.bar
- build/logs
- build/logs/debug.txt
Example
Only folders with a trailing slash will be ignored:
logs/
For example, the following folders will be ignored:
- logs/
- logs/debug.log
- logs/latest/foo.bar
- build/logs
- build/logs/debug.txt
Example
Two asterisks at the beginning of a folder will cause all folders containing such a subfolder in the path to be ignored:
**/logs
For example, the following files will be ignored:
- logs/debug.log
- logs/monday/foo.bar
- build/logs/debug.log
Example
Two asterisks can also be used to match files based on their name and the name of their parent folder:
**/logs/debug.log
For example, the following files will be ignored:
- logs/debug.log
- build/logs/debug.log
But not:
- logs/build/debug.log
Example
One star — is a wildcard that can match any or all characters:
*.log
For example, the following files will be ignored:
- debug.log
- foo.log
- .log
- logs/debug.log
Example
Adding an exclamation point to the beginning of a pattern cancels the pattern's effect. If a file matches a pattern, but also matches the overriding pattern specified after it, the file will not be ignored:
*.log
!important.log
For example, the following files will be ignored:
- important.log
But not:
- file.log
Example
The question mark corresponds to exactly one character:
debug?.log
For example, the following files will be ignored:
- debug0.log
- debugg.log
But not:
- debug10.log
Example
Square brackets match one character from the specified set:
debug[01].log
For example, the following files will be ignored:
- debug0.log
- debug1.log
But not:
- debug2.log
- debug01.log
Example
Square brackets can also be used to specify a match for a single character from a given range:
debug[0-9].log
For example, the following files will be ignored:
- debug0.log
- debug1.log
But not:
- debug10.log
Example
The exclamation mark can be used to match any character except those in the specified set:
debug[!01].log
For example, the following files will be ignored:
- debug2.log
But not:
- debug0.log
- debug1.log
- debug01.log
Example
Ranges can be numeric or alphabetic:
debug[a-z].log
For example, the following files will be ignored:
- debuga.log
- debugb.log
But not:
- debug1.log
Example
Two asterisks correspond to multiple directories or none:
logs/**/debug.log
For example, the following files will be ignored:
- logs/debug.log
- logs/monday/debug.log
- logs/monday/pm/debug.log
Example
Wildcards can also be used in directory names:
logs/*day/debug.log
For example, the following files will be ignored:
- logs/monday/debug.log
- logs/tuesday/debug.log
But not:
- logs/latest/debug.log
Practical tasks
Make it so that files from the cache folder are ignored.
Make it so that files from the cache and tmp folders are ignored.
Make it ignore the file err.txt and the folder tmp.