Several ways for Git to ignore files, and the ignore rules for .gitignore files

.gitignore file

In the process of using Git, we like to not submit some files such as logs, temporary files, and compiled intermediate files to the code warehouse. At this time, we need to set corresponding ignore rules to ignore the submission of these files.

Example of how the rule works:

  • /mtk: filter the entire folder;
  • *.zip: filter all .zip files;
  • /mtk/do.c: filter a specific file;
  • !/mtk/one.txt: Track (do not filter) a specific file.

Notice:If you push a file before creating the .gitignore file, even if you write a rule to filter the file in the .gitignore file, the rule will not work, and git will still perform version management on the file

Configuration syntax:

  • Start with a slash " / " to indicate a directory;
  • Use an asterisk " * " to wildcard multiple characters;
  • A single character is wildcarded with a question mark " ? ";
  • A list of matches enclosing single characters in square brackets " [ ] ";
  • Use an exclamation point " ! " to indicate that the matched files or directories are not ignored (tracked).

Notice:git matches the rules from top to bottom for the .gitignore configuration file

Define Git's global .gitignore file

In addition to defining **.gitignore files in the project , you can also set a global git.gitignore file** to manage the behavior of all Git projects. This method is not shared between different project developers, and is a Git application-level behavior above the project.
This method also needs to create a corresponding .gitignore file, which can be placed in the C:/Users/username/directory. Then configure Git with the following command:

git config --global core.excludesfile ~/.gitignore

Global .gitingore file for personal use:

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
modules.xml

target/

**/.idea
**/*.iws
**/*.iml
**/*.ipr
**/modules.xml
**/mvnw
**/mvnw.cmd
**/.mvn
**/target/
**/.gitignore

### Maven ###
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties
.mvn/wrapper/maven-wrapper.jar

### Java ###
# Compiled class file
*.class

Git ignore rules

For detailed ignore rules, please refer to the official English document .

Git ignore priority of rules

In the .gitingore file, each line specifies an ignore rule. When Git checks the ignore rule, there are multiple sources. Its priority is as follows (from high to low):

  1. Read available ignore rules from the command line;
  2. The rules defined by the current directory;
  3. The rules defined by the parent directory are deduced in turn;
  4. Rules defined in the $GIT_DIR/info/exclude file;
  5. Global rules defined in core.excludesfile.

.gitignore file ignore rules

  1. The / at the beginning does not identify the folder. To indicate that the folder is only ignored, you need to add / after the name, not before.
  2. If you want to ignore a certain folder, some files under it cannot be ignored. You need to add the wildcard *, and then add ! Beginning with a rule, to indicate files or folders not to be ignored.
  3. As long as the path is written, that is, there are characters on the left and right sides of /, then it refers to the "absolute path" (relative to the warehouse, the directory where the warehouse.git folder is located is the root directory), but you can use * to specify the level and which level A folder under a subdirectory.
  4. A space does not match any file and can be used as a separator, which can be escaped with a backslash.
  5. The mode identification comment at the beginning of # can be escaped with a backslash.
  6. If the mode identifier at the beginning of ! is negated, the file will be included again. If the parent directory of the file is excluded, use ! will not be included again . Can be escaped with a backslash.
  7. A pattern ending in / only matches folders and the contents of that folder path, but not the file.
  8. The pattern that starts with / matches projects and directories.
    If a pattern contains no slashes, it matches something relative to the path of the current .gitignore file, or relative to the project root if the pattern is not in a .gitignore file.
  9. ** Match multi-level directories, which can be at the beginning, middle, and end.
  10. ? Generic matches a single character.
  11. [ ] Generic matches a single list of characters.

Examples of common matches:

  1. bin/ : Ignore the bin folder in the current path, all content in this folder will be ignored, and the bin file will not be ignored;
  2. /bin : Ignore the bin file in the root directory;
  3. /*.c : Ignore cat.c, not build/cat.c;
  4. debug/*.obj : ignore debug/io.obj, not ignore debug/common/io.obj and tools/debug/io.obj;
  5. **/foo : ignore /foo, a/foo, a/b/foo, etc.;
  6. a/**/b : a/b, a/x/b, a/x/y/b, etc.;
  7. !/bin/run.sh : Do not ignore the run.sh file in the bin directory;
  8. *.log : ignore all .log files;
  9. config.php : ignore the config.php file in the current path.

Some common settings at the beginning of the .gitignore file:

# 忽略掉所有文件
*
# 取消忽略所有带有后缀名的文件
!*.*
# 取消忽略所有目录
!*/

About the problem that the .gitignore rule does not take effect

.gitignore can only ignore those files that were not originally tracked. If some files have been included in version management, modifying .gitignore is invalid.
The solution is to delete the local cache first (change it to an untracked state), and then submit:

git rm -r --cached .
git add .
git commit -m 'update .gitignore'

Do not ignore files without extensions

Search for files without a suffix in your computer

First of all, let me introduce how to observe and search for files without suffixes on the computer. Directly on the picture:
insert image description here
insert image description here
insert image description here
It is to directly search for the file type.

Do not ignore names without suffix

as follows:

!/**/SCSDKCoreKit
!/**/SCSDKCreativeKit
!/**/SCSDKLoginKit

Just don't ignore all the files with changed file names.

Ignore a file that has been hosted to git to prevent secondary submission

This situation applies to everyone who modifies the configuration of the project, but this is temporary and only applies to their own local situation, and it needs to be done when it cannot be submitted to everyone for public use.

# 执行命令将文件加入不提交队列
git update-index --assume-unchanged 你的文件路径
# 执行命令将文件取消加入不提交队列
git update-index --no-assume-unchanged 你的文件路径

reference link

Guess you like

Origin blog.csdn.net/f_957995490/article/details/130850727