Search code examples
gitgit-submodules

can I copy over local changes from a submodule


I have a project and a utility library. The project uses the utility library as a submodule. Works fine. But if I made a change to the library I have to commit it and push it to github. Then 'git submodule update ' in the main project.

Is there some way I can sync local changes without pushing (I manually copy at the moment)


Solution

  • You have several options.

    Directly make the change to the library in the submodule of the main project.

    Make the change in the library repo, and generate a patch. Apply the patch in the main project submodule.

    # If the change has been committed,
    cd /path/to/library
    git format-patch -1 $commit --stdout > patch.diff
    # If the change has not been committed,
    git diff > patch.diff    # add --cached if the changed file has been added.
    cd /path/to/main/project/submodule
    git apply -3 /path/to/library/patch.diff
    

    Make the change and commit it in the submodule, then fetch the commit and check it out in the main project submodule. You can also cherry-pick or merge the commit according to the actual situation.

    cd /path/to/main/project/submodule
    git fetch /path/to/library $branch
    git checkout $commit
    # git cherry-pick $commit
    # git merge $commit