0

I have a git folder with .gitignore structured like this:

input/
src/
output/
additional-1/
additional-2/
file.csv
file.txt

And my .gitignore currently looks like this:

input/
output/
file.txt

There are some files already (particularly from src/) added to git, and there are plenty which are not. I have noticed that it would probably be a lot easier for me to ignore all files except some directories/files. Therefore I followed this answer and replaced .gitignore body with:

*
!src/*

or

*
!*/src/

(not sure which version is correct)

However, now when I print git status I get only modified files from src/ directory and no untracked files, whereas there are certainly some files from src/ which are not yet added to git. Is is normal behaviour? If they are not recognised by git status how can I git add the new files? Or should I change somehow my "all-except" .gitignore version?

Xaume
  • 293
  • 2
  • 16
  • Can you please make a clearer point on the end result you want to reach ? For example : I don't see how to make sense of "it would probably be a lot easier for me to ignore all files except some directories/files" (which you did) and "If they are not recognised by git status how can I git add the new files?" at the same time. – LeGEC Oct 15 '20 at 07:31

1 Answers1

0

Based on the official documentation:

An optional prefix "!" which negates the pattern; any matching file excluded by a previous pattern will become included again. It is not possible to re-include a file if a parent directory of that file is excluded. Git doesn’t list excluded directories for performance reasons, so any patterns on contained files have no effect, no matter where they are defined.

So it means, that * pattern excludes your src folder, and then you try to include some file from src folder - but it will not work, based on the documentation. If you files to be included would be in root directory (on the same level as .gitignore file), then you could track it regardless * pattern. For example, if you'd have repository like this:

sample repository

and your .gitignore file would be:

*
!D.txt

Then, D.txt file would be tracked.

So the solution to your problem will be to remove * pattern from .gitigore file; then add there each directory you'd like to ignore, and then specify files which you'd like to exclude from .gitigore (which you'd like to track).

kosist
  • 2,868
  • 2
  • 17
  • 30