gitignore 作用

可以忽略对项目没有用的文件

  • 依赖文件: node_modules
  • IDE:.idea .vscode
  • 操作系统:.DS_Store
  • 等等

    gitignore常见问题

  • 不方便管理

  • 偶尔有定制性需求
  • 有冗余的内容

生成.gitignore三种方式

第一种方式:

github上的gitignore项目:https://github.com/github/gitignore 编写了很多语言的共用的 gitignore 文件(比如:搜索node)

第二种方式:

在线生成 .gitignore 文件的网站:https://www.toptal.com/developers/gitignore
image.png
将所需的文件复制到项目的根目录,文件名为.gitignore

实际操作

  1. # 初始化 git 项目
  2. $ npm init -y
  3. $ npm install moment -S
  4. # 初始化 git 仓库
  5. $ git init
  6. # 查看没有状态的文件
  7. $ git status
  8. 未跟踪的文件:
  9. (使用 "git add <文件>..." 以包含要提交的内容)
  10. node_modules/
  11. package.json
  12. 提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)

添加 .gitignore 文件,再次进行 git status

  1. $ git status
  2. 未跟踪的文件:
  3. (使用 "git add <文件>..." 以包含要提交的内容)
  4. .gitignore
  5. package.json
  6. 提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)
  7. node_modules文件就不再显示了

如果现在要忽略的文件之前提交过,直接更改.gitignore 是不生效的,因为git已经保存了一遍快照,需要如下操作:

  1. $ git rm --catched -r .
  2. # 删除git的缓存,但是不会删除提交日志
  3. # 然后gitignore下的内容就都生效了

第三种方式

安装 VScode 编辑器插件:.gitignore Generator
image.png
使用 Ctrl+Shift+P 唤起搜索,输入 git 进行搜索
image.png
选中之后,可以在里面选择需要配置 .gitignore 文件
不过我的电脑(windows)好像不能唤出语言环境,使用不了该插件