How to unignore some files or dirs in git?

Posted on In Software, Tutorial

I set up rules to ignore all files under git by adding a .gitignore like

*

But how to unignore some files or dirs in git? For example unignore all files under

./bin/tool1/

under the git repository.


You can use patterns with as the prefix to “unignore” files. From gitignore man page:

An optional prefix “!” which negates the pattern; any matching file excluded by a previous pattern will become included again.

Please note that it is not possible to re-include a file if a parent directory of that file is excluded because Git doesn’t list excluded directories for performance reasons. So all the parent directories of the unignored files should be unignored too.

For the above example, the .ignore file could be

*
!/bin
/bin/*
!/bin/tool1
!/bin/tool1/*

Eric Ma

Eric is a systems guy. Eric is interested in building high-performance and scalable distributed systems and related technologies. The views or opinions expressed here are solely Eric's own and do not necessarily represent those of any third parties.