Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Commit changes to a submodule without having to commit the parent repo

Let's say I have a parent repo myproject, and a separate repo called submodule, with the directory structure that's the following:


    root$ find . -not -path *.git*
    .
    ./myproject
    ./myproject/submodule

Now I add submodule as a submodule to myproject.


    root$ cd myproject
    myproject$ git submodule add git://url-to-submodule:submodule.git submodule
    Adding existing repo at 'submodule' to the index

Now, let's say i change something to submodule.


    myproject$ cd submodule
    submodule$ touch herpin.txt
    submodule$ add herpin.txt
    submodule$ git commit -am "i'm herpin and i'm derpin"

At this point, I go back to the parent repository, and check the git status:


    submodule$ cd ..
    myproject$ git status
    # On branch master
    # Changes not staged for commit:
    #   (use "git add ..." to update what will be committed)
    #   (use "git checkout -- ..." to discard changes in working directory)
    #
    #   modified:   submodule (new commits)
    #
    no changes added to commit (use "git add" and/or "git commit -a")

Well, damn it -- now each time I commit something in submodule, I also have to commit the parent.

It gets annoying pretty quickly if you have a more complex submodule tree. Let's say -- 4-levels deep. If I make a change at the inner-most submodule, I have to commit its parent, its grandparent, its great-grandparent, and its great-great-parent. That's a freaking pain in the ---.

There must be a better way! (And no, not nesting so many levels isn't an option. :/ That's not my call to make ...) Isn't there a way where git-commit can notify the parent repositories of the commit?

like image 257
ryanrhee Avatar asked Jan 20 '26 02:01

ryanrhee


1 Answers

Committing to a submodule should be treated like committing to another external library / repository.

Don't use git status on the root, use it on the submodule.

No you don't need to commit the changes in the container, you only need to commit them from within the sub module.

You can then make several changes, roll back, keep the submodule on a stable branch etc, and then only update the parent container to a stable version of the submodule.

Updating a submodule reference shouldn't be done constantly, and should only really swing between compatible versions, otherwise you will have a ton of 'bump' commits in a row that's just updating the sub module as you found out.

So in short, do your work in a submodule, commit, do more work in a submodule commit. Then once done, update the submodule reference to either the new version by committing the container, or rolling back the submodule to the last stable version, leaving the commits for future dev work.

like image 184
Ryan Leach Avatar answered Jan 21 '26 21:01

Ryan Leach



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!