Skip to content

Instantly share code, notes, and snippets.

@texboy
Last active January 8, 2025 12:38
Show Gist options
  • Save texboy/538106e095bd526beda539d68ec5c2be to your computer and use it in GitHub Desktop.
Save texboy/538106e095bd526beda539d68ec5c2be to your computer and use it in GitHub Desktop.
How to apply git patches using composer on a magento 2 project.

Introduction

Patches are great for small bugfixes since they're easy to create and maintain. You should follow magento's official documentation to install the required composer plugin. Don't forget to add the required patch section on your composer.json or composer will not be able to see your patches.

Getting started

Create a repo to start the creation of a patch.

  1. Go inside the folder that the file you need to fix is.
  2. Execute the git init . command.
  3. Execute the git add -A . command.
  4. Commit something. Ex: git commit -m "start of patch"

After modifying the file that you need to patch you should add (git add) and commit (git commit) the changes.

Creating the patch based on your commit

After commiting your changes you must generate a new patch. You can do that by using this command: git format-patch -1 HEAD where -<n> is the number of commits to be included in the patch and HEAD can be changed to any previous commit sha hash.

Adding the correct module path to the patch.

When the patch is generated by git, the resulting file will not have the correct paths to the file due to the way in wich we created the repository. After creating a patch, you sould open it with a text editor (vim or emacs) and change only the paths tha are preceded by the letter "a" or "b". Ex:

 diff --git a/File.php b/File.php
 index 6fb4944..f49ea5d 100644
 --- a/File.php
 +++ b/File.php

Should be changed to

 diff --git a/vendor/vendor-name/module-name/path/to/File.php b/vendor/vendor-name/module-name/path/to/File.php
 index 6fb4944..f49ea5d 100644
 --- a/vendor/vendor-name/module-name/path/to/File
 +++ b/vendor/vendor-name/module-name/path/to/File

Testing the patch

You should remove all the changes that you made to the file and remove the git repository that we created on the beginning (rm -rf .git on the folder that you created the patch). After that, you should move your patch to a folder at the root of your project (i use the magento cloud default one called m2-hotfixes).

  1. cd root/of/your/project
  2. execute the following command git apply m2-hotfixes/path-to-patch.patch If everything went well, you should be abble to see the changes on the target file that you created the patch for.

Adding the patch to your composer.json

Now that you know that your patch works, you should add it to your composer.json file to autommatically install it when you run the composer install command.

  1. Install the patch composer module by executing this command composer require cweagans/composer-patches
  2. Open the composer.json file on a text editor
  3. at the end of the file add the following:
   "extra": {
     "composer-exit-on-patch-failure": true,
     "patches": {
         "vendor-name/module-name": {
             "brief description": "m2-hotfixes/file.patch"
         }
     } 
   }
  1. Run the "composer install" command and watch the magic happen ;D
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment