1

Say I have a project with the following structure and I want to ignore everything in data folder but unignore all README.md file.

├── README.md
├── data
│   ├── README.md
│   ├── external
│   │   └── README.md
│   ├── interim
│   │   └── README.md
│   ├── processed
│   │   └── README.md
│   └── raw
│       └── README.md
├── hamlett
│   ├── README.md
│   ├── android
│   ├── models
│   │   ├── README.md
│   │   └── pigeon
│   │       └── README.md
│   ├── site
│   └── tools
└── models
    └── README.md

I tried to put something like this in .gitignore but seems not working.

data/*
!README.md
!**/README.md
!/**/README.md # none of the above 3 lines work...
SwissCodeMen
  • 4,222
  • 8
  • 24
  • 34
shelper
  • 10,053
  • 8
  • 41
  • 67
  • 4
    Does this answer your question? [Make .gitignore ignore everything except a few files](https://stackoverflow.com/questions/987142/make-gitignore-ignore-everything-except-a-few-files) – Gaël J Jun 14 '21 at 20:30
  • it seems doesn't work... – shelper Jun 15 '21 at 01:00

1 Answers1

0

I figured it out how to do this, and below are the .gitignore file with comments

# ignore all files in data directories but not its sub directories
data/**/*
!data/**/

# ignore all files in model directories but not its sub directories
models/**/*
!models/**/

# include all README.md files no matter where it is
!README.md
shelper
  • 10,053
  • 8
  • 41
  • 67
  • 2
    `!**/README.md` makes your Git do unnecessary work. Consider replacing it with the simpler `!README.md`. The effect is the same, but the Git commands run microscopically faster, and—this is the real reason to use the shorter version—it's easier for humans to read. – torek Jun 15 '21 at 12:17