Is it possible to patch a submodule in Git from the parent project?

The simplest way to carry project-specific patches on a vendor history is to clone the vendor repo, carry the changes as a project branch and advertise that clone as the .gitmodules upstream.

This makes work on your changes to the vendor upstream project perfectly ordinary, git clone --recurse-submodules yourproject works fine, your-submodule changes can be pushed back to the your-project-submodule upstream (the submodule repo's origin remote), everything works.

The only additional fillip is, to update your project's version of the submodule to the latest vendor code somebody has to fetch and merge from the (further-upstream) vendor repo

... but that's also perfectly ordinary: the way to fetch and merge from the vendor repo is, do it. git remote add vendor u://r/l; git fetch vendor; git merge vendor/master. Or if you prefer rebase to merge, do that. Once you've done that, push the results to your submodule's origin, your project's version, all as usual.


I would still go with the second option (have a real patch file on main), but adapt my build process to:

  • make a copy of the config.h in the submodule
  • apply the patch
  • build
  • restore config.h to its original content.

That way, I keep the submodule status unchanged.

The OP adds in the comments:

But your solution is not working in a IDE, Intellisense will be confused –

True: for that, I would apply automatically the patch on checkout, and remove it on checking, through a smudge/clean content filter driver.
That way, the patch remains in place during the all session, but would disappear on any git status/diff/checkin.

This is not ideal though, and there does not seem to be a native Git way to handle this.