In general, some files do not need to be managed by git, and we do not want them to appear in the list of untracked files. Generally, these files are automatically generated, such as log files or temporary files created during compilation. You can create a file named. gitignore to list the file modes to ignore. Let's look at a practical example:
$ cat .gitignore*.[oa]*~
The first line tells git to ignore all files ending with. O or.. Generally, such object files and archive files are generated during compilation, so we do not need to track their versions. The second line tells git to ignore all (~
). Many text editing software (such
Emacs) saves copies with such file names. In addition, you may need to ignore the log, TMP or PID directory, and automatically generated documents. You need to develop the habit of setting up. gitignore files from the beginning, so as not to mistakenly submit such useless files in the future.
The format of file. gitignore is as follows:
- All blank lines or lines starting with '#' are ignored by git.
- You can use the standard glob pattern matching.
- The matching mode is followed by the backslash (
/
) Indicates the directory to be ignored.
- To ignore files or directories other than the specified mode, you can add an exclamation point before the mode (
!
.
The so-called glob mode refers to the simplified regular expression used by shell. Asterisk (*
) Matches zero or multiple arbitrary characters;[abc]
Match any character in square brackets (in this example, either
A, either matching a B or a C); Question mark (?
) Matches only one arbitrary character. If two characters are separated by a short line in square brackets, it means that all the characters within the two character ranges can match (for example[0-9]
Match All
0 to 9 ).
Let's look at an example of the. gitignore file:
# This is a comment-it will be ignored by git *. A # All files ending with. A will be ignored! Lib. A # But Lib. except for a/todo # ignore only the todo files under the project root directory, excluding subdir/todobuild/# ignore all files under the build/directory DOC /*. TXT # DOC/notes.txt is ignored, but Doc/Server/arch.txt is not included.