How do I “mount” one github repository as a subfolder of another repository?

大憨熊 提交于 2019-12-21 17:23:12

问题


I have a github repository named "foo" (or rather "myusername/foo"). Now suppose I want to create a repository named "bar", which uses the code in foo; I could just make a copy of the files, but I don't want to need to pull updates - I want to always see only whatever's in the "foo" repository right now; and not to have commits of "updates from foo". I believe I've noticed some repositories in which subfolders are actually separate repositories; and I know git supports sub-repositories or some such thing. So, can I perform such a "repository mount"? If so, how?

Bonus points if I can have the "mount" at a certain version, or a certain branch, rather than the master


回答1:


You have 2 main options to manage sub project in git.
Git does not have any dependency manager and the most common options are the following:

  • submodule
  • subtree

Submodules
allow foreign repositories to be embedded within a dedicated subdirectory of the source tree, always pointed at a particular commit.


git submodule

In order to use submodule each folder will be a standalone repository and will be managed and stored in a different location. Git will checkout specific commit (commit, version, tag etc) and will use it as the HEAD of the submodule. Most of the times the submodule will be in detached HEAD.

# once you have a repo simply add it to git 
git submodule add <url>

Once the projected is added tot your repo you have to init and update it.

git submodule init
git submodule update

As of Git 1.8.2 new option --remote was added

git submodule update --remote --merge

will fetch the latest changes from upstream in each submodule, merge them in, and check out the latest revision of the submodule.

As the docs describe it:

--remote

This option is only valid for the update command. Instead of using the superproject’s recorded SHA-1 to update the submodule, use the status of the submodule’s remote-tracking branch.

This is equivalent to running git pull in each submodule.


How do i fix or change code inside the submodule?

using submodule will place your code inside your main project as part of its content [new folder].

The difference between having it locally inside the folder or having it as part of a submodule is that in submodule the content is managed (commited) to a different standalone repository as explained above.


This is an illustration of submodule - project inside another project in which each project is a standalone project.


git subtree

Git subtree allows you to insert any repository as a sub-directory of another one

Very similar to submodule but the main difference is where your code is managed. In submodules the content is placed inside a separate repo and is managed there which allow you to clone it to many other repos as well.

subtree is managing the content as part of the root project and not in a separate project.

Instead of writing down how to set it up and to understand how to use it you can simply read this excellent post which will explain it all.

https://developer.atlassian.com/blog/2015/05/the-power-of-git-subtree/




回答2:


Beside the submodules and subtree, depending on your specific needs, you may also want to just have another repository without any logic and management.

In such case, you could place another repository in a subdirectory and add that subdirectory to the .gitignore file.

E.g.

RepositoryA/
  - .gitignore (first option)
  - SomeDir/
    - .gitignore (second option)
    - RepositoryB/

.gitignore (first option)

Add one of these:

  • SomeDir/RepositoryB/
  • **/RepositoryB/

.gitignore (second option)

Add this:

  • RepositoryB/

This way, as I said, does not add any logic/management. It just lets you to use any revision from both repositories without any collision.



来源:https://stackoverflow.com/questions/42039633/how-do-i-mount-one-github-repository-as-a-subfolder-of-another-repository

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!