Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git ignore all except subfolder

I searched through other questions but can't find a working solution for my project. Having a Magento project, I want to exclude everything except this:

/app/design/frontend/default/theme_name # and obviously all subfolders
/skin/frontend/default/theme_name # and all subfolders

I've tried a lot of combinations but without luck. Currently I'm stuck with this .gitignore file:

*
!/app/
!/app/*

app/*
!/app/design/
!/app/design/*

But it doesn't work recursively below the design folder. It only added a test file inside the design folder that I created.

like image 468
s3v3n Avatar asked Aug 09 '11 18:08

s3v3n


People also ask

Does Gitignore apply to subdirectories?

gitignore file is usually placed in the repository's root directory. However, you can create multiple . gitignore files in different subdirectories in your repository.

How do I exclude a directory in git?

Repository exclude - For local files that do not need to be shared, you just add the file pattern or directory to the file . git/info/exclude . Theses rules are not committed, so they are not seen by other users.

How do I ignore everything in git?

If you want to ignore a file that you've committed in the past, you'll need to delete the file from your repository and then add a . gitignore rule for it. Using the --cached option with git rm means that the file will be deleted from your repository, but will remain in your working directory as an ignored file.


2 Answers

Look at my answer here: Can't understand how gitignore ignores the folders

Quoting from that:

The following discussion was helpful: http://git.661346.n2.nabble.com/negated-list-in-gitignore-no-fun-td1675067.html , especially the following from Linus:

That's by design. You've chosen to ignore those directories; they match "*" themselves. Thus, 'git add .' doesn't descend into them looking for files.

So basically, for each level you have to go in, unignore that folder, and ignore contents within that folder.

Also, you should look at having .gitignore at subdirectory rather than at root level only as it becomes pretty complex if you have to go to the subdirectory level from the root .gitignore because of the explanation above, whereby for each level, you have to unignore the folder and then ignore the contents and so on.

like image 77
manojlds Avatar answered Sep 24 '22 14:09

manojlds


Did some research here. What worked for me was:

/*
!/directory
!/another
/another/*
!/another/directory

With this subdirectories of /directory were tracked correctly. Curiously it doesn't work with either only / or only * on the first line - I am not sure why.

like image 24
Paweł Obrok Avatar answered Sep 23 '22 14:09

Paweł Obrok