114

Possible Duplicate:
Make .gitignore ignore everything except a few files

Is it possible to let git ignore all files by default, unless specified in a special file?

Community
  • 1
  • 1
morpheus
  • 18,676
  • 24
  • 96
  • 159
  • 1
    As sehe says, the "ignore everything then specify exceptions" approach is probably easiest - see also http://stackoverflow.com/q/987142/223092 and http://stackoverflow.com/q/1248570/223092 – Mark Longair May 15 '11 at 21:59
  • 2
    Not really a duplicate. Solution may be a duplicate, but the idea is to only track listed files. IE. config files in user's home directory on Linux. – xtian Aug 04 '17 at 15:40

2 Answers2

158

You can include !-lines to whitelist files: a .gitignore with:

*
!included/

will exclude all, but the 'included/' directory

Note that if you want files matching a pattern to be un-ignored, in subdirectories, you will need to prevent the containing directories from getting ignored too. This should not pose a large problem, since git doesn't actually track directories, only files (identified by a repository path).

Example:

*
!*/
!SOURCES

will ignore everything, except SOURCES in subdirectories.

sehe
  • 374,641
  • 47
  • 450
  • 633
15

You can use .gitignore for that.

*
!file0.txt
!file1.txt

In a case where you interested in file0.txt and file1.txt.

tamasd
  • 5,747
  • 3
  • 25
  • 31
  • 7
    This will however not work for nested paths, for which you'll have to use [the `!*/` trick](http://stackoverflow.com/a/9227991/321973) – Tobias Kienzler Oct 09 '12 at 06:49