This article has participated in the third “topic writing” track of the Denver Creators Training Camp. For details, check out: Digg Project | Creators Training Camp third is ongoing, “write” to make a personal impact.

preface

When doing local development, it is common to accidentally generate files that are ignored when we want to manage them. Some students at this time began to delete files crazy. Git provides a way to ignore files -.gitignore

.gitignore

Gitignore is a file named.gitignore, which is placed in the root directory of your project, level with.git.

This file is used to specify which files are not included in Git management. Git commit does not commit these files.

This file is not automatically generated and requires you to create and write rules manually.

Some common examples:

  1. This file is automatically created by VScode
  2. The front-end installation relies on a huge generated node_modules folder
  3. The build folder generated by Electron packaging
  4. Ide-automatically generated. Idea file
  5. Files you don’t want to upload, such as password profiles.
  6. .

Of course, there are many examples.

example

The following is a common front-end development. Gitignore file.

.DS_Store
node_modules/
dist/
CONTRIBUTING.md
npm-debug.log
yarn-debug.log*
yarn-error.log*
yarn.lock
package-lock.json
test/unit/coverage
test/e2e/reports
selenium-debug.log

# Editor directories and files
.idea
.vscode
*.suo
*.ntvs*
*.njsproj
*.sln

Copy the code

Match rule

expression Match the file Description *
**/testIgnore testIgnore/debug.log build/testIgnore/debug.log ** indicates that any subdirectory of the current project is the directory of testIgnore
**/testIgnore/debug.log testIgnore/debug.log build/testIgnore/debug.log ** indicates that any sub-directory of the current project is testIgnore and the debug. Log directory of the testIgnore is displayed
*.log debug.log foo.log .log testIgnore/debug.log * Matches zero or more wildcards.
*.log ! other.log debug.log build.log But this file can’t beother.logOr subdirectoriestestIgnore/other.log ! Exclude this file
*.log ! other/*.log debug.log But it’s notAny.log file in the other directory ! To exclude this file * to exclude any filename
/debug.log debug.log But it’s not testIgnore/debug.log / indicates the root directory.
debug.log debug.log testIgnore/debug.log Matches debug files under any file
debug? .log debug0.log debugg.log But it’s not debug123.log The question mark matches a character.
debug[0-9].log debug0.log debug1.log But it’s not debug10.log Square brackets are used to match a single character within a specified range.
debug[01].log debug0.log debug1.log But it’s not debug2.log debug01.log Square brackets match a single character of the specified set.
debug[!01].log debug2.log But it’s not debug0.log debug1.log debug01.log An exclamation mark can be used to match any character except those in the specified set.
debug[a-z].log debuga.log debugb.log But it’s not debug1.log The range can be letters.
testIgnore testIgnore testIgnore/debug.log testIgnore/latest/foo.bar build/testIgnore build/testIgnore/debug.log To match the file named testIgnore and all directories and files under the directory of testIgnore, run the following command
testIgnore/ testIgnore/debug.log testIgnore/latest/foo.bar build/testIgnore/foo.bar build/testIgnore/latest/debug.log Matches all directories of testIgnore and all directories and files under the directory. The difference is that testIgnore can only be a directory
testIgnore/**/debug.log testIgnore/debug.log testIgnore/file1/debug.log testIgnore/file1/file2/debug.log ** Matches zero or more directories.
testIgnore/*1/debug.log testIgnore/file1/debug.log testIgnore/file21/debug.log But it’s not testIgnore/latest/debug.log * Matches folders ending in 1.
testIgnore/debug.log testIgnore/debug.log Matches only testIgnore/debug log

annotation

Use # to include comments in the.gitignore file:

In the example above, we already mentioned it:

# Editor directories and files

Copy the code

Define.gitignore in the root directory

We usually define.gitignore files in the root directory of the project, but you can choose to define different.gitignore files in different folders. I don’t recommend doing this, however, as it will confuse you and hinder your team’s project development.

Resubmit documents that have been ignored for administration

Sometimes, due to a mistake or lack of consideration, we put files into Git management that shouldn’t have been there. How can we fix this?

$ echo debug.log >> .gitignore
  
$ git rm --cached debug.log
  
$ git commit -m "Start ignoring debug.log"

Copy the code

Resubmission brings neglected files into administration

$ echo ! Gitignore $cat. gitignore $git add -f debug. Log $git commit -mCopy the code