How to track file nested in untracked folder?

155 Views Asked by At

Our current git setup works on a whitelist basis, so everything is ignored by default, and we then include directories as needed. This worked fine for a new repository, but is starting to cause issues now that we have a bunch of tracked/untracked files.

Here's an example of our .gitignore:

*
!/example.txt
!/example2.txt

I'm now trying to add a file to git that exists in ignored folder a, the file's path is: /a/b/c/file.txt. My question is, how can I go about not ignoring this file, but ignoring everything else in the /a folder?

So far I've found that adding this to the .gitignore works:

!/a/
!/a/b/
!/a/b/c/
!/a/b/c/file.txt

However, it just seems a bit messy. I originally thought that I'd be able to get away with just having !/a/b/c/file.txt in the .gitignore, but that doesn't seem to work for some reason?

Also for reference, I'm having to run git rm --cached -r . and git add . to get .gitignore changes to take effect.

Any help would be much appreciated - thanks!

2

There are 2 best solutions below

1
EncryptedWatermelon On

I think you need to add !*/. This whitelists directories. Here is a sample ignore..

# ignore everything
*
# don't ignore directories
!*/
# whitelist files with extension
*.xyz

# blacklist individual file
a/b/c.xyz

Other options is to blacklist everything in the directory and whitelist the one file

a/b/c/*
!a/b/c/file.txt
1
phd On

git add --force can be used to add ignored files:

git add -f a/b/c/file.txt

After being added the file is tracked and no longer ignored.