是否可以在另一个 git repo 中拥有一个 git repo

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7150424/
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-19 05:52:59  来源:igfitidea点击:

Is it possible to have a git repo inside another git repo

gitgit-submodules

提问by yasar

I have a django project managed with git. I am git pushing it to my host. Now, I want to be able to push just one of the directories (inc. all sub dirs) to another git repo. How is it possible, if it is possible at all?

我有一个用 git 管理的 django 项目。我正在将它推送给我的主机。现在,我希望能够将其中一个目录(包括所有子目录)推送到另一个 git 存储库。如果可能的话,怎么可能呢?

Edit: So I want that directory to be a git repo itself.

编辑:所以我希望该目录本身就是一个 git repo。

回答by Mark Longair

You could look at git's support for submodules. You would add the second repository as a submodule to your main project, e.g. with commands like:

您可以查看 git 对submodules的支持。您可以将第二个存储库作为子模块添加到主项目中,例如使用以下命令:

git submodule add git://wherever/blah.git library-code
git commit -m "Added a new submodule called 'library-code'"

When you change into the library-codesubdirectory, it's as if the parent repository doesn't exist - you can change originto use a transport that you can push over and then push as if it were completely independent.

当您切换到library-code子目录时,就好像父存储库不存在一样 - 您可以更改origin为使用可以推送的传输,然后推送,就好像它是完全独立的一样。

To specify that you want the submodule to be at a particular version, you should change into the submodule and use git checkoutto switch to the right version. Then you change back up to the main repository and stage and commit that new submodule version with:

要指定您希望子模块处于特定版本,您应该切换到子模块并使用git checkout切换到正确的版本。然后,您更改回主存储库和阶段,并使用以下命令提交新的子模块版本:

git add library-code
git commit -m 'Change the submodule version'

The tree of the main repository just stores the version that the submodule should be at, so when you push the main repository, it's not pushing any of the files in the submodule.

主存储库的树只存储子模块应处于的版本,因此当您推送主存储库时,它不会推送子模块中的任何文件。

In order to split off this subdirectory, while preserving history, you'll need to clone your original repository and user git filter-branchto rewrite the history, as described in this answer:

为了拆分此子目录,同时保留历史记录,您需要克隆原始存储库和用户git filter-branch以重写历史记录,如本答案所述:

Then you can push that to a newly created repository GitHub repository, return to your original project, remove the subdirectory, and replace it with the submodule as described above.

然后,您可以将其推送到新创建的存储库 GitHub 存储库,返回到您的原始项目,删除子目录,并将其替换为如上所述的子模块。

If you're not very familiar with git concepts then this may be difficult for you - I would recommend reading up on git submodules beforehand.

如果您对 git 概念不是很熟悉,那么这对您来说可能很困难 - 我建议您事先阅读 git 子模块。

回答by manojlds

When you make changes to a particular directory, stage those changes and commit and push, you are effectively "pushing" just that directory.

当您对特定目录进行更改、暂存这些更改并提交和推送时,您实际上只是在“推送”该目录。

If the other Git repo has only the directory you want to push, and that is why you want to have it as a repo in itself, you can look at Git Submodules.

如果另一个 Git 存储库只有您想要推送的目录,这就是您希望将其作为存储库本身的原因,您可以查看Git Submodules

回答by lunixbochs

Have you looked into git submodules? It allows git repositories to cleanly nest, but remain fully independent.

你有没有研究过 git 子模块?它允许 git 存储库干净地嵌套,但保持完全独立。

This means you can have a subdirectory with a different remote url for push/pull and different commit tree+log without cluttering up the main project.

这意味着您可以拥有一个子目录,该子目录具有用于推/拉的不同远程 url 和不同的提交树 + 日志,而不会弄乱主项目。

https://git.wiki.kernel.org/index.php/GitSubmoduleTutorial

https://git.wiki.kernel.org/index.php/GitSubmoduleTutorial