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

相关推荐
nyf_unknown7 小时前
(vue)将文件夹打成tar包, Git Bash(推荐)具体使用
vue.js·git·bash
兔老大RabbitMQ8 小时前
Git Revert 特定文件/路径的方法
git
星哥说事1 天前
如何将堡塔云WAF迁移到新的服务器
服务器·git·github
阿政一号1 天前
Git版本控制器
git
妮妮喔妮1 天前
SSH协议的GIT转换
运维·git·ssh
今禾1 天前
Git 日常使用与面试考点详解:从入门到精通
前端·git·面试
Data_Adventure2 天前
能连上 GitHub(SSH 验证成功),却 push 失败?常见原因与逐步解决方案
前端·git·github
间彧2 天前
如何解决Git客户端下载缓慢问题
git
Tearstornbyrain2 天前
在Ubuntu24.04中使用ssh连接本地git仓库到github远程仓库
linux·git·ubuntu·ssh·github
四七伵2 天前
一次 Git Rebase 事故,让我彻底明白 Rebase 和 Merge 的区别
git·后端