Git存储库,其中每个子模块都是相同存储库的一个分支.如何避免double/triple ...使用git clone --recursive下载? [英] Git repo where each submodule is a branch of same repo. How to avoid double/triple... download with git clone --recursive?

查看:59
本文介绍了Git存储库,其中每个子模块都是相同存储库的一个分支.如何避免double/triple ...使用git clone --recursive下载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下项目树:

src
data
doc

我想将所有文件夹保存在发布到Gitlab的Git存储库中.但是我不想跟src一起跟踪datadoc.

I'd like to keep all the folders in a Git repository, published to Gitlab. But I don't want to track data and doc together with src.

所以我使用以下策略:

git remote add origin ADDRESS
git submodule add -b data ADDRESS data
git submodule add -b doc ADDRESS doc

除我尝试使用以下方式复制存储库外,它实际上工作正常:

It actually works fine, except when I try to replicate the repository with:

git clone --recursive ADDRESS

所有对象都传输3次:根和datadoc都包含:

all objects get transmitted 3 times: both the root and data and doc all contain:

  • 起源/主人
  • 来源/数据
  • 来源/文档

是否有避免这种情况的简便方法?只是为了澄清我想要什么:

Is there an easy way to avoid this? Just to clarify what I'd like:

  • 主存储库只能获取origin/master,而不是其他两个
  • 数据子模块只应提取origin/data.
  • doc子模块应该只获取origin/doc.
  • the master repository should only fetch origin/master, not the other two
  • the data submodule should only fetch origin/data.
  • the doc submodule should only fetch origin/doc.

使用3个独立的存储库将很容易实现,但这太麻烦了,因为我将这种方法应用于多个项目.

Would be easy to achieve with 3 separate repositories, but that's too cumbersome, since I apply this approach for multiple projects.

git worktree 允许我手动实现所需的功能.

git worktree from this answer allows me to achieve what I want manually.

但是现在,代替自动方法(占用4倍的带宽):

But now, instead of the automatic approach (which consumes 4x bandwidth):

git clone --recursive git@foo:foo/bar.git

我必须做:

git clone git@foo:foo/bar.git
cd bar
git worktree add data origin/data
git worktree add src/notebooks origin/notebooks
git worktree add doc origin/doc
git worktree add reports origin/reports

由于.gitmodules文件已包含完整的信息,因此我可以使用一些脚本来自动执行此过程:

I could automate this process with some scripts, since .gitmodules file already contains the complete info:

[submodule "data"]
    path = data
    url = git@foo:foo/bar.git
    branch = data
[submodule "src/notebooks"]
    path = src/notebooks
    url = git@foo:foo/bar.git
    branch = notebooks
[submodule "doc"]
    path = doc
    url = git@foo:foo/bar.git
    branch = doc
[submodule "reports"]
    path = reports
    url = git@foo:foo/bar.git
    branch = reports

我想知道是否已经有一些标准的git脚本或标志来处理此问题?

I wonder if there already is some standard git script or flag that handles this?

推荐答案

Git设计为可分发的,这意味着每个用户都应具有完整的历史记录和所有分支.如果您只想使用一个裸仓库,但要使用不同的工作树来减少网络流量,则可以使用git worktree命令:

Git is designed to be distributed, that means every user should have whole history and all branches. If you want to have a single bare repo, but different working trees to reduce network traffic, you can do it using git worktree command:

因此,在您的情况下,假设您有一个src文件夹作为带有src分支的主文件夹,从中创建另外两个文件夹应该像

So in your case, let's say you have a src folder as a main folder with src branch, creating other two from it should be as simple as

git worktree add ../data data
git worktree add ../doc doc

查看此很棒的答案 https://stackoverflow.com/a/30185564/3066081 以获得有关此信息的更多信息命令.但是,如果您的git较旧而没有工作树支持,则可以将git-new-workdir脚本用作

See this awesome answer https://stackoverflow.com/a/30185564/3066081 to get more info about this command. But if you have an older git without worktree support, you can use git-new-workdir script as

git-new-workdir project-dir new-workdir branch

使用Git的多个工作目录中也对此进行了描述?

这篇关于Git存储库,其中每个子模块都是相同存储库的一个分支.如何避免double/triple ...使用git clone --recursive下载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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