Ignore all files in a directory except a specific file

Published on
2 mins read
Gitignore Example

How to Ignore All Files in a Directory Except a Specific File

There are situations in Git where you may want to ignore all files within a directory but keep one specific file under version control. For example, you might want to track a configuration file while ignoring all others. This can be achieved using a .gitignore file.

Basic .gitignore Example

In your .gitignore file, the pattern is as follows:

# Ignore all files in a directory
directory/*

# Keep only this file
!directory/file-to-keep.ext

How It Works

  1. The first line directory/* tells Git to ignore everything inside the directory/ folder.
  2. The second line !directory/file-to-keep.ext tells Git to explicitly not ignore the file file-to-keep.ext in that directory.

This way, you can ensure that while the folder's contents remain untracked, one file stays under version control.

Example Use Case

Let’s say you have a configuration directory with many generated files, but you only want to keep config.json:

config/*
!config/config.json

This configuration ensures that only config.json is tracked, while all other files in the config/ directory are ignored.

Notes and Tips

  • Order matters: The order of lines in the .gitignore file is important. You must first ignore the entire directory and then add the exception for the file you want to keep.
  • Subdirectories: You can apply the same rule for files in subdirectories. For example:
    directory/subdirectory/*
    !directory/subdirectory/file-to-keep.ext
    
  • Tracking changes: If the file you want to keep is already tracked and you're just updating your .gitignore, Git will continue to track it even if it's listed to be ignored. You can untrack the file using:
    git rm --cached directory/file-to-remove.ext
    

By following this approach, you can keep your repository clean while ensuring that only essential files are tracked. This can be useful in scenarios such as tracking only environment files or configuration settings in a project.

Keep your repo clean!