How To Create Patch For Fixing Bug On Vendor In Magento 2.X By Using Git.

Nimish Agarwal
5 min readJan 18, 2023

Step by step approach to create patch for fixing bugs in vendor in magento 2.x version by using git commands on local system.

Before moving to the method/approach of fixing vendor bugs by creating patch, We have to understand first what is patch?..

What is Patch?

A patch is a block of code or a file that consist the updated code inside it that helps to fix the issue by applying in it on the location that need to be fixed. The file is called patch!

The patch is applied automatically after updating composer.json file. We have to giving the path of patch and file name with providing the bug updated directory path inside composer.

Steps to create patch file for vendor in magento 2?

Important Note: Make sure to take a backup of updated code before start following this approach.

Step 1: Create a blank directory (folder) on any path in your system. (For Me, I created folder name “Blank_Folder”).

Fig 1: Created Blank_Folder on Desktop.

Step 2: Copy and paste your initial code file by following same directory structure inside your blank folder. (Make sure to follow or create same folder structure).

Fig 2: Updated File With Initial File.

For Example: I have to fix bug in elastic search inside vendor.

Original Path In Root Of Magento: (Make sure to replace this path with your path)

vendor/magento/module-elasticsearch-7/SearchAdapter

That’s consist some file name Adapter.php.

Create similar file path in your blank folder (Updated Path With File): (Make sure to follow same directory structure as original path is described.)

Blank_Folder/vendor/magento/module-elasticsearch-7/SearchAdapter

That’s consist same file name Adapter.php with similar initial code without modifying it.

Note: Above path is my path, on which i am facing issue inside vendor code, so make sure to replace it with yours path.

Step 3: Now, Open Terminal or Cmd and go to Blank_Folder directory path.

Fig 3: Terminal With Blank_Folder Directory.

Step 4: Then, Run below command (Copy and paste below command on your terminal or cmd)

git init

Description- This command initialise the git on that blank_folder directory.

Fig 4: Git Init Initialisation on local.

Step 5: Now, Run below command for adding directory and file for creating patch.

git add .

Description- The . [dot] is used to add the whole current directory item for making patch. so make sure to add . [dot] in your command

Fig 5: Run Command Git add .

Step 6: Now, Replace the code to the updated code and save it inside file that is place on “Blank_Folder/vendor/magento/module-elasticsearch-7/SearchAdapter” given path.

Step 7: After updated code and save it successfully, Run the below command to save it.

git commit -m “your_message”

Fig 6: Commit Changes.

Step 8: For checking the changes that is successfully updated or not. Run below Command.

git status

Fig 7: See the changes after run git status.

Step 9: Now, Run below command to create a patch file.

git diff > file_name_anything.patch

Fig 8: make file with any_name.patch extension

Important Note: Make sure to write .patch extension after file name in above command.

Patch Is Successfully Created!..

Fig 9: Patch File is Created.
Fig 10: See the file code inside the file. Changes can display correctly!

Applying Patch File Through Composer.json

Now, It was a time to apply the patch file using composer.json file for automatically apply changes in vendor using composer.

Step 1: Go to Magento Root Directory.

Step 2: Create a custom folder with any name. In my case, I create it by name is vendor_patch folder on magento root directory.

Step 3: Paste Your Patch_File inside your custom folder on root directory of magento.

OR

Optional: You can create sub-directory inside it and paste your Patch_File on that sub-directory.

Step 4: After Paste File successfully.

Step 5: Go to terminal or cmd and run below command.

composer require cweagans/composer-patches

Important Note: Make sure to install it first before moving on next step.

Step 6: Go to magento root directory again.

Step 7: Open Composer.json File (Must sure to take backup before edit the code).

Step 8: For applying patch, just change the below code of composer.json file that is situated in magento root directory by adding your patch details.

From Code:

“extra”: {
“magento-force”: “override”
}

Fig 11: Initial Composer.json file code before modifying.

To code:

“extra”: {
“magento-force”: “override”,
“composer-exit-on-patch-failure”: true, //If patch apply is fail then execution is not stopped.
“patches”: {
“vendor_name/module_name”: { // vendor_name/module_name: write on which you have to change inside vendor
“Patch_File_Name: Custom-Message-Here”: “Complete_Path/With_file_Name_of_Patch.With_Extension”
}
}
}

Fig 12: See Example How to Write in composer.json file (Must Read File Comments)

Above image is a example of “How To Write In Composer.json File”.

In My Case, below is the example of code inside coposer.json file after updated.

Fig 13: After Updated Composer.Json.

Step 9: Now, Comment (Rename) Vendor Folder to “Vendor-old” & Composer.lock file to “composer-old.lock”.

Step 10: Go to your magento root directory path in terminal or cmd.

Step 11: Run below command one by one each.

rm -rf generated/ var/cache/ var/page_cache/ pub/static/frontend var/view_preprocessed/

composer install

php bin/magento setup:upgrade

php bin/magento setup:static-content:deploy -f

php bin/magento indexer:reset

php bin/magento indexer:reindex

php bin/magento cache:flush

Patch Is Successfully Applied Using Composer (Now, No need to apply manually)!..

Thanks For Reading! : )

If You having any doubt and you are facing any issue in this complete process, please drop your comment or connect with me via mail: nimishagarwal1995@gmail.com.

--

--