How to Create a Magento 2 Gitignore File

How to Create a Magento 2 Gitignore File

A Gitignore File contains patterns of intentionally untracked files.

While working on a Magento project, you deal with thousands of lines of code using Git.

Gitignore ensures you can ignore specific files, so they are not tracked.

We cover what a Gitignore file is and how to create a Magento 2 gitignore file.

Best Magento Hosting now

What is Git?

Git is an open-source distributed version control system. It is used to track changes in any set of files.

Git allows you to collaborate during software development and work efficiently on the source code.

It helps developers on small or large projects such as static sites or complex ERP solutions.

What is Magento 2 Gitignore

What is Magento 2 Gitignore

Magento gitignore ensures that specific files or directories remain untracked by Git.

The following command is used to un-track the files by removing them from the index.

The file is still present but on the next Git pull, it is untracked.

git rm --cached.

The files that are already tracked by Git are not affected.

What is the Magento 2 Gitignore Pattern?

Magento 2 Gitignore Pattern Mastery

7 Essential Rules for Clean Repositories

Comment
#

Comment Pattern

Marks explanatory text in gitignore files. Requires backslash escape for literal # characters.

# This is a comment
\#not-a-comment.txt
Separator
[ ]

Blank Line Separator

Empty lines improve readability between pattern groups in gitignore files.

pattern1.txt

pattern2.txt
Escape
\

Trailing Spaces

Ignored by default unless escaped with backslash for literal space matching.

file.txt # spaces ignored
file.txt\ \ # spaces kept
Include
!

Negation Pattern

Re-includes previously excluded files. Critical for Magento security files.

/media/*
!/media/.htaccess
Path
/

Directory Separator

Controls pattern matching scope based on position (start/middle/end).

/build/ # root only
build/ # any level
Recursive
**

Wildcard Pattern

Matches across directory levels for flexible path patterns in Magento.

**/vendor
**/cache/*.xml
Contents
/**

Trailing Wildcard

Matches all files within a directory recursively. Essential for bulk exclusions.

/generated/**
/pub/static/**

Pro Tip

Combine these patterns to create sophisticated ignore rules that keep your Magento 2 repository lean while preserving critical configuration and security files.

A few ways that help understand the Magento 2 gitignore pattern:

  • A line starting with # represents a comment. Add a backslash () in front of the first # for the patterns that begin with a hash.

  • A blank line with no match files is a separator for readability.

  • Trailing spaces are ignored by default. It is useless until you won’t add them with a backslash ().

  • The prefix “!” negates the gitignore pattern. It adds any previously matching file that was excluded by a previous pattern.

  • Slashes (/) is used as directory separators. It is placed at the starting, middle, or end of your gitignore pattern.

  • Two consecutive asterisks (** ) are used in gitignore patterns to match against the full pathname. For example, “**/abcd” will match the file or directory “abcd” regardless of the full pathname.

  • Use two consecutive asterisks (**) preceded by a slash (/) and followed by a slash (/) to match zero or more than zero directories.

  • Leaving trailing asterisks (/**) at the end helps match all files in the directory.

  • The two consecutive asterisks (**) in patterns matched against full pathname.

  • A trailing (/**”) helps match all files inside the parent directory.

Magento 2 Gitignore File

Anatomy of Magento 2 Gitignore: 55 Patterns Across 6 Critical Categories

Understanding the 40% media dominance in version control exclusions

Media & Upload Files 22 entries
40%

Largest category - includes product images, customer uploads, wysiwyg content

IDE & Editor Configs 10 entries
18%

Development environment files that vary per developer

System & Config Files 8 entries
15%

Critical system files containing sensitive data

Dependencies & Packages 6 entries
11%

Third-party code and build dependencies

Generated & Cache Files 5 entries
9%

Compiled and cached content

Build & Test Files 4 entries
7%

Development and testing artifacts

KEY

Media Files Dominate Repository Exclusions

The dominance of media-related entries (40% of all patterns) reflects the reality of e-commerce development where product catalogs, customer uploads, and WYSIWYG editor content can quickly consume gigabytes of storage. These files are environment-specific and should be managed through proper backup strategies rather than version control.

Common Patterns by Category

Media
/pub/media/*.*
IDE
/.vscode, /.idea
Config
/app/etc/env.php

A Magento gitignore file contains the patterns of files used to un-track useless directories.

Gitignore for Magento is also published with the source on Github.

To see the list of all ignored files, run the command open.gitignore on Mac or start.gitignore on PC.

Magento 2 Media Security Matrix

12 Directories, 9 Security Checkpoints

12
MEDIA SUBDIRECTORIES
9
SECURITY CHECKPOINTS
1

DIRECTORY SCAN

Identify media folders

2

SECURITY CHECK

Verify .htaccess rules

3

VERSION CONTROL

Apply gitignore rules

Directory Security Status

catalog/ SECURED
customer/ SECURED
wysiwyg/ SECURED
import/ SECURED
theme_custom/ SECURED
tmp/ SECURED
downloadable/ SECURED
captcha/ SECURED
email/ SECURED
analytics/ IGNORED
favicon/ IGNORED
logo/ IGNORED
KEY PATTERN

Dual-Pattern Security Approach

Each secured directory follows the pattern: ignore all content with /directory/* then explicitly preserve security with !/directory/.htaccess. This ensures security configurations remain consistent across all environments while keeping repositories lean.

You can choose to ignore the following files:

  • /pub/media/.
  • /sitemap
  • /node_modules
  • /package.json
  • /pub/static/*

When making changes on Magento Website, you might have to check if these files are on this list or not.

If the files are present, you have to update those files with Git Commit.

/.buildpath
/.cache
/.metadata
/.project
/.settings
/.vscode
atlassian*
/nbproject
/robots.txt
/pub/robots.txt
/sitemap
/sitemap.xml
/pub/sitemap
/pub/sitemap.xml
/.idea
/.gitattributes
/app/config_sandbox
/app/etc/config.php
/app/etc/env.php
/app/code/Magento/TestModule*
/lib/internal/flex/uploader/.actionScriptProperties
/lib/internal/flex/uploader/.flexProperties
/lib/internal/flex/uploader/.project
/lib/internal/flex/uploader/.settings
/lib/internal/flex/varien/.actionScriptProperties
/lib/internal/flex/varien/.flexLibProperties
/lib/internal/flex/varien/.project
/lib/internal/flex/varien/.settings
/node_modules
/.grunt
/Gruntfile.js
/package.json
/.php_cs
/.php_cs.cache
/grunt-config.json
/pub/media/*.*
!/pub/media/.htaccess
/pub/media/attribute/*
!/pub/media/attribute/.htaccess
/pub/media/analytics/*
/pub/media/catalog/*
!/pub/media/catalog/.htaccess
/pub/media/customer/*
!/pub/media/customer/.htaccess
/pub/media/downloadable/*
!/pub/media/downloadable/.htaccess
/pub/media/favicon/*
/pub/media/import/*
!/pub/media/import/.htaccess
/pub/media/logo/*
/pub/media/custom_options/*
!/pub/media/custom_options/.htaccess
/pub/media/theme/*
/pub/media/theme_customization/*
!/pub/media/theme_customization/.htaccess
/pub/media/wysiwyg/*
!/pub/media/wysiwyg/.htaccess
/pub/media/tmp/*
!/pub/media/tmp/.htaccess
/pub/media/captcha/*
/pub/static/*
!/pub/static/.htaccess
/var/*
!/var/.htaccess
/vendor/*
!/vendor/.htaccess
/generated/*
!/generated/.htaccess
.DS_Store

EndNote

Git allows developers to track all events within the modules and files containing thousands of code lines.

It helps developers be on the same page while working on a Magento project.

Magento gitignore ensures that specified files are ignored or not tracked by Git. We have covered the steps to set up a Magento gitignore file.

To get more insights on the Magento platform, check out MGT Commerce Tutorials.

Nikita S.
Nikita S.
Lead Technical Writer

As a lead technical writer, Nikita S. is experienced in crafting well-researched articles that simplify complex information and promote technical communication. She has expertise in cloud computing and holds a specialization in SEO and digital marketing.


Get the fastest Magento Hosting! Get Started