.gitignore: The Gate Keeper to Your Repository

A .gitignore file is a handy tool to use for defining certain types of files or paths to exclude from tracking by the Git repository. By using a .gitignore, certain files will not appear in the git status or be picked-up by git add--they're ignored. I like to selectively add files to a commit but it's easy to miss things or unintentionally add changes, especially on a commit that includes many changes.

The .gitignore is not hard to work with, it uses simple pattern matching. This allows you to specify specific files or directories as well as certain types of files based on the pattern. You can even configure the .gitignore to be ignored. Think about that for a minute. The complicated part is deciding what should be committed.

My take on managing a project repository is that the framework of the project goes in the repository, and content does not.

In theory the framework is the delivery mechanism, it can stand on its own regardless of the content and it consists of the code base and general configurations. This includes PHP code, CSS and JavaScript files, and other files that provide the structure for the framework. Although Drupal clouds this notion by holding some configuration in the database, I don't recommend storing database backups in the repository.

Content is what is delivered by the framework and it does not belong in the repository. It's generally user-input data (nodes, etc) and uploaded files. It's generally quite easy to use .gitignore to prevent these from getting into the repository since they're segregated into the sites/default/files directory.

In the Repository

  • The Drupal project. In fact I usually start a Drupal project by cloning the repository directly from Drupal.org and perform core upgrades by merging in tags as new releases are available.
  • Contributed modules and themes.
  • Custom modules and themes, including features.
  • Images and files critical to the structure of your site.

NOT In the Repository

  • User uploaded images and files.
  • Database dumps.
  • Site-specific configuration, namely settings.php files.
  • Backup copies of folders or files. C'mon, you're using version control.
  • Other stuff that just doesn't belong.

Workable Baseline .gitignore

Drupal 6 does not ship with a .gitignore, so you will need to add a .gitignore to any D6 projects. Drupal 7 and 8 do include a .gitignore but it's brief and can be improved upon. I'm curious to hear tips from other developers so feel free to leave comments and suggestions.

# Ignore paths that contain user-generated content

# Packages

# Logs and databases

# OS generated files