Home > Mobile >  Upload changes to git submodule automatically
Upload changes to git submodule automatically

Time:07-17

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.

CodePudding user response:

to add [things in] submodules, I need to cd to those folders and git add from there.

You not only need to git add, you also need to git commit. You should then take care to git push at the right time (though recent versions of Git can arrange to do this from the superproject; see the --recurse-submodules option to git push).

This is quite tedious to do it manually. Is there a way to automate this process?

Of course there is—but automating commits is usually a bad idea, as there's no way to automate the production of a good commit message. So don't do that.

Add-and-commit within the submodule should not be tedious. It can consist of:

git switch <branch>  # Use the correct branch.
git add -u           # Add all updated files.
git status           # Make sure the list looks right and
                     # if necessary, add *new* files here.
git diff --cached    # Read through the proposed new commit.
                     # Verify that everything you want is in it,
                     # that nothing you *don't* want is in it, and
                     # that it makes sense as a single atomic
                     # commit.  If not, break it into as many
                     # atomic commits as are appropriate.
git commit           # Write a good commit message, and commit.

(Note that the commit message you intend to use for the superproject is quite likely to be inappropriate for the submodule, and vice versa.)

You can use Git's aliases to combine some of these, but each git switch should only be used once here, git add might need to be used multiple times to make multiple commits, and git status, git diff with or without --cached, and git commit should be used as often as needed to make good atomic commits. If you over-added things you may need to git reset or git restore, and you might want to use git add -p and so on. So it's pretty rare to have a situation in which it's sensible to do all of this as one operation in the first place.

CodePudding user response:

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>"

  • Related