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 并推送到远程仓库。
相关推荐
李boyang9 天前
Git(四):远程操作
git
荻野泽溪9 天前
Git新建分支并同步到远程
git
漫步企鹅9 天前
【Git】新建一个远程分支的常规操作
git·新建远程分支
潇-xiao10 天前
Linux下的版本控制器Git(15)
linux·笔记·git
@昵称不存在10 天前
Git学习
git·学习
pe7er10 天前
⛔️⛔️⛔️丢弃本地commit,强制采用远端代码
git
pe7er10 天前
使用 `git worktree` 管理多个工作目录,比 `git stash` 更高级的分支管理方式
git
浮梦终焉10 天前
CLion开发Qt桌面程序_git的简单使用_小团体
c++·git·qt·桌面应用·clion
至此流年莫相忘10 天前
Git集成Jenkins通过Pipeline方式实现一键部署
运维·git·jenkins
科文小白狼10 天前
Git使用总结
git