You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Gitignore配置求助:如何忽略所有内容仅保留指定3个文件

How to Configure .gitignore to Keep Only Specific Files

Hey there! I’ve run into this exact scenario before—trying to ignore everything except a handful of specific files can feel tricky at first, but the solution is straightforward once you get the hang of Git’s ignore rules.

Here’s the correct .gitignore setup you need:

# Ignore all files and directories by default
*

# Unignore the files you want to keep
!.gitignore
!README.md
!main.c

Why this works:

Git’s .gitignore uses negation patterns (!) to override earlier ignore rules. By starting with *, we tell Git to ignore every single thing in the directory. Then, each ! line explicitly tells Git "wait, don’t ignore this specific file."

Important notes if your files are in subdirectories:

If, for example, main.c lives inside a src/ folder, you’ll need to adjust the rules to first unignore the directory (otherwise Git won’t look inside it to find the file):

*
!.gitignore
!README.md
!src/
!src/main.c

Quick troubleshooting tips:

  • Double-check for conflicting rules in your .gitignore (like other lines that might be accidentally ignoring these files).
  • If you’ve already tracked files that should be ignored, run git rm --cached <filename> to remove them from Git’s tracking without deleting them locally, then commit the change.

内容的提问来源于stack exchange,提问作者L Ja

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 03:39:36