git中忽略文件.gitignore文件的用法

我们在日常开发中 项目中有很多文件是不需要上传的 比如一些依赖或自动生成的文件 如果多人开发的时候 上传这些文件 不仅导致远端项目体积增大 并且很容易产生冲突

对此我们可以使用一个.gitignore文件来让git忽略跟踪某些文件的改变,如下所示

复制代码
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?

# Environment variables
.env.test
.env.production

# Auto-generated types
@types/auto-imports.d.ts
@types/components.d.ts
  1. 日志文件
    日志文件通常是运行应用程序时生成的,包含调试信息或错误信息,通常不需要提交到版本控制系统。

    logs/
    .log
    npm-debug.log

    yarn-debug.log*
    pnpm-debug.log*
    lerna-debug.log*

  2. 依赖文件
    依赖文件夹(如 node_modules)包含通过包管理工具(如 npm、yarn 或 pnpm)安装的依赖项。这些文件可以通过 package.json 和 package-lock.json 重新生成,因此不需要提交。

    node_modules/

  3. 构建输出
    构建工具生成的文件(如 dist 或 build 文件夹)是由源代码编译生成的,通常不需要提交到版本控制系统。

    dist/
    build/
    dist-ssr/

  4. 环境变量文件
    环境变量文件(如 .env 文件)通常包含敏感信息(如 API 密钥、数据库连接字符串等),不应该提交到版本控制系统。

    .env
    .env.local
    .env.*.local
    .env.production
    .env.test

  5. 编辑器和 IDE 配置
    开发人员使用的编辑器或 IDE 可能会生成一些本地配置文件,这些文件通常是个人化的,不需要提交到版本控制系统。

    .vscode/
    .idea/
    *.suo
    .ntvs
    *.njsproj
    *.sln
    .DS_Store
    *.sw?

  6. 调试文件
    调试工具生成的文件(如 npm-debug.log、yarn-error.log)通常是临时文件,用于记录调试信息,不需要提交。

    npm-debug.log*
    yarn-debug.log*
    pnpm-debug.log*

  7. 自动生成的文件
    自动生成的文件(如类型声明文件、缓存文件等)通常是由工具生成的,不需要提交到版本控制系统

    @types/auto-imports.d.ts
    @types/components.d.ts

但由于

● .gitignore 只对未被跟踪的文件生效。

● 如果文件之前已被提交到仓库,Git 会继续跟踪它,即使后来添加了 .gitignore 规则。

● 因此,必须先使用 git rm --cached 清除 Git 对该文件的跟踪,再提交更改。

在终端执行 git rm --cached @types/auto-imports.d.ts以及git rm --cached @types/components.d.ts

相关推荐
驯龙高手_追风9 小时前
Gitlab本地服务器搭建及配置-详细教程
git·github
czhc114007566310 小时前
6.11:halcon,Sqlserver;项目sql连接;git
git·sql·sqlserver
炸炸鱼.11 小时前
Git+Jenkins 基本使用:从入门到实战(知识点大全)
运维·git·jenkins
戴国进13 小时前
git stash 用法详解
git
木雷双雄714 小时前
Git 版本回退操作指南
git
m0_579146651 天前
已被 Git 追踪的本地修改文件如何实现临时忽略
git
糖少主1 天前
WSL中使用Beyond Compare 3/4/5作为difftool
git·wsl·beyond compare·difftool
console.log('npc')1 天前
Git版本管控:git reset \+ git push \-f 原理、实操与避坑指南
git
恋喵大鲤鱼1 天前
git reflog
git·git reflog
MatrixOrigin1 天前
MatrixOne Git4Data 技术详解(二):从零跑通所有 Git 原语
git