目录

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

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

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


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

touch .gitignore

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

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

logs/
temp/

⚠️ 注意:

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

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

    config.json

让 Git 识别忽略规则:

git add .gitignore
git commit -m "Update .gitignore to exclude logs and temp directories"

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

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

*.log
node_modules/
build/
dist/
.env

  1. 创建 .gitignore 并添加需要忽略的目录/文件。
  2. 如果已经被 Git 跟踪,执行 git rm -r --cached 清除缓存
  3. 提交 .gitignore 并推送到远程仓库。