When sharing your code, there are often files or parts of your project you might not want to include, such as:
Git allows you to specify which files or parts of your project should be ignored by using a .gitignore file.
Git will not track the files and folders listed in .gitignore, but the .gitignore file itself is tracked by Git.
To create a .gitignore file, navigate to the root directory of your local Git repository and create the file there.
[user@localhost] $ |
touch .gitignore |
Now, open the file in a text editor.
We’ll add two simple rules:
# ignore ALL .log files *.log # ignore ALL files in ANY directory named temp temp/ |
Now, all .log files and anything within temp folders will be ignored by Git.
Note: In this case, a single .gitignore file applies to the entire repository. However, you can also have additional .gitignore files in subdirectories, which apply only to files or folders within those specific directories. |
Here are the general rules for pattern matching in .gitignore files:
Pattern |
Explanation/Matches |
Examples |
|
Blank lines are disregarded. |
|
# text comment |
Lines beginning with |
|
name |
All files named name, all folders named name, and all files and folders within any folder named name. |
/name.log |
name/ |
A pattern ending with / specifies that it applies to a folder. It matches all files and folders within any folder named name. |
/name/file.txt no match: |
name.file |
All files with the name |
/name.file |
/name.file |
A pattern starting with |
/name.file no match: |
lib/name.file |
Patterns specifying files in specific folders are always relative to the root directory, even if they don’t start with |
/lib/name.file no match: |
**/lib/name.file |
Starting with **/ before a / specifies that the pattern matches any folder in the repository, not just those at the root. |
/lib/name.file |
**/name |
All folders named name, and all files and folders within any name folder. |
/name/log.file |
/lib/**/name |
All name folders, and files and folders in any name folder within the lib folder. |
/lib/name/log.file no match: |
*.file |
All files with the .file extension. |
/name.file |
*name/ |
All folders ending with name. |
/lastname/log.file |
name?.file |
? matches a single unspecified character. |
/names.file no match: |
name[a-z].file |
[range] matches a single character within the specified range (for example, any character from a to z, or any numeric character). |
/names.file no match: |
name[abc].file |
[set] matches a single character from the specified set of characters (for example, a, b, or c). |
/namea.file no match: |
name[!abc].file |
[!set] matches a single character except those specified in the set (for example, any character except a, b, or c). |
/names.file no match: |
*.file |
All files with the .file extension. |
/name.file |
name/ |
! specifies a negation or exception. It matches all files and folders in any name folder, except for name/secret.log. |
/name/file.txt no match: |
*.file |
! specifies a negation or exception. It matches all files with the .file extension, except for name.file. |
/log.file no match: |
*.file |
Adding new patterns after a negation will override a previously negated file. For example, all files with the .file extension are ignored, except for those in the name folder, unless the file is named junk. |
/log.file no match: |
You can also ignore files or folders without including them in the distributed .gitignore file.
These ignores are specified in the .git/info/exclude file, which functions like .gitignore but is not visible to others.