.gitignore ignores files and directories

  • 1. Introduction to .gitignore
  • 2. .gitignore comments
  • 3. Ignoring at the beginning or end of /
  • 4. glob pattern matching ignore
  • 5. .gitignore global ignore
  • 6. Ignore content that has been committed to the remote repository
  • 7. Use ignore rules under various frameworks

1. Introduction to gitignore


The function of the .gitignore file is to tell git which files do not need to be added to version management (definition ignores submitted files)

The .gitignore file is used to ignore the changes of the specified file or folder. The file or folder recorded in the .gitignore file cannot be tracked by git. In other words, the ignored file will not be placed into the remote repository.

In other words, if the file already exists in the remote warehouse, it cannot be ignored by the .gitignore file

2. .gitignore comments


All blank lines or lines starting with the comment symbol # will be ignored by git

Lines beginning with a pound sign (#) are comments and will be ignored, blank lines can be used to improve the readability of the file and to group related modelines

  1. # 忽略编辑器配置目录
  2. /.idea
  3. /.vscode

3. Ignoring at the beginning or end of /


I saw this sentence in an article before: Starting with a slash  / means ignoring the directory

It was later found that this is not the case. Its real function is to ignore only the content of the same level, but not the content of the lower-level directory.

Conclusion: The role of / at the head is to ignore only the contents of the current directory; the role of putting it at the end is to ignore only the directory, not the file

neither head nor tail /

  1. # 忽略当前目录及下级目录中所有的 runtime
  2. # runtime 文件或目录都会被忽略
  3. runtime

preceded by /

  1. # 只忽略当前目录中的 runtime 文件或目录
  2. # 不忽略下级目录中的 runtime 文件或目录
  3. /runtime

followed by /

  1. # 只忽略当前目录和下级目录中的 runtime 目录
  2. # 不忽略当前目录和下级目录中的 runtime 文件
  3. runtime/

Both head and tail /

  1. # 只忽略当前目录中的 runtime 目录
  2. /runtime/

4. glob pattern matching ignore


Standard glob pattern matching can be used in .gitignore files

* Wildcard multiple characters with an asterisk 

  1. # 忽略 vendor 目录下的所有文件
  2. /vendor/*
  3. # 忽略所有后缀名为 txt 的文件
  4. *.txt

? wildcard a single character with a question mark 

  1. # 忽略文件名称为一个字符, 后缀名为 php 的文件
  2. ?.php

[] A matching list of single characters enclosed in square brackets 

  1. # 忽略 125.php、135.php文件
  2. 1[23]5.php

Use an exclamation point  ! to indicate that no matching files or directories are ignored (tracked)

[Note] Note that the folder to be ignored must end at the end  /* , otherwise the rule will not take effect if it is not ignored

  1. # 忽略vendor目录下的所有文件
  2. /vendor/*
  3. # 不忽略vendor目录下的 1.php
  4. # 在已忽略的文件夹中不忽略指定文件
  5. !/vendor/1.php
  6. # 不忽略vendor目录下 dev目录
  7. # 在已忽略的文件夹中不忽略指定文件夹
  8. !/vendor/dev

5. .gitignore global ignore


git allows the creation of global .gitignore files, and all local git repositories will obey the global ignore rules.

The name and location of the file are not required, as long as the path is specified correctly in the git configuration file.

For example, to  ~/.gitignore_global set global git ignore files, you can do:

  1. # 创建文件
  2. touch ~/.gitignore_global
  3. # 将文件添加到 git 配置
  4. git config --global core.excludesfile ~/.gitignore_global

6. Ignore content that has been committed to the remote repository


Delete files or directories in the staging area

  1. git rm --cached <file>
  2. git rm -r --cached <folder>

Add ignore configuration in .gitignore

  1. <file>
  2. /<folder>

Push to remote warehouse

  1. git add .gitignore
  2. git commit -m '忽略文件'
  3. git push origin master

7. Use ignore rules under various frameworks


Ignore editor configuration files

  1. .idea
  2. .vscode
  3. .hbuilderx

ignore special files

.DS_Store files generally appear in MacOS, and are used by Finder to store the display properties of folders, such as: the placement of file icons

  1. .DS_Store

ThinkPHP 5.0

  1. /runtime
  2. /vendor
  3. /thinkphp

Uni-App project

  1. unpackage

 

Guess you like

Origin blog.csdn.net/qq_43985303/article/details/131247361
Recommended