{"id":1286,"date":"2024-06-07T19:20:36","date_gmt":"2024-06-07T16:20:36","guid":{"rendered":"https:\/\/plumrocket.com\/learn\/?p=1286"},"modified":"2025-04-01T19:21:18","modified_gmt":"2025-04-01T16:21:18","slug":"magento-2-gitignore-file-how-to-optimize-add-to-your-repo","status":"publish","type":"post","link":"https:\/\/plumrocket.com\/learn\/magento-2-gitignore","title":{"rendered":"Magento 2 Gitignore File: How to Optimize &#038; Add to Your Repo"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full disable_zoom\"><img loading=\"lazy\" width=\"1600\" height=\"600\" src=\"https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/08\/magento-2-gitignore-file.png\" alt=\"Magento 2 Gitignore File: How to Optimize &amp; Add to Your Repo\" class=\"wp-image-1530\" srcset=\"https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/08\/magento-2-gitignore-file.png 1600w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/08\/magento-2-gitignore-file-300x113.png 300w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/08\/magento-2-gitignore-file-1024x384.png 1024w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/08\/magento-2-gitignore-file-768x288.png 768w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/08\/magento-2-gitignore-file-1536x576.png 1536w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/08\/magento-2-gitignore-file-1568x588.png 1568w\" sizes=\"(max-width: 1600px) 100vw, 1600px\" \/><\/figure>\n\n\n\n<p>Most developers have found Git an integral tool when working on any software development project, large or small. This version control system helps keep track of all code changes and creates an effective environment for the team to work on the same project simultaneously.<\/p>\n\n\n\n<p>However, as the project grows, keeping the repository clean and tidy with thousands of files becomes difficult. And here\u2019s where the gitignore file comes into play.<\/p>\n\n\n\n<p>Generally, the .gitignore config allows you to specify the files and folders you do not want to add to the Git repository. These may include temporary and test files, logs, backups, copies of Magneto files, or folders specific to your environment.<\/p>\n\n\n\n<p>This tutorial describes a workflow for skipping unnecessary files and folders while storing the Magento 2 eCommerce platform in the Git repository. Please note that due to common rules, these instructions apply to any other system.<\/p>\n\n\n\n<div class=\"wp-block-cover has-background-dim\" style=\"background-color:#f0f6ff;min-height:150px\"><div class=\"wp-block-cover__inner-container\">\n<p class=\"has-text-align-center has-dark-gray-color has-text-color\">Managing large Magento repositories often requires specialized knowledge. Our expert team offers comprehensive <a href=\"\/magento-development\" target=\"_blank\" rel=\"noreferrer noopener\">development service<\/a> to handle complex configurations and ensure your version control strategy is robust from the start.<\/p>\n<\/div><\/div>\n\n\n\n<h2 style=\"font-size:26px\">Magento 2 Gitignore File Example<\/h2>\n\n\n\n<p>In the Magento 2 project, the gitignore file is placed in the Magento root directory. Here&#8217;s an example of what the .gitignore file format can look like:<\/p>\n\n\n\n<div class=\"wp-block-prismatic-blocks\"><div><\/div><pre><code class=\"language-bash\"># Ignore all folders\/files inside the \/vendor folder, except \/vendor\/.htaccess\n\/vendor\/*\n!\/vendor\/.htaccess\n\n# Ignore the package.json file inside the root directory \n\/package.json\n\n# Skip all ZIP archives in the Magento 2 root directory or any subdirectories\n*.zip<\/code><\/pre><\/div>\n\n\n\n<p>Here are some key points about .gitignore file format:<\/p>\n\n\n\n<ul><li>Lines starting with &#8220;#&#8221; are comments and ignored by Git.<\/li><li>You can use wildcards like &#8220;*&#8221; to match multiple characters and &#8220;?&#8221; for a single character. For example, a line *.txt would ignore all files ending with &#8220;.txt&#8221;.<\/li><li>To ignore entire directories, add a forward slash &#8220;\/&#8221; at the end of the pattern.<\/li><\/ul>\n\n\n\n<h2 style=\"font-size:26px\">How to Optimize Magento 2 Gitignore File<\/h2>\n\n\n\n<p>Prerequires:<\/p>\n\n\n\n<ul><li>Git tool, installed on your server or computer<\/li><li>Repository, where you plan to store the files (e.g., GitHub, GitLab, BitBucket)<\/li><li>SSH connection in case you work remotely with the server<\/li><li>Full backup of the Magento 2 files<\/li><\/ul>\n\n\n\n<h3 style=\"font-size:23px\">Step #1. Clean Unnecessary Magento files<\/h3>\n\n\n\n<p>Before you add the Magento files to Git, it\u2019s important to remove unnecessary files, especially if they are big or contain sensitive data. These files may include:<\/p>\n\n\n\n<ul><li>Temporary and test files (e.g., test.php, phpinfo.php, adminer.php)<\/li><li>Backups (e.g., \u201cfiles.zip\u201d, \u201cdb-dump.sql\u201d, \u201c*.php-bak\u201d files, and so on)<\/li><li>Logs outside of the \u201cvar\u201d directory<\/li><li>Copies of Magento files and folders. For example, sometimes, you can find a copy of the \u201capp\/etc\/env.php\u201d file or \u201cpub\/media\u201d folder<\/li><li>Other unused garbage<\/li><\/ul>\n\n\n\n<h3 style=\"font-size:23px\">Step #2. Enhance default Magento .gitignore file<\/h3>\n\n\n\n<p>Magento 2 contains the .gitignore out of the box. Here you can find the <a href=\"https:\/\/experienceleague.adobe.com\/en\/docs\/commerce-operations\/configuration-guide\/files\/config-reference-gitignore\" target=\"_blank\" rel=\"noreferrer noopener\">latest Magento Open Source gitignore file<\/a> and see what\u2019s included. It typically covers the most common files you wouldn\u2019t want to track in your Git repository, for example:<\/p>\n\n\n\n<ul><li>Composer \u201cvendor\u201d folder<\/li><li>Catalog images and others under the \u201cpub\/media\u201d directory<\/li><li>Caches, logs, and autogenerated files (\/var\/, \/generated\/, \/pub\/static\/*)<\/li><li>app\/etc\/env.php file, which contains sensitive data (e.g., database password)<\/li><\/ul>\n\n\n\n<p>However, your project-specific files must be manually added to Magento 2 gitignore to prevent any sensitive information about your custom modules or themes from being included in your repository.<\/p>\n\n\n\n<p>Please follow the 4 tips below to enhance and optimize your Magento 2 gitignore file.<\/p>\n\n\n\n<h4 class=\"padding-top: 15px\" style=\"font-size:20px\">1. Add more formats<\/h4>\n\n\n\n<div class=\"wp-block-prismatic-blocks\"><div><\/div><pre><code class=\"language-bash\"># Packages\n*.7z\n*.dmg\n*.gz\n*.iso\n*.jar\n*.rar\n*.tar\n*.zip\n*.tgz\n*.tar.gz\n\n# Logs and databases\nlog\/\n*.log\n*.sql\n*.sqlite\n\n# OS generated files\n.DS_Store\n.DS_Store?\nehthumbs.db\nIcon?\nThumbs.db\n._*\n.Spotlight-V100\n.Trashes\n\n# Exclude IDEs directories\n.idea\nnbproject<\/code><\/pre><\/div>\n\n\n\n<h4 style=\"font-size:20px\">2. Move subsystems to separate repositories<\/h4>\n\n\n\n<p>Sometimes, additional systems can be installed together with the Magento store. For example, a WordPress CMS can be installed in the \u201cblog\u201d subfolder.<\/p>\n\n\n\n<p>In this case, we recommend storing the Magento and WordPress files in separate Git repositories. Therefore, you should add the \u201c\/blog\u201d folder to the Magento .gitignore file.<\/p>\n\n\n\n<h4 style=\"font-size:20px\">3. Add large files<\/h4>\n\n\n\n<p>To ensure the efficient performance of repositories, Github imposes limitations related to file size and <a href=\"https:\/\/docs.github.com\/en\/repositories\/working-with-files\/managing-large-files\/about-large-files-on-github\">blocks files larger than 100 MB<\/a>. In order to prevent issues with pushing or pulling changes due to oversized files, you should add them to the gitignore file.<\/p>\n\n\n\n<p>To find and download the list of large files, you can use the following command:<\/p>\n\n\n\n<div class=\"wp-block-prismatic-blocks\"><div><\/div><pre><code class=\"language-bash\">find . -size +100M | cat >> large-files.txt<\/code><\/pre><\/div>\n\n\n\n<p>Additionally, it makes sense to review small files in the same way (e.g., 50MB or even 10MB). It can help you exclude unnecessary from Git.<\/p>\n\n\n\n<p>If you need to check the list of the largest folders and files, use the following command:<\/p>\n\n\n\n<div class=\"wp-block-prismatic-blocks\"><div><\/div><pre><code class=\"language-\">du -sh .\/* | sort -h<\/code><\/pre><\/div>\n\n\n\n<h4 style=\"font-size:20px\">4. Add autogenerated files<\/h4>\n\n\n\n<p>The base gitignore file, which is included in the Magento 2 root directory by default, already ignores standard autogenerated files, such as \/var\/, \/generated\/, \/pub\/static\/*, etc.<\/p>\n\n\n\n<p>However, your custom code or third-party extensions can also automatically generate some files. These include data feeds, exports, resized images, combined CSS\/JS assets, backups, sitemaps, etc. It is recommended to ignore such files to optimize your repository performance.<\/p>\n\n\n\n<h3 style=\"font-size:23px\">Step #3. Check files that will be added to the commit<\/h3>\n\n\n\n<p>The <code>git status<\/code> command displays files ready to be added and pushed to the Git repository. If the list of files is huge, it is recommended to save an output to the file using the command below and review the list.<\/p>\n\n\n\n<div class=\"wp-block-prismatic-blocks\"><div><\/div><pre><code class=\"language-bash\">git status > git-files.txt<\/code><\/pre><\/div>\n\n\n\n<p>Repeat the previous steps (#1 and #2) if the list still contains unnecessary files.<\/p>\n\n\n\n<h3 style=\"font-size:23px\">Step #4. Add an exception in composer.json<\/h3>\n\n\n\n<p>When you update Magento or set up a fresh dev copy, you run the following command:<\/p>\n\n\n\n<div class=\"wp-block-prismatic-blocks\"><div><\/div><pre><code class=\"language-bash\">composer install<\/code><\/pre><\/div>\n\n\n\n<p>This command downloads the PHP libraries (dependencies) into the \/vendor folder. During execution, it overrides Magento files in the root directory (e.g., .gitignore, .htacess, index.php, etc.).<\/p>\n\n\n\n<p>To prevent overriding and thus removing all changes you have made to the .gitignore file, add the following command in the \u201ccomposer.json\u201d config:<\/p>\n\n\n\n<div class=\"wp-block-prismatic-blocks\"><div><\/div><pre><code class=\"language-\">\"extra\": {\n    \"magento-force\": \"override\",\n    \"magento-deploy-ignore\": {\n        \"*\": [\n            \"\/.gitignore\"\n        ]\n    }\n},<\/code><\/pre><\/div>\n\n\n\n<h3 style=\"font-size:23px\">Step #5. Add files to gitignore<\/h3>\n\n\n\n<p>Finally, you can proceed with the standard Git workflow &#8211; add and commit files and push them to the Github repository.<\/p>\n\n\n\n<h2 style=\"font-size:26px\">Conclusion<\/h2>\n\n\n\n<p>Git is the necessary version control system for software developers, and keeping it clean of unnecessary files helps maintain its high performance and efficiency for future development and collaborations.<\/p>\n\n\n\n<p>Although Magento 2 gitignore file is included by default, it\u2019s important to optimize it and tailor it to your specific project. It excludes the most common files from tracking in your Git repository but doesn\u2019t cover files generated by your custom theme or extension.<\/p>\n\n\n\n<p>Using tips from this tutorial, you will optimize the default Magento 2 gitignore by adding your project-specific, large, auto-generated, and other files that should be excluded from tracking in your Git repository. This will result in a much cleaner Magento 2 store, optimize your current workflow, and facilitate future development. So, don\u2019t hesitate to implement these best practices to enjoy a more efficient development process.<\/p>\n\n\n\n<div class=\"wp-block-cover has-background-dim\" style=\"background-color:#f0f6ff;min-height:20px\"><div class=\"wp-block-cover__inner-container\">\n<p style=\"margin-bottom:-5px\" class=\"has-text-align-center has-dark-gray-color has-text-color\">If you need assistance or encounter any challenges, feel free to\u00a0<a href=\"\/contacts\" target=\"_blank\" rel=\"noreferrer noopener\">contact us<\/a>\u00a0for expert support!<\/p>\n<\/div><\/div>\n\n\n\n<p>P.S. GitHub has prepared a collection of useful .gitignore templates for many systems (including Magento, WordPress, Joomla, Laravel framework, etc.): <a href=\"https:\/\/github.com\/github\/gitignore\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/github.com\/github\/gitignore<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Most developers have found Git an integral tool when working on any software development project, large or small. This version control system helps keep track of all code changes and creates an effective environment for the team to work on the same project simultaneously. However, as the project grows, keeping the repository clean and tidy with thousands of files becomes difficult. And here\u2019s where the gitignore file comes into play.<\/p>\n<p>This tutorial describes a workflow for skipping unnecessary files and folders while storing the Magento 2 eCommerce platform in the Git repository. Due to common rules, these instructions apply to any other system.<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[121],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v16.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Magento 2 Gitignore File: How to Optimize &amp; Add to Your Repo<\/title>\n<meta name=\"description\" content=\"5 steps to create and optimize Magento 2 gitignore file to prevent all unnecessary files from being tracked by the Git repository.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/plumrocket.com\/learn\/magento-2-gitignore\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Magento 2 Gitignore File: How to Optimize &amp; Add to Your Repo\" \/>\n<meta property=\"og:description\" content=\"5 steps to create and optimize Magento 2 gitignore file to prevent all unnecessary files from being tracked by the Git repository.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/plumrocket.com\/learn\/magento-2-gitignore\" \/>\n<meta property=\"og:site_name\" content=\"Magento Tutorials for Beginners &amp; Professionals\" \/>\n<meta property=\"article:published_time\" content=\"2024-06-07T16:20:36+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-04-01T16:21:18+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/08\/magento-2-gitignore-file.png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"6 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/plumrocket.com\/learn\/wp-json\/wp\/v2\/posts\/1286"}],"collection":[{"href":"https:\/\/plumrocket.com\/learn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/plumrocket.com\/learn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/plumrocket.com\/learn\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/plumrocket.com\/learn\/wp-json\/wp\/v2\/comments?post=1286"}],"version-history":[{"count":19,"href":"https:\/\/plumrocket.com\/learn\/wp-json\/wp\/v2\/posts\/1286\/revisions"}],"predecessor-version":[{"id":2366,"href":"https:\/\/plumrocket.com\/learn\/wp-json\/wp\/v2\/posts\/1286\/revisions\/2366"}],"wp:attachment":[{"href":"https:\/\/plumrocket.com\/learn\/wp-json\/wp\/v2\/media?parent=1286"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/plumrocket.com\/learn\/wp-json\/wp\/v2\/categories?post=1286"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/plumrocket.com\/learn\/wp-json\/wp\/v2\/tags?post=1286"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}