简体   繁体   English

自动将更改上传到 git 子模块

[英]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 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.要添加这些子模块,我需要cd到这些文件夹并从那里 git add 。 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?也许使用我不知道的 bat 命令或 git 命令之类的东西? I am using a windows operating system so please give an answer that works for windows.我使用的是windows操作系统,所以请给出适用于windows的答案。

to add [things in] submodules, I need to cd to those folders and git add from there.要添加 [things in] 子模块,我需要 cd 到这些文件夹并从那里 git add 。

You not only need to git add , you also need to git commit .您不仅需要git add ,还需要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 ).然后,您应该注意在正确的时间进行git push (尽管最新版本的 Git 可以安排从超级项目中执行此操作;请参阅git push--recurse-submodules选项)。

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.你可以使用 Git 的别名来组合其中的一些,但是每个git switch在这里只能使用一次, git add可能需要多次使用才能进行多次提交,以及git statusgit diff有或没有--cached ,以及git commit应该根据需要经常使用以进行良好的原子提交。 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.如果你添加了过多的东西,你可能需要git resetgit restore ,你可能想使用git add -p等等。 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.因此,在这种情况下,将所有这些首先作为一个操作来做是明智的,这种情况非常罕见。

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 . > git submodule foreach --recursive

the --recursive tells git to loop through each submodule and the submodules that each one can contain. --recursive告诉 git 循环遍历每个子模块以及每个子模块可以包含的子模块。 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 || git diff --quiet && git diff --staged --quiet || tells git to run our next command if and only if there are changes to commit.当且仅当有更改要提交时,告诉 git 运行我们的下一个命令。

Here's the full command:这是完整的命令:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM