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

Question

I have a project main that contains a submodule foo. For this particular project, I would like to make a small change to foo that only applies to this particular project main.

main/
  + .git
  + main.c
  + lib/
  |   + bar.c
  + foo/           # My `foo` submodule
      + .git
      + config.h   # The file I want to patch from `main`
      + ...

A common solution would be to go down to my submodule, make a commit Applied patch for main on a new branch called main-project, then push it. Unfortunately, this is a very bad approach because I am making changes to foo that only matters to main. Also, when I update foo to the latest version, I will have to cherry-pick the patch too which introduces a lot of noise in foo's history.

Another solution is to have a real patch file on main that is applied to foo just before the build. Unfortunately as this modifies the submodule content, and I will have uncommitted changed on foo, so it is not a good solution either.

The ideal solution would be to track my patch using Git, but at the top-level (e.g. directly on main, not on foo). Theoretically, it would be possible to add a blob on the Git tree that points into the submodule location:

blob   <sha> main.c
tree   <sha> lib/
commit <sha> foo
blob   <sha> foo/config.h

With this idea, the patched file config.h belonging to foo will be tracked on main.

How is it possible to do it so?


Show source
| git   | git-submodules   2017-11-29 22:11 1 Answers

Answers to Is it possible to patch a submodule in Git from the parent project? ( 1 )

  1. 2017-11-30 06:11

    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.

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

◀ Go back