簡體   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