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

相关推荐
SStone_TJ14 小时前
【常用的git命令】
git
没有鸡汤吃不下饭15 小时前
Git将某个分支合并到开发(dev)、测试(test)后突然想撤销该分支的功能,怎么处理?
前端·git·github
康一夏15 小时前
git fatal:Server aborted the SSL handshake
git·网络协议·ssl
Vio7251 天前
在IntelliJ IDEA中使用Git
git
Net_Walke1 天前
git 的常用命令
git·物联网·github·iot
L X..1 天前
Git 无法访问 GitHub(Recv failure: Connection was reset)问题解决教程
git·github
建群新人小猿2 天前
客户标签自动管理:标签自动化运营,画像持久保鲜
android·java·大数据·前端·git
来一颗砂糖橘2 天前
Git 进阶指南:深入掌握 git log 查看提交历史
git·版本控制·开发技巧
六点半8882 天前
【Git】远程操作 + 给命令配置别名 + 标签管理
git
虫师c3 天前
GitOps实战:ArgoCD+Tekton打造云原生CI/CD流水线
git·ci/cd·云原生·kubernetes·argocd·tekton