Search code examples
windowsgitbatch-filegithubgit-submodules

Upload changes to git submodule automatically


I want to push all of the changes in my submodule to my main repo. When running > git add *, it adds all of the changes in my main repo and not the submodules.

> git add *

> git status
On branch main
Your branch is up to date with 'origin/main'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   Limux/vendor/stb

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
  (commit or discard the untracked or modified content in submodules)
        modified:   Limux/vendor/assimp (modified content)

to add these submodules, I need to cd to those folders and git add from there. This is quite tedious to do it manually. Is there a way to automate this process? Maybe use something like bat commands or a git command that I don't know about? I am using a windows operating system so please give an answer that works for windows.


Solution

  • We need to add and commit changes for each and every one of our submodules. Therefore, we can use this command:

    > git submodule foreach --recursive.

    the --recursive tells git to loop through each submodule and the submodules that each one can contain. then we add:

    > git diff --quiet && git diff --staged --quiet || git commit -am <message>

    at the end. the git diff --quiet && git diff --staged --quiet || tells git to run our next command if and only if there are changes to commit.

    Here's the full command:

    > git submodule foreach --recursive "git diff --quiet && git diff --staged --quiet || git commit -am <message>"