【问题标题】:Is there a way to commit external files into current repository?有没有办法将外部文件提交到当前存储库?
【发布时间】:2017-07-20 07:25:33
【问题描述】:

我有一个 Git 存储库 d:\repositories\temp 和一个目录 d:\temp1,我想实现这个:目录中的所有更改 d:\temp1 可以被检测到并提交到存储库 d:\repositories\temp(本地和远程)。可能吗?谢谢。

注意:我这里需要的功能可能和SVN的外链类似。

【问题讨论】:

  • 关于 temp1 的内容:是否应该将其与 repo 的完整内容进行比较?或者到 repo 的子文件夹?

标签: git


【解决方案1】:

您可以将d:\temp1 声明为:

  • 存储库本身 (git init .) 以记录其更改
  • d:\repositories\temp中的submodule

    git submodule add -b master -- /d/temp1
    git submodule update --init
    

见“true nature of submodules”:

  • temp1 将成为 d:\repositories\temp 的子文件夹,
  • d:\temp1 中所做的任何更改都可以在git submodule update --remote 之后提交并在d:\repositories\temp\temp1 中更新

这样,temp 在任何时候都会记录它正在使用的temp1 的确切状态(我更喜欢to a subtree
正如我在“SVN:externals equivalent in GIT?”中提到的,这接近于svn:external,尽管它是not completely the same

请注意,如果您将temp 推送到 GitHub,您将看到 temp1 为灰色文件夹:该灰色文件夹是 gitlinkspecial entry in the parent index

如果您使用 --recursive 选项克隆该 GitHub 存储库,您将取回 temp1 的内容,因为 Git 将能够克隆回 /d/temp1
...
但是,没有其他人能够克隆您的 GitHub 存储库获取 temp1 内容,因为他们无权访问您的 /d 磁盘!

为了保留子模块的内容,最好先将temp1本身推送到自己的GitHub repo,然后temp通过其GitHub url引用temp1(而不是通过本地路径@ 987654350@).
See here修改子模块的urltemp1.

【讨论】:

  • 感谢您的详细回复。当我在d:\repositories\temp 执行命令git submodule add -b master -- d:\temp1 时,显示以下消息“repo URL: 'master' must be absolute or begin with ./|../”,你知道如何解决吗?谢谢。
  • @RayChen 尝试使用相对路径:../../temp1
  • 嗨,VonC。消息更改为"fatal: repository 'https://github.com/temp1/' not found""fatal: clone of 'https://github.com/temp1' into submodule path 'temp1' failed"
  • @RayChen git remote -v(在两个回购中完成)返回什么?这些 repos 是从 GitHub 克隆的吗?
  • 如果使用/d/temp1作为子模块路径呢?
【解决方案2】:

Git 有一个叫做 submodule 的特性,可以用来实现你想要实现的,这里看看这个https://git-scm.com/book/en/v2/Git-Tools-Submodules

【讨论】:

  • 是的,我刚才提到了子模块,有很多链接来说明它与 svn 外部的细微差别。
  • 是的,我刚刚看了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-08-31
  • 2022-11-07
  • 2021-10-11
  • 2021-12-18
  • 1970-01-01
  • 2016-04-24
  • 2020-04-07
相关资源
最近更新 更多