Wizard
Wizard

Reputation: 11295

Git ignore all files in directory but not directory

Now I was added in .gitignore

image/cache/*

Now git ignore all cache directory and I can't add this directory to git. Question is how to ignor image/cache/ files but, not directory ?

Upvotes: 2

Views: 1621

Answers (3)

Wizard
Wizard

Reputation: 11295

One of solution is add to directory empty file .gitkeep

Upvotes: 0

Yan Loetzer
Yan Loetzer

Reputation: 56

While the previous answer is pretty correct, this might be helpful:

You can specify a subdirectory of an ignored directory to not be ignored. eg: Ignore cache, but not 'excluded' in cache:

!image/cache
images/cache/*
!images/cache/excluded

You can also force add ignored files using:

git add path/to/file --force

But as previously stated, this only works on files, not directories.

Upvotes: 4

Tim
Tim

Reputation: 43354

As has been mentioned, git tracks files, git does not track folders.

If you put test.txt in a folder, git will "add" that folder only because it's part of the path that leads to test.txt.

Hence, if you ignore all files in a folder, git does nothing with the folder itself and it will not be tracked.

Upvotes: 4

Related Questions