What is .gitignore for

Git ignores files. [gitignore] details

In the project, not all files need to be saved in the repository, for example, the “target” directory and the files in the directory can be ignored. [Create a special .gitignore file in the root directory] of the Git workspace , and then fill in the file names to be ignored, and Git will automatically ignore these files or directories.

Git ignore rule [priority]

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):

Read available ignore rules from the command line
Rules defined by the current directory
Rules defined by the parent directory, recursively
Rules defined in the $GIT_DIR /info/exclude file
Global rules defined in core.excludesfile

Git ignores rule matching syntax

In the .gitignore file, the syntax of the ignore rule for each line is as follows:

Space does not match any file, can be used as a delimiter, can be escaped with a backslash
A file identifying comment at the beginning, which can be escaped with a backslash
Patterns at the beginning of ! are negated, the file will be included again, and if the parent directory of the file is excluded, using ! will not be included again. Can be escaped with a backslash
/ The pattern ending in only matches the folder and the contents of the folder path, but does not match the file
/ starts with pattern matching items and directories
If a pattern does not contain slashes, it matches relative to the path of the current .gitignore file, or relative to the project root if the pattern is not in a .gitignore file
** Match multi-level directory, can be at the beginning, middle, end
? Generic matches a single character
* Universal matches zero or more characters
[] Generic matches a list of single characters

Common match examples

bin/: Ignore the bin folder in the current path, all contents in this folder will be ignored, and the bin file will not be ignored
/bin: ignore the bin file in the root directory
/*.c: ignore cat.c, don't ignore 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: abbreviated a/b , a/ x /b, a/ x / y/b, etc.
! /bin/run.sh: do not ignore the run.sh file in the bin directory
*.log: ignore all .log files
config.php: ignore the config.php file in the current path

.gitignore rule does not take effect

.gitignore can only ignore 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 it:

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

You want to add a file to Git, but you can’t, because the file is ignored by .gitignore:

$ git add App.class
The following paths are ignored by one of your .gitignore files:
Use -f if you really want to add them.

If you really want to add the file, you can force it to Git with -f:

$ git add -f App.class

Or you find that there may be something wrong with .gitignore, and you need to find out which rule is wrong. You can use the git check-ignore command to check:

$ git check-ignore -v App.class
.gitignore:3:*.class    App.class

Git will tell us that the .gitignore rule on line 3 ignores the file, so we can know which rule to revise.

Commonly used .gitignore files in Java projects

# Compiled class file

# Eclipse

# Intellij

# Maven

# Gradle

# Log file

# out

# Mac

# others

Leave a Comment

Your email address will not be published. Required fields are marked *