Startec
Startec

Reputation: 13206

How to ignore everything except a folder and all it's subfolders / contents in git

In my .gitignore I have:

*
!file.txt
!folder1
!folder1/
!folder1/*

This almost does what I want except whenever I add something beyond one folder past folder/ it does not get committed. For instance folder1/test/folder2/test.txt will not be committed. Is there any way to fix this without adding endless * to my .gitignore?

Upvotes: 0

Views: 260

Answers (1)

Alex Baker
Alex Baker

Reputation: 1627

EDIT:

I kept playing with this, and here is a shorter version

/*          # ignore everything in root
!/folder1   # except folder1

ORIGINAL:

Give this a shot

*
!folder1
!folder1/**

From gitignore documentation:

Two consecutive asterisks ("**") in patterns matched against full pathname may have special meaning:

...

  • A trailing "/**" matches everything inside. For example, "abc/**" matches all files inside directory "abc", relative to the location of the .gitignore file, with infinite depth.

...

Upvotes: 4

Related Questions