繁体   English   中英

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

[英]Upload changes to git submodule automatically

我想将子模块中的所有更改推送到我的主仓库。 运行> 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)

要添加这些子模块,我需要cd到这些文件夹并从那里 git add 。 手动执行此操作非常乏味。 有没有办法自动化这个过程? 也许使用我不知道的 bat 命令或 git 命令之类的东西? 我使用的是windows操作系统,所以请给出适用于windows的答案。

要添加 [things in] 子模块,我需要 cd 到这些文件夹并从那里 git add 。

您不仅需要git add ,还需要git commit 然后,您应该注意在正确的时间进行git push (尽管最新版本的 Git 可以安排从超级项目中执行此操作;请参阅git push--recurse-submodules选项)。

手动执行此操作非常乏味。 有没有办法自动化这个过程?

当然有——但是自动化提交通常是一个坏主意,因为没有办法自动化生成一个好的提交消息 所以不要那样做。

子模块中的添加和提交不应该是乏味的。 它可以包括:

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.

(请注意,您打算用于超级项目的提交消息很可能不适合子模块,反之亦然。)

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

我们需要为每个子模块添加和提交更改。 因此,我们可以使用这个命令:

> git submodule foreach --recursive

--recursive告诉 git 循环遍历每个子模块以及每个子模块可以包含的子模块。 然后我们添加:

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

在最后。 git diff --quiet && git diff --staged --quiet || 当且仅当有更改要提交时,告诉 git 运行我们的下一个命令。

这是完整的命令:

> 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