- Building Enterprise JavaScript Applications
- Daniel Li
- 228字
- 2021-07-23 16:31:16
Using .gitignore to ignore files
Git allows for a special .gitignore file that allows us to specify which files Git should ignore. So, create a .gitignore file at the project root directory, and add the following lines:
node_modules/
dist/
Now, when we run git status again, node_modules/ and dist/ are gone from our list, and .gitignore is added:
$ git status
Untracked files:
.babelrc
.eslintrc.json
.gitignore
.nvmrc
package.json
src/
yarn.lock
Apart from the node_modules/ and dist/ directories, there will be many other files we'll eventually want Git to ignore; for example, a yarn-error.log is generated whenever yarn encounters an error. It is for our information only and should not be tracked on Git. While we can keep adding more and more lines to the .gitignore file as is required, many others working with Node.js have already worked together to compile a list of common files and directories that most projects should ignore; we can use that as a basis and modify it as needed.
Go to github.com/github/gitignore/blob/master/Node.gitignore and replace our .gitignore file with the content of the Node.gitignore file; but remember to add the dist/ entry back at the end.
Now, let's add everything to the staging area and commit them:
$ git status
Untracked files:
.babelrc
.eslintrc.json
.gitignore
.nvmrc
package.json
src/
yarn.lock
$ git add -A
$ git commit -m "Initial project setup"