In the process of using Git, we like to have files such as logs, temporary files, compiled intermediate files, etc. not submitted to the code repository, so we need to set corresponding ignore rules to ignore the commit of these files.
Git ignores the method of file commits
There are three ways to ignore files you don't want to commit in Git.
Define the .gitignore file in your Git project
This is managed by defining a .gitignore file in a folder in the project, where the corresponding ignore rules are definedCurrent folderThe Git commit behavior of the file under the .gitignore files can be submitted to public repositories, which means that all developers under the project share a defined set of ignore rules. In the .gitingore file, specify a ignore rule on each line, following the corresponding syntax. For example:
Specify the exclude files in the settings of the Git project
This is just a temporary specification of the project's behavior, which requires editing the .git/info/exclude file under the current project, and then writing the file to which the commit needs to be ignored. Note that the root directory of the ignored files specified in this way is the project root.
A .gitignore file that defines the Git global
In addition to being able to define .gitignore files in your project, you can also set up a global git .gitignore file to manage the behavior of all Git projects. This approach is not shared between different project developers, and is a behavior at the Git application level on top of the project.
This also requires creating a corresponding .gitignore file, which can be placed anywhere. Then configure Git with the following command:
Git ignores rules
For detailed ignoring rules, please refer to the official English documentation at the address:The hyperlink login is visible.
Git ignores rule priority
In the .gitingore file, specify an ignore rule for each line, and Git checks for multiple sources when ignoring rules, with the following priority (from highest to lowest):
- Read the available ignore rules from the command line
- The rules defined by the current directory
- The rules defined by the parent directory are recursive
- $GIT_DIR/info/exclude file
- core.excludesfile
Git ignores rule matching syntax
In the .gitignore file, the syntax for the ignore rule for each line is as follows:
- Spaces that do not match arbitrary files can be used as separators and can be escaped with backslashes
- # The file identification comment at the beginning can be escaped using a backslash
- ! The schema identification at the beginning is negative, and the file will be included again, if the file's parent directory is excluded, use ! will not be included again. Escape can be done using a backslash
- / 结束的模式只匹配文件夹以及在该文件夹路径下的内容,但是不匹配该文件
- / 开始的模式匹配项目跟目录
- If a pattern does not contain a slash, it matches the contents relative to the current .gitignore file path, and if the pattern is not in the .gitignore file, relative to the project root
- ** Matching multi-level catalogs can be used at the beginning, middle, and end
- ? Universal matching of individual characters
- [ ] Universal match list of individual characters
Common matching examples:
- bin/: ignores the bin folder under the current path, everything under that folder is ignored, not the bin file
- /bin: 忽略根目录下的bin文件
- /*.c: 忽略 cat.c,不忽略 build/cat.c
- debug/*.obj: Ignore debug/io.obj, do not ignore debug/common/io.obj and tools/debug/io.obj
- **/foo: ignore /foo, a/foo, a/b/foo, etc
- a/**/b: ignore a/b, a/x/b, a/x/y/b, etc
- !/bin/run.sh: Do not ignore run.sh files in the bin directory
- *.log: Ignore all .log files
- config.php: 忽略当前路径的 config.php 文件
.gitignore rule does not take effect
.gitignore can only ignore files that are not tracked originally, and modifying .gitignore is ineffective if some files have already been included in versioning.
The workaround is to delete the local cache (change it to untracked state) before committing:
(End)
|