Composer install command overides .gitignore file, Adobe Commerce

This article provides a solution for when a tracked .gitignore file is overriden by composer on Adobe Commerce on cloud infrastructure 2.4.2-p1 and 2.3.7.

Affected products and versions

Adobe Commerce on cloud infrastructure 2.4.2-p1 and 2.3.7.

Issue

.gitignore file is being overwritten when running composer install command.

Steps to reproduce:

  1. Create an empty directory for your workspace.

  2. Run this command in the root directory:

    code language-bash
    composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition:2.4.2-p1.
    

    # or 2.3.7

  3. Then run the following commands:

    1. echo "/this/line/should/stay" >> .gitignore

    2. git init

    3. git add * && git add .*

    4. git commit -m "Init" # file commited to repo

    5. rm -rf vendor/*

    6. composer install

    7. git diff

      code language-git
      diff --git a/.gitignore b/.gitignore
      index c144521..7092a56 100644
      --- a/.gitignore
      +++ b/.gitignore
      @@ -70,4 +70,3 @@ atlassian*
      /generated/*
      !/generated/.htaccess
      .DS_Store
      -/this/line/should/stay
      

Expected result:

.gitignore is not overriden by composer.

Actual result:

.gitignore is overriden by every composer install run.

Solution

To keep your custom .gitignore file you need to ignore it in the magento-deploy-ignore section.

{
...
"extra": {
    "magento-deploy-ignore": {
        "*": [
            "/.gitignore"
        ]
    }
    ...
}
recommendation-more-help
8bd06ef0-b3d5-4137-b74e-d7b00485808a