Git Tutorial
Git and GitHub
Git Contribute
Git Advanced
Git Undo
Git Ignore and .gitignore
Git Ignore
When sharing of the code is done, there are certain files or parts of the project that we don’t wish to share.
Examples
- log files
- temporary files
- hidden files
- personal files
Git can specify the files or parts of the project that must be ignored by Git using a .gitignore file.
Git will not track files and folders specified in .gitignore. However, the .gitignore file itself IS tracked by Git.
Create .gitignore
In order to create a .gitignore file, go to the root of the local Git, and create it:
Example
[user@localhost]$ touch .gitignore
Open the file with a text editor.
Now, we’ll add two simple rules:
- Any files with the .log extension should be ignored.
- Any directory named temp should be ignored.
# ignore ALL .log files
*.log
# ignore ALL files in ANY directory named temp
temp/
Now all .log files and anything in temp folders will be ignored by Git.
Pattern | Explanation/Matches | Examples |
| Blank lines are ignored |
|
# text comment | Lines starting with # are ignored |
|
name | All name files, name folders, and files and folders in any name folder | /name.log |
name/ | Ending with / specifies the pattern is for a folder. Matches all files and folders in any name folder | /name/file.txt |
name.file | All files with the name.file | /name.file |
/name.file | Starting with / specifies the pattern matches only files in the root folder | /name.file |
lib/name.file | Patterns specifiing files in specific folders are always realative to root (even if you do not start with / ) | /lib/name.file |
**/lib/name.file | Starting with ** before / specifies that it matches any folder in the repository. Not just on root. | /lib/name.file |
**/name | All name folders, and files and folders in 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 |
*.file | All files withe .file extention | /name.file |
*name/ | All folders ending with name | /lastname/log.file |
name?.file | ? matches a single non-specific character | /names.file |
name[a-z].file | [range] matches a single character in the specified range (in this case a character in the range of a-z, and also be numberic.) | /names.file |
name[abc].file | [set] matches a single character in the specified set of characters (in this case either a, b, or c) | /namea.file |
name[!abc].file | [!set] matches a single character, except the ones specified characters (in this case a, b, or c) | /names.file |
*.file | All files withe .file extention | /name.file |
name/ | ! specifies a negation or exception. | /name/file.txt |
*.file | ! specifies a negation or exception. | /log.file |
*.file | Adding new patterns after a negation will re-ignore a previous negated file. name folder. Unless the file name is junk | /log.file |
Local and Personal Git Ignore Rules
We can also ignore files or folders but not show them in the distributed .gitignore file.
These kinds of ignoring are specified in the .git/info/exclude file. It works a similar way as .gitignore and is not known to anyone else