Home  >  Article  >  Development Tools  >  Version control tool Git - ignore files

Version control tool Git - ignore files

齐天大圣
齐天大圣Original
2020-06-17 08:11:462455browse

We know that we do not want to include some cache files, files generated by the editor and other files into the repository. But if you type in the file names one by one every time you git add them, this is really a very annoying operation and can easily cause people to crash. It’s better to use git add. It’s cool! So is there any way for us to ignore some files that we don't want to include in the repository? The answer is yes, you can use .gitignore to set the files you want to ignore in this file. Then use git add. These files will not be submitted.

For example, if I don’t want to include the swp file into the repository, and I don’t need the runtime folder, then I can set it up like this

$ vim .gitignore

# 忽略swp文件
*.swp
# 忽略runtime文件夹
/runtime/

Let’s talk about the usage specifications of .gitignore

  • Git will ignore lines starting with # and blank lines

  • You can use glob regular, which will recurse to each directory (if no recursion is specified)

  • Can start with / to prevent recursion

  • Can end with / to specify the directory

  • You can use ! to select the direction.

! A common usage is that we want to ignore a folder, but keep the index.html in the folder. Then

/data/
!/data/index.html

means ignoring the /data folder but tracking the /data/index.html file.

Let’s talk about what glob regularity is. In fact, it is a simplified version of regularity.

It uses * to match 0 or more arbitrary characters. For example,

*.php 表示匹配以.php结尾的文件
tmp* 表示匹配以tmp开头的所有文件

uses ? to match any character, such as

周?伦

Use [] to match any character in [], such as

[abc] 表示匹配abc其中任意一个

In addition, multiple characters If it is continuous, you can use - instead, such as

[0-9] 表示匹配任意数字

Use two asterisks (**) to match any intermediate directory

比如 a/**/z 可以匹配 a/z 、 a/b/z 或 a/b/c/z 等。

Let’s use an example to explain the usage of ignored files. , Generally, we will ignore the /vendor directory, configuration files, log folder /log, cache files, etc.

# 忽略swp文件
*.swp
# 忽略/vendor文件夹
/vendor/
# 忽略配置文件
/app/config/config.php
# 忽略/log文件夹
/log/
# 忽略缓存文件夹
/runtime/

The above is how to ignore files that you do not want to add to the repository. It is generally recommended to configure this file at the beginning to prevent unnecessary files from being submitted.

The above is the detailed content of Version control tool Git - ignore files. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn