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/

忽略 build 和 dist 目录(编译产物)

plaintext 复制代码
build/
dist/

忽略 .env 配置文件

plaintext 复制代码
.env

💡 结论

  1. 创建 .gitignore 并添加需要忽略的目录/文件。
  2. 如果已经被 Git 跟踪,执行 git rm -r --cached 清除缓存。
  3. 提交 .gitignore 并推送到远程仓库。
相关推荐
黎羽_12 小时前
终端/办公工具第二组:timewarrior + git-filter-repo + pre-commit + trafilatura + monolith
git·终端工具·开发效率·实战复盘
skywalk816312 小时前
git上传的时候,它这个版的提交,是否包括了以前的改动? 比如下面这个,它前面的一些改动,会自动因为f91285c70dde 提交而提交吗?
git
Haooog1 天前
OpenHands 定时任务同步后还是关着?先查 Git Sync 的启停开关
git·agent·定时任务·配置管理·openhands
崖边看雾2 天前
Git 手册:三区、分支、撤销、冲突与团队协作
大数据·git·elasticsearch
李永奉2 天前
Git和GitHub-VS Code 图形化 Git 放弃更改、暂存与取消暂存、撤销上一次提交(VS Code 界面操作 + 命令行对照)
git·github
aifannihao3 天前
断言从 == 改成 > 0:用 Python 审出 git diff 里的测试降级
git·python·测试·标准库·代码审查
李永奉3 天前
Git和GitHub-VS Code 图形化 Git初始化、状态标识、提交、关联远程仓库与首次推送(全程在 VS Code 内完成,每步附对应Git命令)
git·github
橘子☆心酸3 天前
Git 的自我修养
git·svn·gitee
李永奉4 天前
Git和GitHub-用操作命令控制Git 分支的查看、创建、切换、合并与删除管理实操教程
git·github
7177774 天前
SVN 迁移到 Git 用什么工具:git-svn、svn2git 与平台化方案的取舍
git·svn