- 1. Introduction to .gitignore
- 2. .gitignore comments
- 3. Ignoring at the beginning or end of /
- 4. glob pattern matching ignore
- 5. .gitignore global ignore
- 6. Ignore content that has been committed to the remote repository
- 7. Use ignore rules under various frameworks
1. Introduction to gitignore
The function of the .gitignore file is to tell git which files do not need to be added to version management (definition ignores submitted files)
The .gitignore file is used to ignore the changes of the specified file or folder. The file or folder recorded in the .gitignore file cannot be tracked by git. In other words, the ignored file will not be placed into the remote repository.
In other words, if the file already exists in the remote warehouse, it cannot be ignored by the .gitignore file
2. .gitignore comments
All blank lines or lines starting with the comment symbol # will be ignored by git
Lines beginning with a pound sign (#) are comments and will be ignored, blank lines can be used to improve the readability of the file and to group related modelines
# 忽略编辑器配置目录
/.idea
/.vscode
3. Ignoring at the beginning or end of /
I saw this sentence in an article before: Starting with a slash /
means ignoring the directory
It was later found that this is not the case. Its real function is to ignore only the content of the same level, but not the content of the lower-level directory.
Conclusion: The role of / at the head is to ignore only the contents of the current directory; the role of putting it at the end is to ignore only the directory, not the file
neither head nor tail /
# 忽略当前目录及下级目录中所有的 runtime
# runtime 文件或目录都会被忽略
runtime
preceded by /
# 只忽略当前目录中的 runtime 文件或目录
# 不忽略下级目录中的 runtime 文件或目录
/runtime
followed by /
# 只忽略当前目录和下级目录中的 runtime 目录
# 不忽略当前目录和下级目录中的 runtime 文件
runtime/
Both head and tail /
# 只忽略当前目录中的 runtime 目录
/runtime/
4. glob pattern matching ignore
Standard glob pattern matching can be used in .gitignore files
*
Wildcard multiple characters with an asterisk
# 忽略 vendor 目录下的所有文件
/vendor/*
# 忽略所有后缀名为 txt 的文件
*.txt
?
wildcard a single character with a question mark
# 忽略文件名称为一个字符, 后缀名为 php 的文件
?.php
[]
A matching list of single characters enclosed in square brackets
# 忽略 125.php、135.php文件
1[23]5.php
Use an exclamation point !
to indicate that no matching files or directories are ignored (tracked)
[Note] Note that the folder to be ignored must end at the end /*
, otherwise the rule will not take effect if it is not ignored
# 忽略vendor目录下的所有文件
/vendor/*
# 不忽略vendor目录下的 1.php
# 在已忽略的文件夹中不忽略指定文件
!/vendor/1.php
# 不忽略vendor目录下 dev目录
# 在已忽略的文件夹中不忽略指定文件夹
!/vendor/dev
5. .gitignore global ignore
git allows the creation of global .gitignore files, and all local git repositories will obey the global ignore rules.
The name and location of the file are not required, as long as the path is specified correctly in the git configuration file.
For example, to ~/.gitignore_global
set global git ignore files, you can do:
# 创建文件
touch ~/.gitignore_global
# 将文件添加到 git 配置
git config --global core.excludesfile ~/.gitignore_global
6. Ignore content that has been committed to the remote repository
Delete files or directories in the staging area
git rm --cached <file>
git rm -r --cached <folder>
Add ignore configuration in .gitignore
<file>
/<folder>
Push to remote warehouse
git add .gitignore
git commit -m '忽略文件'
git push origin master
7. Use ignore rules under various frameworks
Ignore editor configuration files
.idea
.vscode
.hbuilderx
ignore special files
.DS_Store files generally appear in MacOS, and are used by Finder to store the display properties of folders, such as: the placement of file icons
.DS_Store
ThinkPHP 5.0
/runtime
/vendor
/thinkphp
Uni-App project
unpackage