.gitignore file writing
.gitignore
The file is used to specify which files or directories should be ignored by Git and not included in version control. In .gitignore
, you can use the following syntax:
1 Empty line: Empty lines are ignored.
2 Comments: #
Lines starting with are considered comments and will be ignored by Git.
3 file and directory pattern matching:
-
Use slashes
/
for directories. -
Use an asterisk
*
for wildcards, matching zero or more characters. -
Use double asterisks
**
for recursive wildcards, matching zero or more directories. -
Use a question
?
mark to match any character. -
Use square brackets
[]
to indicate a character set, and match any character in the square brackets. -
Use an exclamation point
!
to indicate negation, that is, do not ignore the specified file or directory.
4 examples:
-
Ignore specified files:
.gitignore
Enter a filename or relative path in . -
myfile.txt directory/file.txt path/to/myfile.txt
-
Ignore files with specific extensions:
-
*.log *.csv
-
Ignore specific directories:
-
mydirectory/ path/to/mydirectory/
-
Ignore files with the specified pattern:
-
secret* /build/
-
Exclude specific files or directories:
-
!important.txt !path/to/important/file.txt
-
Use double asterisks for recursive matching:
-
logs/**/*.log
-
Exclude all files:
-
/* !/.gitignore
-
Ignore all files with extension
.txt
: -
*.txt
-
Ignore files
.bak
with that extension, but not.important.bak
those ending with: -
*.bak !*.important.bak
Note: `.gitignore` files can only ignore files that are not yet tracked by Git. If a file has been included in version control, it needs to be removed from Git to make `.gitignore` take effect. You can use the `git rm --cached <filename>`command to remove the file.