git 如何使外部存储库和嵌入式存储库作为公共/独立存储库工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47008290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 05:10:26  来源:igfitidea点击:

How to make outer repository and embedded repository work as common/standalone repository?

gitgithubversion-control

提问by Mithril

I have a big project(let's say A repo), and it there one child folder which is come from B repo. I would meet warning like below when I commit from A repo

我有一个大项目(比方说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.

I have seen git-submoduleand git-subtree:

我见过git-submodulegit-subtree

Maintaining Git repo inside another git repo

在另一个 git 仓库中维护 Git 仓库

https://www.atlassian.com/blog/git/alternatives-to-git-submodule-git-subtree

https://www.atlassian.com/blog/git/alternatives-to-git-submodule-git-subtree

But I don't like them , because they need extra config.

但我不喜欢它们,因为它们需要额外的配置。



What I want is , for example:

我想要的是,例如:

structure like:

结构如:

A/
--- a.py

--- B/
--- B/b.py

When I change B/b.py.

当我改变B/b.py.

  1. If I am on path A/, git addcan detect B/b.pychanged, git pushonly commit that to 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 )
    
  2. If I am on path A/B/, git addonly add B/b.pychanges to B repo, and git pushonly commit that to B repo.

    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/  )
    
  3. Once I want to snyc A and B in another machine, just do

    git clone A
    rm -rf A/B/
    git clone B ./B
    git add . && git commit 'sync with B'
    
  1. 如果我在 path 上A/git add可以检测到B/b.py更改,git push只需将其提交给 A 存储库。

    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 )
    
  2. 如果我在 path 上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/  )
    
  3. 一旦我想在另一台机器上 snyc A 和 B,就做

    git clone A
    rm -rf A/B/
    git clone B ./B
    git add . && git commit 'sync with B'
    

In another word, A and B act as a standalone repo.

换句话说,A 和 B 充当独立的回购。

But the truth is , A repo treat B repo as submodule:

但事实是,A repo 将 B repo 视为子模块:

A repo https://github.com/eromoe/test

一个回购 https://github.com/eromoe/test

B repo https://github.com/eromoe/test2

B 仓库 https://github.com/eromoe/test2



How do I force A repo track all files under A/, and B repo track all files under A/B/? I want A and B act as a self-contain repo , without any other config.

如何强制 A 回购跟踪 下的所有文件A/,而 B 回购跟踪 下的所有文件A/B/?我希望 A 和 B 作为一个独立的 repo ,没有任何其他配置。

回答by Marina Liu

You can use below commands to add files from test2 repo to test repo as below:

您可以使用以下命令将 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

Note:

笔记:

You should use git add test2/(with slash, not git add test2).

您应该使用git add test2/(带斜杠,而不是git add test2)。

git add test2/will treat test2folder and it's files as ordinary folder and file for test repo (create mode 100644).

git add test2/test2文件夹及其文件视为普通文件夹和用于测试存储库的文件(创建模式100644)。

git add test2will treat test2folder as a submodule for test repo (create mode 160000).

git add test2test2文件夹视为测试仓库的子模块(创建模式160000)。

回答by rost shan

Probably, git reminded the repository. It helped for me:

可能是 git 提醒了存储库。它对我有帮助:

    git rm --cached your_folder_with_repo
    git commit -m "remove cached repo"
    git add your_folder_with_repo/
    git commit -m "Add folder"
    git push

回答by VonC

If you don't care about the exact version of B A is using, you can keep your current setting (nested git repos).

如果您不关心 BA 正在使用的确切版本,则可以保留当前设置(嵌套的 git 存储库)。

You will have the "embedded repo" warning, but beside that, both repos will behave as you expect, each one adding, committing and pushing only their repos.
Note: you can make that warning shorted/empty with git config advice.addEmbeddedRepo

您将收到“嵌入式存储库”警告,但除此之外,两个存储库都将按照您的预期运行,每个存储库仅添加、提交和推送它们的存储库。
注意:您可以使该警告短路/清空git config advice.addEmbeddedRepo