PHP .gitignore template
The PHP template ignores installed dependencies, environment files and test caches, and takes a position on composer.lock that suits a library rather than an application.
At a glance
- Category
- Language
- Patterns in this template
- 12 rules
- Directories ignored
- 2 of the 12 rules end in a slash, so they match directories only
- Also needs an OS template
- Yes. Nothing here covers file manager or editor artefacts.
- Typical companions
- Laravel if you use it, plus an editor template and an OS template.
In detail
/vendor is the dependency directory, restored by the package manager from the manifest. It is anchored to the root with a leading slash, which is deliberate: a project that keeps its own vendored assets in a nested folder of the same name is not affected. The same reasoning as every other language applies, with one extra wrinkle: the vendor directory contains a generated autoloader that hard codes paths, so a committed copy can break on a machine with a different layout.
composer.lock is where this template makes a choice you may want to reverse. The lockfile records the exact resolved version of every package. For an application, that is the only thing standing between your tested dependency set and whatever the resolver decides on the day you deploy, so the near universal advice is to commit it. Only a library, whose consumers resolve their own tree, has a reason to leave it out.
.env holds the database credentials, mail configuration and API keys for a local install, and this template ignores it correctly. The companion habit that makes this work is committing a .env.example with the same keys and empty values, so a new developer knows what to fill in without you having to send anything privately.
storage/*.key is a small rule with a large consequence. That path holds application encryption keys, and a leaked one lets anyone decrypt session cookies and any data the application encrypted at rest. The Homestead.json and Homestead.yaml rules cover local virtual machine configuration that describes one developer's folder layout.
The rest is caches and logs: *.cache, .phpunit.result.cache, npm-debug.log and yarn-error.log. node_modules appears here too, because a PHP application with a frontend build step will have one, and the general rule about restoring rather than committing dependencies does not change with the language.
Examples
/vendor/
node_modules/
composer.phar
composer.lock
*.cache
.env
.phpunit.result.cache
Homestead.json
Homestead.yaml
npm-debug.log
yarn-error.log
storage/*.key12 lines exactly as the tool writes them, before any common additions you tick.
Things that catch people out
- composer.lock is ignored by default. For an application, remove that line, otherwise production installs whatever the resolver picks rather than what you tested.
- If .env was committed before you added this template, ignoring it now hides it from git status while leaving it in every clone and in the history. Remove it from the index, then rotate the credentials.
- The template ignores composer.phar but not a committed vendor/autoload.php from an earlier era, so an old repository can end up with a stale autoloader that is still tracked.
Try it: .gitignore Generator
Pick one or more templates, tick the common additions you want, and copy or download the combined .gitignore.
.gitignore Generator
LOCAL · Browser-OnlyGenerate comprehensive .gitignore files by combining templates for languages, frameworks, IDEs, and operating systems
Language
Framework
IDE
OS
Build Tool
Infrastructure
Common Additions
Preview
Select templates or common additions to generate a .gitignore file.
Related .gitignore templates
All .gitignore templates →Laravel .gitignore template
The Laravel template ignores environment files, application keys, compiled assets and editor folders, and it is the most secret aware template here.
Ruby .gitignore template
The Ruby template anchors most rules to the repository root with a leading slash, and ignores Gemfile.lock, which is right for a gem and wrong for an app.
Node.js .gitignore template
The longest JavaScript oriented template: dependency directories, every package manager's debug log, bundler caches, env files and Yarn Plug'n'Play state.
Swift .gitignore template
What the Swift template ignores: the package build folder, Xcode user state, derived data and symbol archives, plus the Package.resolved decision it makes.
Kotlin .gitignore template
The Kotlin template covers the same class and archive output as Java, plus the .kotlin session directory the newer compiler writes into the project root.
Dart .gitignore template
The Dart template ignores the tool directory, generated docs and compiled JavaScript output, and ignores pubspec.lock, which an application should commit.
Tools for this
- .gitignore Generator Generate .gitignore files for any project - 50+ templates for languages, frameworks, IDEs, and OS
- .env Editor & Validator Edit and validate .env files online - detect duplicates, mask secrets, generate .env.example