gitgitignore

Make .gitignore ignore everything except a few files


I understand that a .gitignore file cloaks specified files from Git's version control.

How do I tell .gitignore to ignore everything except the files I'm tracking with Git? Something like:

# Ignore everything:
*

# Do not ignore these files:
script.pl
template.latex

Solution

  • An optional prefix ! which negates the pattern; any matching file excluded by a previous pattern will become included again. If a negated pattern matches, this will override lower precedence patterns sources.

    # Ignore everything
    *
    
    # But not these files...
    !.gitignore
    !script.pl
    !template.latex
    # etc...
    
    # ...even if they are in subdirectories
    !*/