Search code examples
gitgit-submodules

Are changes in a submodule committed to my local repo or to the repo the sobmodule was created from?


I read a couple of articles about submodules but I haven't figured this out yet.

Given this structure

+ my-repo
+ --- + somepkg
+ --- + submod

I made changes to submod which I would like to keep all nicely within myrepo. If I commit the changes in submod, then push - will the upstream repo from which I created the submodule be changed in any way or am I just keeping everything in my repo?

My targeted solution is that I can maintain my own copy of that submodule, ideally internally in my-repo. Maybe submodules aren't the best option here? I don't have rights upstream...so maybe I should first create a fork of that, then push there?


Solution

  • I made changes to submod which I would like to keep all nicely within myrepo.

    The think to understand here is: submodules are separate repositories. Their content is incorporated into the working tree but into the parent repository (called superproject).

    If I commit the changes in submod, then push - will the upstream repo from which I created the submodule be changed in any way

    No. If you don't have rights to push they simply refuse your push, git push returns an error.

    My targeted solution is that I can maintain my own copy of that submodule, ideally internally in my-repo.

    You are on the right track but not there yet. You cannot save changes from a submodule in the superproject.

    You need to fork the submod original repository to your own repo to which you have all the rights and change remote names. The traditional names are: upstream for the initial repo and origin for your fork. When you want to incorporate changes from the upstream you do git pull upstream. When you want to push you push to your fork: git push origin.

    In the superproject my-repo you also need to change .gitmodules: change submod URL to your fork. Also you need to add, commit and push any changes in submod because superprojects store the current commit reference (called "gitlink") for every submodule.