提交对子模块的更改,而无需提交父存储库

发布于 2025-01-02 14:55:08 字数 1270 浏览 0 评论 0原文

假设我有一个父存储库 myproject 和一个名为 submodule 的单独存储库,其目录结构如下:

    root$ find . -not -path *.git*
    .
    ./myproject
    ./myproject/submodule

现在我将 submodule 添加为myproject 的子模块。

    root$ cd myproject
    myproject$ git submodule add git://url-to-submodule:submodule.git submodule
    Adding existing repo at 'submodule' to the index

现在,假设我对子模块进行了一些更改。

    myproject$ cd submodule
    submodule$ touch herpin.txt
    submodule$ add herpin.txt
    submodule$ git commit -am "i'm herpin and i'm derpin"

此时,我返回父存储库,并检查 git 状态:

    submodule$ cd ..
    myproject$ git status
    # On branch master
    # Changes not staged for commit:
    #   (use "git add ..." to update what will be committed)
    #   (use "git checkout -- ..." to discard changes in working directory)
    #
    #   modified:   submodule (new commits)
    #
    no changes added to commit (use "git add" and/or "git commit -a")

好吧,该死——现在每次我在子模块中提交某些内容时,我也必须提交父存储库。

如果你有一个更复杂的子模块树,它很快就会变得烦人。比方说 - 4 层深。如果我在最里面的子模块进行更改,我必须提交它的父模块、祖父母、曾祖父母和曾曾父母。那是一种令人毛骨悚然的痛苦——。

一定有更好的办法! (不,不嵌套这么多级别不是一个选项。:/ 这不是我的调用...)是否有一种方法可以让 git-commit 通知父存储库提交?

Let's say I have a parent repo myproject, and a separate repo called submodule, with the directory structure that's the following:


    root$ find . -not -path *.git*
    .
    ./myproject
    ./myproject/submodule

Now I add submodule as a submodule to myproject.


    root$ cd myproject
    myproject$ git submodule add git://url-to-submodule:submodule.git submodule
    Adding existing repo at 'submodule' to the index

Now, let's say i change something to submodule.


    myproject$ cd submodule
    submodule$ touch herpin.txt
    submodule$ add herpin.txt
    submodule$ git commit -am "i'm herpin and i'm derpin"

At this point, I go back to the parent repository, and check the git status:


    submodule$ cd ..
    myproject$ git status
    # On branch master
    # Changes not staged for commit:
    #   (use "git add ..." to update what will be committed)
    #   (use "git checkout -- ..." to discard changes in working directory)
    #
    #   modified:   submodule (new commits)
    #
    no changes added to commit (use "git add" and/or "git commit -a")

Well, damn it -- now each time I commit something in submodule, I also have to commit the parent.

It gets annoying pretty quickly if you have a more complex submodule tree. Let's say -- 4-levels deep. If I make a change at the inner-most submodule, I have to commit its parent, its grandparent, its great-grandparent, and its great-great-parent. That's a freaking pain in the ---.

There must be a better way! (And no, not nesting so many levels isn't an option. :/ That's not my call to make ...) Isn't there a way where git-commit can notify the parent repositories of the commit?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

遮云壑 2025-01-09 14:55:08

提交到子模块应该被视为提交到另一个外部库/存储库。

不要在根模块上使用 git status,而在子模块上使用它。

不,您不需要在容器中提交更改,您只需从子模块内提交它们。

然后,您可以进行多项更改、回滚、将子模块保留在稳定分支上等,然后仅将父容器更新为子模块的稳定版本。

更新子模块引用不应该经常进行,而应该只在兼容版本之间切换,否则您将连续出现大量“碰撞”提交,而这些提交只是您发现的更新子模块。

简而言之,在子模块中完成工作,提交,在子模块提交中完成更多工作。完成后,通过提交容器将子模块引用更新为新版本,或者将子模块回滚到最后一个稳定版本,将提交留给将来的开发工作。

Committing to a submodule should be treated like committing to another external library / repository.

Don't use git status on the root, use it on the submodule.

No you don't need to commit the changes in the container, you only need to commit them from within the sub module.

You can then make several changes, roll back, keep the submodule on a stable branch etc, and then only update the parent container to a stable version of the submodule.

Updating a submodule reference shouldn't be done constantly, and should only really swing between compatible versions, otherwise you will have a ton of 'bump' commits in a row that's just updating the sub module as you found out.

So in short, do your work in a submodule, commit, do more work in a submodule commit. Then once done, update the submodule reference to either the new version by committing the container, or rolling back the submodule to the last stable version, leaving the commits for future dev work.

动听の歌 2025-01-09 14:55:08

当您对子模块进行更改时,您可以将未提交的更改推送到远程存储库,而无需触摸父项目,如下所示:

  • 将目录从根项目更改为子模块。 cd 子模块
  • 存储您对子模块所做的未提交的更改。 git stash
  • 拉取您想要应用更改的分支。 git pull;
  • 签出分支。 git checkout
  • 弹出存储。 git stash pop
  • 像往常一样提交未提交的更改。 git add, git commit
  • 将提交的更改推送到远程存储库。 git Push <远程名称>; <分支名称>

现在您已在本地子模块中提交了更改并将它们推送到远程存储库。在您的主项目中,.gitmodules 应更新并指向您在子模块目录中所做的最后一次提交。现在,您可以选择提交 .gitmodules 文件以使用包含更新子模块的提交,也可以放弃并继续使用旧的且未更新的子模块。但是,如果您放弃并运行 git submodule update --remote ,它将提取旧的未更新分支,因此您可能需要在主项目中提交此 .gitmodules 文件以及。

这是 Pro Git 书中关于它的一章: https://git -scm.com/book/en/v2/Git-Tools-Submodules

When you make changes to the submodule, you can push your uncommited changes to the remote repository without touching your parent projects as following:

  • Change directory to your submodule from your root project. cd submodule.
  • Stash uncommited changes you have made to your submodule. git stash
  • Pull a branch you'd like to apply your changes on. git pull <remotename> <branchname>
  • Checkout the branch. git checkout <branchname>
  • Pop the stash. git stash pop
  • Commit uncommited changes as usual. git add, git commit
  • Push your commited changes to the remote repository. git push <remotename> <branchname>.

Now you have commited changes in your submodule locally and pushed them to your remote repository. In your main project .gitmodules should be updated and point to the last commit you made in your submodule directory. Now it's your choice to either commit your .gitmodules file to use a commit with updated submodule, or you can discard and continue using old and not updated submodule. But if you discard and run git submodule update --remote, it will pull that old not updated branch, so you probably want to commit this .gitmodules file in your main project as well.

Here's a chapter about it from the Pro Git book: https://git-scm.com/book/en/v2/Git-Tools-Submodules

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文