How to ignore all files in a repository except a small list of given files?

33 Views Asked by At

I have a git repository with the following structure:

.\.gitignore
.\Dir1
.\Dir2
.\Hello world.txt
.\Dir1\Hi.txt
.\Dir2\Hey!.txt

And I want Git to track only the .\Dir1\Hi.txt, so I excluded all the files with a *, and tried to add an exception for the one file I care. But I tried all the following gitignore exceptions, but none is working:

*
!Dir1/Hi.txt
!/Dir1/Hi.txt
!Dir1\Hi.txt
!\Dir1\Hi.txt

Any idea why?

In reality I want to do this exception for a long list of files in more complex directory structures, but getting the first example to work is crucial on understanding why this is not working.

1

There are 1 best solutions below

0
danimacs On

You can see in this other answer a complete explanation of why your .gitignore is failing: https://stackoverflow.com/a/61556103/12508080

Correct syntax :

*
!Dir1
!Dir1/Hi.txt