Max
Max

Reputation: 3

.gitignore overly ignores files

I'm working on a project on linux, with git version 1.8.x . I tried to set some rules with .gitignore and quickly realized that listing everything you don't want to track is not what I actually want. So I tried instead to precise which patterns git should track, ie:

*

#code files
!**/*.cpp
!**/*.h

But then, git doesn't track any new files anymore. So is it possible to find a way to make it works? Moreover using !**.cpp works only for the currents folders files.

Thanks o/

Upvotes: 0

Views: 218

Answers (2)

Samuel O'Malley
Samuel O'Malley

Reputation: 3551

Don't forget to add !.gitignore so it doesn't ignore itself!

#ignore everything
*
#Whitelist subdirectories - thanks to @Gabriele Petronella
!*/    

#except these:
!*.cpp
!*.h
!.gitignore

Upvotes: 0

Gabriele Petronella
Gabriele Petronella

Reputation: 108169

#Ignore everything
*

#Whitelist subdirectories
!*/

#Whitelist the gitignore itself
!.gitignore

#Whitelist .cpp and .h files
!*.h
!*.cpp

Remarks

  • remember to include the .gitignore itself
  • * ignores everything, directories included. Explicitly whitelisting subdirectories still ignores the files they contain, but it allows you to whitelist recursively
  • with the previous point in mind, you can now just use !*.fileext as a pattern to whilelist single extensions

Upvotes: 2

Related Questions