Best practices for using Git with Magento?

Best practices for using Git with Magento?

July 17, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Best practices for using Git with Magento?

Working Effectively with Magento and Git: Best Practices

Managing custom code alongside vendor libraries like Magento can be a complex challenge when working with Git. Your concerns about integrating your project within the Magento structure without pushing patches are valid. Let’s explore some best practices for handling this situation.

Understanding Git Submodule vs. Git Subtree

You’re correct that using git submodule might not work well in this case due to the deep integration of your custom code with Magento’s core files. The assumption behind a submodule is that it represents a separate, self-contained project structure. However, in your scenario, you need to treat Magento as the primary framework rather than a standalone module.

Why Git Subtree Might Not Be Ideal

You’re right again about the limitations of git subtree. This feature expects the vendor branch to be a subtree of the main project, which is not the case with Magento. The core library is not just a subtree; it’s the foundation upon which your custom code is built.

A more suitable approach for your use case is using multiple worktrees within one repository. This allows you to keep your custom code separate from the vendor’s, while still being able to integrate with the vendor’s library when needed.

Here’s how you can achieve this:

  1. Create a new worktree in your Magento directory:

    git worktree add -b local-magento /path/to/magento
    
  2. Switch to the new worktree:

    cd /path/to/magento
    git checkout local-magento
    
  3. Make changes and commit in this isolated environment:

    git add your-modified-file.php
    git commit -m "Your custom change"
    
  4. Push the changes to the remote repository:

    git push origin local-magento
    

This approach allows you to keep your custom code separate from Magento’s core files, ensuring that your changes don’t accidentally get overwritten during updates.

Using a Mirror Repository

Your final option of using a mirrored repository is also viable. You can pull the latest Magento version as a tarball and apply your custom patches on top of it. This method preserves the vendor’s commit history while keeping your changes separate.

For a more detailed guide on managing Git with large frameworks like Magento, check out our comprehensive tutorial at Laravel Company.

Conclusion

When working with complex frameworks like Magento and Git, it’s crucial to choose the right tool for the job. While git submodule and git subtree have their uses, they might not be the best fit for integrating custom code within a vendor’s core library.

Using multiple worktrees or a mirrored repository provides a more stable and manageable solution. It allows you to focus on your custom development while ensuring that updates from the vendor don’t interfere with your project.

Database Security Extensions

Our network