.gitignore file role

Generally, we always have some files that don't need to be managed by Git , and we don't want them to always appear in the list of untracked files .

These are usually automatically generated files, such as log files, or temporary files created during compilation. In this case, we can create a  .gitignore file named , listing the patterns of the files to ignore.

Summary of the role of the .gitignore file

.gitignoreThe file is used to ignore the changes of the specified file or folder. The file or folder recorded in the .gitignorefile cannot be tracked by git, that is, the ignored file will not be put into the remote warehouse. If the file already exists in the remote warehouse, it cannot be .gitignoreignored by the file.

.gitignore file creation

.gitignore The files are stored in the root directory of the git repository .

The commands to create/delete files on the command line under linux/mac  .gitignore are as follows:

touch .gitignore # 新建
rm -f .gitignore # 删除

Versions before win10 of the windows system may not be able to directly use the view menu to create/delete  . files beginning with a sign, and need to operate on the command line:

:: 创建
TYPE NUL>.gitignore
:: 删除
DEL /F .gitignore

Format specification (syntax) for .gitignore files

  • All blank lines or  # lines beginning with . will be ignored by Git.
  • Standard  glob pattern matching can be used, which is applied recursively across the workspace.
  • A matching pattern can start with ( /) to prevent recursion.
  • A matching pattern can end with ( /) to specify a directory.
  • Use an exclamation point !to indicate that matching files or directories are not to be ignored (tracked).

A glob pattern is a simplified version of a regular expression used by the shell.

  1. An asterisk ( *) matches zero or more of any character;
  2. A question mark ( ?) matches only one arbitrary character;
  3. [ ]Indicates matching a single character in square brackets:
    (1)  [abc] Either match a, match a b, or match a c
    (2) can be used -to represent consecutive characters, such as 0-9, a-z, A-Zetc., [0-9]which means to match a single character from 0 to 9 character.
    (3) Can be used ^to indicate exceptions, such as [^0-9]to indicate a single character other than 0 to 9.
  4. Use two asterisks ( **) to match any intermediate directory , for example,  a/**/z you can match  a/z ,  a/b/z or,  a/b/c/z etc.

Example .gitignore file

# 忽略所有的 .a 文件
*.a

# 但跟踪所有的 lib.a,即便你在前面忽略了 .a 文件
!lib.a

# 只忽略当前目录下的 TODO 文件,而不忽略 subdir/TODO
/TODO

# 忽略任何目录下名为 build 的文件夹,等价于 build/*.*
build/

# 忽略 doc/notes.txt,但不忽略 doc/server/arch.txt
doc/*.txt

# 忽略 doc/ 目录及其所有子目录下的 .pdf 文件
doc/**/*.pdf

Precautions for using .gitignore files

  1. Before submitting official data for the first time, create a submission  .gitignore file to the local library (and then push it to the remote library).
    Note:  In earlier versions of git, only  .gitignore after submitting the file to the local library, the filtering rules can be applied to the submitted formal data (in the  git2.+ above version, there is no need to submit the file first , as long as  the file exists and is present .gitignore before the formal data is added/submitted for the first time  .gitignoreSet up the filtering rules, but in order to standardize the operation of git, it is recommended to submit and push .gitignore to ).
  2. .gitignoreThe data that has been submitted to the local library (or pushed to the remote library) before being added  will not be affected by the filtering rules.
    Therefore, it must be submitted and pushed first  .gitignore.
  3. After submitting and pushing  .gitignore , it is necessary to update its filtering rules. You must first modify/submit/push to the remote library before submitting formal data to prevent unnecessary data submission.

Guess you like

Origin blog.csdn.net/weixin_40593838/article/details/125803987