How do I ignore all files apart from one or two directories?

Go To StackoverFlow.com

3

I have looked through all previous stackoverflow questions related to this, but found none of the answers there to match what I want to do.

I am developing a component and a template for a content management system. All of my component files are in a subdirectory called 'components/com_rhgallery/', all of my template files are in a subdirectory called 'templates/rhgallery/'

I would like to have a single repository at the root and a single '.gitignore' file. I would prefer not to use git's submodule features as these are overkill for what I need.

Ideally I'd like to create a gitignore file that ignores everything in the project apart from the contents of the directories 'components/com_rhgallery' and 'templates/rhgallery/'

Simplistic solutions such as the following don't work because of the way that git works:

*
!components/com_rhgallery/
!templates/rhgallery/

Linus (himself!) in one posting had suggested force adding files or directories, but this is only appropriate when there are a small number of files to be added and when the directory structure is failry static.

So, my question is, how would you create a single gitignore file to accomplish this relatively simple task of ensuring that only those files in these two directories are tracked?

2012-04-03 23:05
by user1104799
What if you just ignore all other directories explicitly - zerkms 2012-04-03 23:07
I could do that but there are an awful lot of them and they change dynamically as extraneous plugins are added - user1104799 2012-04-04 02:03


2

I believe

.gitignore :
*
!/components/com_rhgallery/*
!/templates/rhgallery/*

components/com_rhgallery/.gitignore :
!*
templates/rhgallery/.gitignore :
!*

might solve your problem.

2012-04-03 23:19
by JDD
I want to avoid multiple gitignore files. For something as simple as this i'd hoped to be able to use only one gitignore file - user1104799 2012-04-04 01:19
Unfortunately that is where you need to come to the realization that what you are asking for while simple in concept is not simple in practice. The gitignore is generally to ignore a few select build files and things of that nature, and we are attempting to in a way repurpose it's use here :). If it was as simple as your wanting to believe it is and looking for, you probably wouldn't be posting the question =D. I hope you find a simpler method however - JDD 2012-04-04 01:32
I understand your point jdd and appreciate your solution as it does seem the cleanest so far even though it requires the extra gitignore file - user1104799 2012-04-04 02:01
Just tried this and it didn't work :( I think that the second and third lines aren't being executed for some reason, so it is never finding the additional .gitignore files ... when i 'git add .' to add files nothing gets added to the index from the /com_rhgallery -and other directory -- any idea why that would be - user1104799 2012-04-04 04:44
OK. Just found the error. You have to 'unignore' both the 'components/' and the 'comrhgallery/' folders (if the first isn't unignored the second doesn't get found I think) ... so the file would read * !components/ !components/comrhgallery

using this approach seems to work without the addiitional * matche - user1104799 2012-04-04 05:17

that was a typo in my post i just noticed =D but good find and I'm glad it's working for you now - JDD 2012-04-04 06:31
Ads