Skip to content

Git .gitignore for Laravel: Complete Guide for PHP Developers

32 people viewed this

Git .gitignore for Laravel: Complete Guide for PHP Developers

Git .gitignore for Laravel — Complete Guide for PHP Developers

When you create a Laravel application, many files are generated automatically during development.

Some files should be committed to Git because they are part of the application source code. Other files should not be committed because they contain:

  • Environment-specific configuration
  • Passwords and API keys
  • Installed dependencies
  • Temporary files
  • Application logs
  • Cache files
  • IDE configuration
  • Operating-system files
  • Generated build artifacts

This is where .gitignore becomes important.

A typical Laravel project contains:

laravel-project/
├── app/
├── bootstrap/
├── config/
├── database/
├── public/
├── resources/
├── routes/
├── storage/
├── tests/
├── vendor/
├── node_modules/
├── .env
├── .env.example
└── .gitignore

In this article, we will understand exactly how .gitignore works and how to use it correctly in Laravel projects.


1. What Is .gitignore?

.gitignore is a special Git configuration file that tells Git which files or directories should be ignored when detecting untracked files.

For example:

.env
/vendor/
/node_modules/

This means Git should not normally show these ignored paths as untracked files.

The basic idea is:

Local Project
     │
     ├── Application Code ───────→ Git
     │
     ├── .env ───────────────────→ Ignore
     ├── vendor/ ────────────────→ Ignore
     └── node_modules/ ──────────→ Ignore

2. Why Is .gitignore Important in Laravel?

A Laravel application can generate a large number of files that don't belong in source control.

For example:

vendor/
node_modules/
storage/logs/
.env
bootstrap/cache/

Without proper ignore rules, developers could accidentally commit:

  • Production credentials
  • Database passwords
  • API keys
  • Thousands of dependency files
  • Log files
  • Temporary cache files

A good .gitignore helps keep the repository clean and reduces the chance of accidentally committing environment-specific or sensitive files.


3. Laravel's .gitignore File

When you create a Laravel application, the project normally includes a .gitignore file.

A typical Laravel project needs ignore rules for directories such as:

/vendor/
/node_modules/
/storage/*.key
/storage/framework/
/storage/logs/
/bootstrap/cache/

The exact contents can vary by Laravel version and project requirements, so you should treat the project's generated .gitignore as the starting point rather than blindly replacing it.


4. A Practical Laravel .gitignore Example

A practical project may contain rules such as:

/vendor/
/node_modules/

/.env
.env.*
!.env.example

/storage/*.key
/storage/framework/
/storage/logs/

/bootstrap/cache/

/public/build/
/public/hot/

/phpunit.xml
/.phpunit.result.cache

.php-cs-fixer.cache

/.idea/
/.vscode/

.DS_Store
Thumbs.db

However, some entries are project-dependent. For example, if your CI system or team intentionally tracks a particular configuration file, don't ignore it merely because another Laravel project does.


5. What Does /vendor/ Mean?

Laravel applications use Composer for PHP dependencies.

When you run:

composer install

Composer creates:

vendor/

This directory contains installed PHP packages.

Normally, you do not commit the entire vendor/ directory.

Instead, commit:

composer.json
composer.lock

Then another developer can run:

composer install

to recreate the dependencies.


6. Why Should vendor Be Ignored?

Suppose your project contains:

vendor/
    laravel/
    symfony/
    guzzle/
    monolog/
    psr/
    ...

These files can be numerous and are generated from the dependency definitions.

The repository should generally track the dependency definitions rather than the generated dependency tree.

composer.json
composer.lock
       ↓
composer install
       ↓
vendor/

This keeps the repository smaller and makes dependency installation reproducible.


7. What Is node_modules?

Laravel projects can use Node.js-based frontend tooling such as Vite.

Running:

npm install

creates:

node_modules/

This directory can contain a very large number of files.

Therefore, it is normally ignored:

/node_modules/

Instead, commit:

package.json
package-lock.json

or the package-manager files appropriate to your project.


8. What Is .env in Laravel?

The Laravel .env file contains environment-specific configuration.

For example:

APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:...
DB_DATABASE=pharmacy
DB_USERNAME=root
DB_PASSWORD=secret

These values can contain sensitive information.

Therefore, a real environment file should generally not be committed to the repository.

.env

should normally be ignored.


9. Why Should .env Be Ignored?

Consider this:

DB_HOST=production-db.example.com
DB_USERNAME=admin
DB_PASSWORD=MySecretPassword
STRIPE_SECRET=...
AWS_SECRET_ACCESS_KEY=...

If these values are committed to a public or incorrectly accessible repository, they can become a serious security problem.

Instead:

.env
   ↓
Local / Server Environment

.env.example
   ↓
Safe configuration template

10. What Is .env.example?

Laravel projects commonly include:

.env.example

This file provides example configuration without containing real secrets.

For example:

APP_NAME=Laravel
APP_ENV=local
APP_KEY=
APP_DEBUG=true

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=
DB_USERNAME=
DB_PASSWORD=

This file is normally committed to Git because it helps developers understand which environment variables are required.


11. .env vs .env.example

File Usually Commit? Purpose
.env No Actual environment configuration
.env.example Yes Configuration template

12. What Is storage/?

Laravel uses the storage/ directory for generated application data.

It can contain:

  • Logs
  • Framework cache
  • Compiled Blade files
  • Application-generated files
  • Temporary data

A typical structure is:

storage/
├── app/
├── framework/
│   ├── cache/
│   ├── sessions/
│   └── views/
└── logs/

Many generated contents should not be tracked.


13. Why Ignore storage/logs?

Laravel log files can change constantly.

For example:

storage/logs/laravel.log

If this file were committed every time it changed, the Git repository would contain application runtime data rather than source code.

Therefore:

/storage/logs/

is normally ignored.


14. What Is bootstrap/cache?

Laravel uses:

bootstrap/cache/

for generated framework cache files.

These files can be recreated by Laravel commands and should generally not be treated as application source code.

A common ignore rule is:

/bootstrap/cache/

15. Laravel Public Build Files

Modern Laravel applications commonly use Vite for frontend assets.

Depending on the project's build configuration, generated files may appear under:

public/build/

and development tooling may use:

public/hot

These are often ignored in projects where the deployment process builds assets separately.

However, this is deployment-specific. If your deployment process intentionally stores built assets in Git, do not add a rule that conflicts with that workflow.


16. IDE Files

Developers use different IDEs and editors.

For example:

.idea/
.vscode/

These directories may contain local workspace settings.

If your team does not intentionally share these settings through Git, they can be ignored.

Example:

/.idea/
/.vscode/

17. Operating System Files

Operating systems can create files that should not be committed.

Examples:

.DS_Store
Thumbs.db

You can add:

.DS_Store
Thumbs.db

to your ignore file.


18. PHPUnit Cache

Testing tools can generate local cache files.

For example:

.phpunit.result.cache

If your project's Laravel/PHPUnit setup generates this file, it is commonly ignored.


19. What About Composer.lock?

This is an important interview question.

For applications, composer.lock is generally valuable because it records the exact dependency versions used by the application.

Therefore, for a Laravel application, you generally commit:

composer.json
composer.lock

while ignoring:

vendor/

Don't add composer.lock to .gitignore simply because it is generated by Composer.


20. What About package-lock.json?

Similarly, if your project uses npm and the team relies on a lock file, you generally commit:

package-lock.json

while ignoring:

node_modules/

The same principle applies to other package managers and their lock files according to project conventions.


21. Gitignore Syntax Basics

Understanding Gitignore patterns is important.

Ignore a file

.env

Ignore a directory

node_modules/

Ignore from the repository root

/vendor/

The leading slash makes the pattern relative to the location of that .gitignore file.

Ignore all files with an extension

*.log

This can ignore log files matching that pattern.

Ignore a pattern anywhere

*.tmp

This matches matching temporary files according to Git's pattern rules.


22. Wildcards in .gitignore

The * wildcard can match multiple characters.

Example:

*.log

This can match:

debug.log
error.log
application.log

Another example:

*.cache

23. Ignoring a Directory

To ignore a directory:

node_modules/

or:

/node_modules/

The second form specifically anchors the directory at the repository root when the rule is in the root .gitignore.


24. Negation with !

Gitignore supports negation using !.

For example:

*.log
!important.log

This means:

  • Ignore files ending in .log
  • Do not ignore important.log when the pattern hierarchy allows Git to reach it

Negation rules can become tricky when parent directories are ignored, so test the pattern rather than assuming it behaves as intended.


25. Comments in .gitignore

Lines beginning with # are comments.

Example:

# Laravel dependencies
/vendor/

# Environment configuration
.env

Comments make a complex .gitignore easier for the team to maintain.


26. Example: A Clean Laravel .gitignore

Here is a practical example:

# Dependencies
/vendor/
/node_modules/

# Environment files
.env
.env.*
!.env.example

# Laravel generated files
/storage/framework/
/storage/logs/
/storage/*.key
/bootstrap/cache/

# Frontend build output
/public/build/
/public/hot

# Testing cache
.phpunit.result.cache

# IDE
/.idea/
/.vscode/

# OS files
.DS_Store
Thumbs.db

This is a starting point, not a universal template. Always adapt it to your project's build, deployment, and team requirements.


27. Should You Ignore the Entire storage Directory?

This requires some care.

Laravel's storage directory itself is part of the project structure, but many generated contents inside it should not be tracked.

A blanket rule such as:

/storage/

may be too broad for some projects because you may intentionally want to track specific directories or placeholder files.

Instead, use the project's generated Laravel rules and only customize them when necessary.


28. Why .gitkeep Is Sometimes Used

Git does not track empty directories.

Suppose your project expects:

storage/logs/

but there are no log files yet.

Some projects use a placeholder:

.gitkeep

For example:

storage/logs/.gitkeep

The placeholder itself is tracked while generated log files remain ignored.

The name .gitkeep is a convention, not a special Git feature.


29. Important: .gitignore Does Not Remove Tracked Files

This is one of the most important Git concepts.

Suppose you accidentally committed:

.env

Later you add:

.env

to .gitignore.

Git will not automatically remove the already-tracked file from the repository.

Why?

Because .gitignore primarily affects files Git is not already tracking.


30. How to Stop Tracking a File

Suppose .env is already tracked.

First add it to .gitignore:

.env

Then remove it from Git's index while keeping the local file:

git rm --cached .env

Then commit:

git add .gitignore
git commit -m "chore: stop tracking environment file"

The local .env remains on your machine, but it is removed from version control in the new commit.


31. Stop Tracking a Directory

Suppose node_modules/ was accidentally committed.

After adding it to .gitignore:

/node_modules/

remove it from Git's index:

git rm -r --cached node_modules

Then:

git add .gitignore
git commit -m "chore: stop tracking node modules"

The local directory remains on your machine.


32. Does .gitignore Protect Secrets Already Committed?

No.

This is extremely important.

If an API key or password has already been committed and pushed to a remote repository, simply adding it to .gitignore does not make the secret safe.

The appropriate response may include:

  • Revoking or rotating the exposed credential
  • Removing the secret from the current repository state
  • Assessing whether historical Git objects contain the secret
  • Following the organization's security incident process

If a secret has been exposed, treat it as compromised rather than assuming Gitignore fixes the problem.


33. .gitignore vs .env

These are different things.

.gitignore .env
Git configuration file Laravel environment configuration
Controls which untracked files Git ignores Stores environment-specific values
Usually committed Usually not committed

34. .gitignore vs .git/info/exclude

Git also supports:

.git/info/exclude

This can be useful for repository-local ignore rules that you don't want to share with other developers.

For example, if you have a personal temporary file:

notes-bikram.txt

you could ignore it locally using .git/info/exclude instead of changing the project's shared .gitignore.

This is useful when the ignore rule is personal rather than a project-wide convention.


35. Global Gitignore

Git can also use a global ignore file for files that should be ignored across your repositories.

For example, operating-system files such as:

.DS_Store

may be better handled globally on a developer's machine.

A global ignore file can prevent the same personal machine-generated files from appearing across many repositories.


36. Check Whether Git Is Ignoring a File

One useful troubleshooting command is:

git check-ignore -v .env

This can show which ignore rule is causing Git to ignore the path.

For example, you may discover that:

.env

is being ignored by:

 
.gitignore:5:.env
 

This is extremely useful when debugging complex ignore rules.

 

 

37. Why Is My File Not Being Ignored?

 

There are several possible reasons.

 

Reason 1 — The file is already tracked

 

Check:

 
git ls-files .env
 

If it appears, it is already tracked.

 

Reason 2 — The pattern is incorrect

 

Use:

 
git check-ignore -v path/to/file
 

Reason 3 — Another rule overrides it

 

Gitignore patterns can interact with other patterns and nested ignore files.

 

Reason 4 — You edited the wrong .gitignore

 

Remember that ignore rules can exist at different levels.

 

 

38. Laravel .gitignore Example with Explanation

 
# Composer dependencies
/vendor/
 

Ignore PHP packages installed by Composer.

 
# Node dependencies
/node_modules/
 

Ignore frontend dependencies.

 
# Environment
.env
.env.*
!.env.example
 

Ignore environment-specific files while keeping the example template.

 
# Laravel generated files
/storage/framework/
/storage/logs/
/storage/*.key
/bootstrap/cache/
 

Ignore generated runtime/cache content and local application keys as appropriate for the project.

 
# Vite build output
/public/build/
/public/hot
 

Ignore generated frontend artifacts when the deployment process builds them elsewhere.

 
# IDE
/.idea/
/.vscode/
 

Ignore editor-specific workspace settings when the team does not intentionally share them.

 

 

39. What Should Be Committed in a Laravel Project?

 

A typical Laravel repository should contain important source and configuration templates such as:

 
app/
bootstrap/
config/
database/
public/
resources/
routes/
tests/

composer.json
composer.lock

package.json
package-lock.json

.env.example
.gitignore

artisan
 

The exact tracked files depend on the project and Laravel version.

 

 

40. What Should Usually Not Be Committed?

 

Common examples include:

 
.env
/vendor/
/node_modules/
/storage/logs/
/storage/framework/
/bootstrap/cache/
/public/hot
IDE workspace files
OS-specific files
local caches
temporary files
 

Again, deployment and project-specific requirements can change which generated artifacts are intentionally tracked.

 

 

41. Gitignore and Bitbucket

 

Bitbucket does not change how Git's .gitignore rules fundamentally work.

 

If your Laravel repository is hosted on Bitbucket:

 
Local Laravel Project
        ↓
     .gitignore
        ↓
      git add
        ↓
     git commit
        ↓
     git push
        ↓
      Bitbucket
 

Files excluded by Gitignore are normally not included in the commit because Git does not stage them as untracked files.

 

 

42. Example — Bitbucket Laravel Workflow

 
# Create feature branch
git switch -c feature/payment

# Check files
git status

# Review ignored/untracked state
git status --ignored

# Stage project files
git add .

# Review staged changes
git diff --staged

# Commit
git commit -m "feat(payment): add payment feature"

# Push
git push -u origin feature/payment
 

Before pushing, verify that sensitive or generated files are not accidentally staged.

 

 

43. git status --ignored

 

Normally:

 
git status
 

does not show ignored files.

 

To see ignored files as well:

 
git status --ignored
 

This can help you understand whether Gitignore rules are working as expected.

 

 

44. Check Tracked Files

 

You can see files already tracked by Git with:

 
git ls-files
 

To check a specific file:

 
git ls-files .env
 

If the file is returned, Git is already tracking it.

 

 

45. Common .gitignore Mistakes

 

❌ Mistake 1 — Committing .env

 

This can expose credentials and environment-specific configuration.

 

❌ Mistake 2 — Ignoring composer.lock

 

For Laravel applications, this is generally undesirable because the lock file helps keep dependency versions consistent.

 

❌ Mistake 3 — Committing vendor

 

Normally, Composer dependencies should be installed from the dependency definitions rather than committed as the entire vendor tree.

 

❌ Mistake 4 — Committing node_modules

 

This can add a huge number of dependency files to the repository.

 

❌ Mistake 5 — Thinking .gitignore removes tracked files

 

It doesn't. Use git rm --cached when you need to stop tracking an already-tracked path while retaining it locally.

 

❌ Mistake 6 — Ignoring too much

 

A rule such as:

 
*
 

can effectively ignore almost everything and make the repository unusable unless carefully combined with negation rules.

 

❌ Mistake 7 — Ignoring required build artifacts

 

Some deployment workflows intentionally commit generated assets. Always understand the project's deployment process before adding ignore rules.

 

 

46. Best Practices for Laravel .gitignore

 
  •  
  • Start with the Laravel project's generated .gitignore.
  •  
  • Keep .env out of source control.
  •  
  • Commit .env.example without real secrets.
  •  
  • Ignore vendor/.
  •  
  • Ignore node_modules/.
  •  
  • Ignore generated logs and cache files.
  •  
  • Don't automatically ignore dependency lock files.
  •  
  • Ignore IDE files if the team doesn't share them.
  •  
  • Ignore OS-specific files where appropriate.
  •  
  • Review custom ignore rules with the team.
  •  
  • Use git check-ignore -v when troubleshooting.
  •  
  • Remember that Gitignore does not remove already-tracked files.
  •  
  • Never assume Gitignore can protect a secret that was already committed.
  •  
 

 

47. A Real Laravel Project Example

 

Imagine your Laravel project contains:

 
pharmacy-erp/
├── app/
├── bootstrap/
├── config/
├── database/
├── public/
├── resources/
├── routes/
├── storage/
│   ├── framework/
│   └── logs/
├── tests/
├── vendor/
├── node_modules/
├── .env
├── .env.example
├── composer.json
├── composer.lock
├── package.json
├── package-lock.json
└── .gitignore
 

A sensible Git repository would generally track:

 
app/
bootstrap/required project files
config/
database/
public/project source files
resources/
routes/
tests/

composer.json
composer.lock
package.json
package-lock.json
.env.example
.gitignore
 

while ignoring environment-specific and generated content according to the project's deployment process.

 

 

48. Interview Question — Why Do We Ignore vendor?

 

Because vendor/ contains Composer-installed dependencies that can be recreated from composer.json and composer.lock.

 

A fresh environment can run:

 
composer install
 

to install them.

 

 

49. Interview Question — Why Is .env Not Committed?

 

Because it commonly contains environment-specific configuration and potentially sensitive credentials such as database passwords, API keys, and application secrets.

 

A safe template such as .env.example can be committed instead.

 

 

50. Interview Question — Does .gitignore Remove a File Already Committed?

 

No.

 

If a file is already tracked, adding it to .gitignore does not automatically stop tracking it.

 

For example:

 
git rm --cached .env
 

Then commit the change.

 

 

51. Interview Question — How Do You Check Why a File Is Ignored?

 
git check-ignore -v path/to/file
 

This helps identify which ignore rule is responsible.

 

 

52. Interview Question — How Do You See Ignored Files?

 
git status --ignored
 

 

53. Interview Question — Should composer.lock Be Ignored?

 

For a typical Laravel application, composer.lock is normally committed so the team and deployment environments can install the intended dependency versions.

 

The policy can differ for some package/library repositories, so always consider the project type.

 

 

54. Interview Question — Should node_modules Be Committed?

 

Normally no.

 

The dependency definition and lock file are committed, and the dependencies are installed using the project's package manager.

 

 

55. Interview Question — What Is the Difference Between .gitignore and git rm --cached?

 
.gitignore git rm --cached
Defines paths Git should ignore Removes an already-tracked path from the Git index
Useful before a file is tracked Useful when a file was already committed/tracked
Does not automatically untrack existing files Can stop tracking while keeping the local file
 

 

56. Complete Laravel .gitignore Cheat Sheet

 
# Composer
/vendor/

# Node
/node_modules/

# Environment
.env
.env.*
!.env.example

# Laravel generated files
/storage/framework/
/storage/logs/
/storage/*.key
/bootstrap/cache/

# Vite
/public/build/
/public/hot

# PHPUnit
.phpunit.result.cache

# IDE
/.idea/
/.vscode/

# OS
.DS_Store
Thumbs.db
 

Use this as a reference and adapt it to your application's actual workflow.

 

 

57. Important Git Commands for .gitignore

 
Command Purpose
git status Check repository status
git status --ignored Show ignored files
git check-ignore -v file Find the rule ignoring a file
git ls-files List tracked files
git ls-files .env Check whether .env is tracked
git rm --cached .env Stop tracking .env while keeping it locally
git rm -r --cached node_modules Stop tracking node_modules while keeping it locally
 

 

58. Easy Way to Remember Laravel .gitignore

 
SOURCE CODE
    ↓
COMMIT

SECRETS
    ↓
IGNORE

DEPENDENCIES
    ↓
IGNORE GENERATED DIRECTORIES

LOCK FILES
    ↓
USUALLY COMMIT

LOGS
    ↓
IGNORE

CACHE
    ↓
IGNORE

IDE / OS FILES
    ↓
IGNORE WHEN NOT SHARED
 

The key rule is:

 
Commit the files needed to reproduce and develop the application; ignore local, generated, environment-specific, and sensitive files according to the project's workflow.
 

 

Conclusion

 

A properly configured .gitignore is essential for maintaining a clean Laravel Git repository.

 

The most important files and directories to understand are:

 
  •  
  • .env — environment-specific configuration and potentially secrets
  •  
  • .env.example — configuration template
  •  
  • vendor/ — Composer dependencies
  •  
  • node_modules/ — Node dependencies
  •  
  • storage/logs/ — application logs
  •  
  • storage/framework/ — generated framework data
  •  
  • bootstrap/cache/ — generated framework cache
  •  
  • public/build/ — generated frontend build output when not intentionally committed
  •  
  • IDE and OS files — local development artifacts
  •  
 

Also remember one critical point:

 
.gitignore ≠ remove already tracked files
 

If something is already tracked, you may need:

 
git rm --cached <file>
 

And if a secret has already been committed, treat it as exposed and rotate/revoke it rather than assuming that adding it to .gitignore fixes the security issue.

 

 

What's Next?

 

Now that we have covered .gitignore, the next useful Git topic for Laravel developers is how to label and manage production versions.

 

Next Article: Git Tags & Releases Explained

 

We will cover:

 
  •  
  • What is a Git tag?
  •  
  • Lightweight vs annotated tags
  •  
  • Creating version tags
  •  
  • Semantic versioning
  •  
  • Tagging Laravel releases
  •  
  • Pushing tags to Bitbucket
  •  
  • Listing and deleting tags
  •  
  • Checking out a specific release
  •  
  • Git tags vs branches
  •  
  • Production release workflow
  •  
  • Git tag interview questions
  •  
 

Was this useful?

No ratings yet — yours can be the first.

Discussion

Notes from readers. Comments appear after a short review.

No comments yet. Start the thread below.