我有一个大项目(比方说A repo
),它有一个来自B repo
的子文件夹。当我从 A repo
warning: adding embedded git repository: extractor/annotator-server
hint: You've added another git repository inside your current repository.
hint: Clones of the outer repository will not contain the contents of
hint: the embedded repository and will not know how to obtain it.
hint: If you meant to add a submodule, use:
hint:
hint: git submodule add <url> extractor/annotator-server
hint:
hint: If you added this path by mistake, you can remove it from the
hint: index with:
hint:
hint: git rm --cached extractor/annotator-server
hint:
hint: See "git help submodule" for more information.
我看过git-submodule
和git-subtree
:
Maintaining Git repo inside another git repo
https://www.atlassian.com/blog/git/alternatives-to-git-submodule-git-subtree
但我不喜欢它们,因为它们需要额外的配置。
我想要的是,例如:
结构如下:
A/
--- a.py
--- B/
--- B/b.py
当我更改 B/b.py
时。
如果我在路径
A/
上,git add
可以检测到B/b.py
已更改,git push
仅将其提交给 A repo。git add . (would add changes under A/ ) git push (would push changes under A/ ) git pull (would pull changes under A/ ) git clone XXX:A (would clone all files under A/ , A/B/ is just looks like plain folder with all files, not a repo )
如果我在路径
A/B/
上,git add
只会将B/b.py
更改添加到 B 仓库,并且git push
只将其提交给 B 仓库。git add . (would add changes under B/ , but not add changes to A repo) git push (would push changes under B/ , but not push changes to A repo) git pull (would clone changes under B/ , ) git clone XXX:B (would clone all files under B/ )
一旦我想在另一台机器上同步 A 和 B,就这样做
git clone A rm -rf A/B/ git clone B ./B git add . && git commit 'sync with B'
换句话说,A 和 B 充当独立的存储库。
但事实是,A repo 将 B repo 视为子模块:
repo https://github.com/eromoe/test
B repo https://github.com/eromoe/test2
如何强制 A repo 跟踪 A/
下的所有文件,B repo 跟踪 A/B/
下的所有文件?我希望 A 和 B 充当一个独立的 repo,没有任何其他配置。
最佳答案
您可以使用以下命令将文件从 test2 存储库添加到测试存储库,如下所示:
# In local test repo
rm -rf test2
git clone https://github.com/eromoe/test2
git add test2/
git commit -am 'add files from test2 repo to test repo'
git push
注意:
您应该使用 git add test2/
(带斜线,而不是 git add test2
)。
git add test2/
会将 test2
文件夹及其文件视为普通文件夹和测试 repo 文件(创建模式 100644
)。
git add test2
会将 test2
文件夹作为测试仓库的子模块(创建模式 160000
)。
关于Git:如何使外部存储库和嵌入式存储库作为公共(public)/独立存储库工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47008290/