Git:可以通过多个项目使用相同的子模块工作副本? [英] Git: Possible to use same submodule working copy by multiple projects?

查看:178
本文介绍了Git:可以通过多个项目使用相同的子模块工作副本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Git的新手。
比方说,我有两个与子模块添加了相同库的git仓库:

  / home / projects / project1 / library_XYZ 
/ home / projects / project2 / library_XYZ

也可以说, m同时在项目上工作。当我对库进行了更改时,可以在 / home / projects / project1 / library_XYZ 中说,我必须推送这些更改,然后将它们放入 / home / projects / project2 / library_XYZ 使它们可用于 project2 ,对吧?我认为这是不方便的,原因有两个:


  • 我必须建立 library_XYZ 两次。

  • 我有一个与实际项目组织相抵触的不必要的冗余。
    有任何方法可以让Git将子模块 library_XYZ 克隆到相同的本地目录中,即使文件像这样组织

      / home / projects / project1 
    / home / projects / project2
    / home / projects / library_XYZ
    code>

    while library_XYZ 仍然是两个项目的子模块?



    我认为这可能与这个,这是没有答案的,尽管我的设置有些不同。

    解决方案

    设置共享依赖作为子模块非常简单。 git子模块命令不会自动执行,但子模块不过是嵌套的存储库 - 而git不需要任何实际的存储库或其工作树在任何特定的地方。

    设置一个libraryXYZ repo作为共享子模块使用

     #子模块只是一个存储库。我们将分享这一个。 
    git clone u:// r / libraryXYZ

    #并保留它的工作权:
    (cd libraryXYZ; git config core.worktree ..)

    然后,从任何地方使用子模块复制项目并将其设置为使用共享模式:

      git clone u:// r / project1 
    cd project1
    git submodule init
    echo gitdir:path /到/ shared / libraryXYZ / .git> libraryXYZ / .git

    现在 project1 的s libraryXYZ 子模块将使用共享 libraryXYZ 回购和工作树。



    <建立你的构建系统来使用同样的工作树,你就完成了。您当然可以让git告诉您在任何给定的回购库中的库存情况:

     #例如,查看项目的全部内容子模块的部分保留
    git submodule foreach git rev-parse --git-dir --show-toplevel

    #或者只有一个:
    git --git-dir = $ project1 / libraryXYZ / .git rev-parse --git-dir --show-toplevel

    (后期编辑: @ twalberg的评论值得记住,这可能会使得从一个项目中执行 git子模块更新变得有点过于简单,而没有意识到您还改变了构建环境每个其他共享依赖项目。)


    I'm new to Git. Lets say, I have two git repositories that have the same library added as submodule:

    /home/projects/project1/library_XYZ
    /home/projects/project2/library_XYZ
    

    Also lets say, I'm working on the projects and the library simultaneously. When I did changes to the library, lets say in /home/projects/project1/library_XYZ, I would have to push these changes and then pull them in /home/projects/project2/library_XYZ to make them available for project2, right? I think this is unhandy for two reasons:

    • I will have to build library_XYZ two times.
    • I have an unwanted redundancy that contradicts the actual project organization.

    Is there any way to make Git clone the submodule library_XYZ to the same local directory, i.e. making the files being organized like this

    /home/projects/project1
    /home/projects/project2
    /home/projects/library_XYZ
    

    while library_XYZ still being a submodule of both projects?

    I think this might be related to this, which is unanswered, although my setup is somewhat different.

    解决方案

    Setting up shared dependencies as submodules is easy. The git submodule command doesn't do it automatically, but a submodule is nothing more than a nested repository -- and git doesn't require any actual repository or its worktree to be in any particular place.

    Set up a libraryXYZ repo for use as a shared submodule

    # a submodule is just a repository. We're going to share this one.
    git clone u://r/libraryXYZ 
    
    # and keep its worktree right here:
    ( cd libraryXYZ; git config core.worktree .. )
    

    Then, from anywhere, clone the project using the submodule and set it up to use the shared one:

    git clone u://r/project1
    cd project1
    git submodule init
    echo gitdir: path/to/shared/libraryXYZ/.git > libraryXYZ/.git
    

    Now project1's libraryXYZ submodule will use the shared libraryXYZ repo and worktree.

    Set up your build system to use that same worktree and you're done. You can of course get git to tell you where those are in any given repo:

    # for example to see where all a project's submodules' parts are kept
    git submodule foreach git rev-parse --git-dir --show-toplevel
    
    # or for just one:
    git --git-dir=$project1/libraryXYZ/.git rev-parse --git-dir --show-toplevel
    

    (late edit: @twalberg's remark is worth keeping in mind, this might make it a bit too easy to do a git submodule update from one project without realizing you've also altered the build environment for every other project that shares dependencies.)

    这篇关于Git:可以通过多个项目使用相同的子模块工作副本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆