How do I tell Git to ignore everything except a subdirectory?

Michael Goerz picture Michael Goerz · Aug 8, 2009 · Viewed 94.4k times · Source

I want to ignore all files in my repository except those that occur in the bin subdirectory. I tried adding the following to my .gitignore:

*
!bin/*

This does not have the desired effect, however: I created a new file inside of bin/, but doing git status still shows nothing to commit (working directory clean).

Any suggestions?

Answer

Tyler Laing picture Tyler Laing · Dec 21, 2011

This ignores root files & root directories, then un-ignores the root bin directory:

/*
/*/
!/bin/

This way you get all of the bin directory, including subdirectories and their files.