Little Git Tricks: Ignore Everything in a Folder Except a Few Files

With the .gitignore file we can tell Git to ignore certain types of files (like *.exe) or whole folders. All it takes is an entry like this if we want to ignore the folder database:

As explained in another post, we can ignore the .gitignore by putting a ! in front of the entry. To keep the file /database/create.sql, we create an entry like this:

Unfortunately, Git still ignores our create.sql file. It took me a while to figure out that the problem is not in the line with create.sql, but with the database folder. We must append /* to the folder name so that Git can ignore everything inside the folder but keeps the file create.sql:

With this syntax you can add all the files you want to keep in Git while ignoring everything else in a folder.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.