git忽略特定文件或者文件夹

如果想让 Git 忽略指定目录 ,不进行更新或提交,可以使用 .gitignore 文件进行配置。


🛠 方法:使用 .gitignore 忽略目录

1️⃣ 在仓库根目录创建 .gitignore 文件

如果你的项目目录下还没有 .gitignore 文件,可以新建一个:

sh 复制代码
touch .gitignore

或者在 VS Code 中手动创建 .gitignore 文件。

2️⃣ 在 .gitignore 文件中添加要忽略的目录

例如,想要忽略 logs/temp/ 目录:

plaintext 复制代码
logs/
temp/

⚠️ 注意:

  • 斜杠 / 代表目录,例如 logs/ 表示忽略 logs 目录及其所有文件。

  • 如果是单个文件,例如 config.json,则写:

    plaintext 复制代码
    config.json

🔄 3️⃣ 提交 .gitignore

让 Git 识别忽略规则:

sh 复制代码
git add .gitignore
git commit -m "Update .gitignore to exclude logs and temp directories"

🚨 4️⃣ 如果目录已经被跟踪,需先清除缓存

如果 logs/temp/ 目录之前已经被 Git 跟踪,即使你添加了 .gitignore,它们仍然会被 Git 识别。此时需要执行:

sh 复制代码
git rm -r --cached logs/ temp/
git commit -m "Remove logs and temp from tracking"
git push origin main  # 推送到远程

📌 其他 .gitignore 示例

忽略所有 .log 文件

plaintext 复制代码
*.log

忽略 node_modules(前端项目)

plaintext 复制代码
node_modules/

忽略 builddist 目录(编译产物)

plaintext 复制代码
build/
dist/

忽略 .env 配置文件

plaintext 复制代码
.env

💡 结论

  1. 创建 .gitignore 并添加需要忽略的目录/文件。
  2. 如果已经被 Git 跟踪,执行 git rm -r --cached 清除缓存
  3. 提交 .gitignore 并推送到远程仓库。
相关推荐
Qres8216 小时前
Git安装记录
git
wj3055853787 小时前
Codex + Git 开发环境配置指南(WSL版)
linux·运维·git
楠枬10 小时前
Git 分支管理
git
奇怪的点12 小时前
git clone失败
git
WaiSaa12 小时前
Ubuntu配置Git免密操作
git·ubuntu·gitee
牛奶咖啡1313 小时前
Git实践——分支管理与标签管理及git个性化配置
git·禁用 fast forward·bug分支的创建与操作·远程分支的查看与推送·拉取仓库·推送指定分支到远程仓库·标签的创建与操作
千寻girling16 小时前
五一劳动节快乐 [特殊字符][特殊字符][特殊字符]
java·c++·git·python·学习·github·php
波特率11520016 小时前
git指令学习
git·学习
Karry_66617 小时前
[特殊字符] Git 提交项目 全套命令(按顺序执行)
git
计算机安禾17 小时前
【Linux从入门到精通】第39篇:版本控制Git服务器搭建——Gitea/GitLab私有化部署
linux·服务器·git