How can I keep a subfolder of a git repo in sync with a subfolder of another git repo

怎甘沉沦 提交于 2019-12-12 09:09:07

问题


I have two git projects. One depends on a subfolder of another repository.

Here's how the folders are setup.

repoA
    .git
    folderA1/
    folderA2/

repoB
    .git
    folderB1/
    folderB1/folderB11
    folderB2/

What I want would like to achieve is the following

repoA
    .git
    folderA1/
    folderA2/
    folderB11   <<<< This maps to the repoB on branch name "blah"

repoB
    .git
    folderB1/
    folderB1/folderB11
    folderB2/

In repoA, some files in folderA1 might reference the ones in folderB11. repoA contains python modules that reference files in folderB11. Similarly, repoB also contains python modules that reference files in folderB11.

I looked at git subtree, but it doesn't appear to sync both folder.

One option is to create folderB11 as a repository and add it a subtree to repoA and repoB, but I would rather not have a third repository as it's going to be a pain to maintain the code. Also, it's not ideally an option to have a third repository as folderB11 should be located in repoB to ensure accuracy with the rest of the project.

Is there a way to synchronize a subfolder of a git repo with a subfolder of another git repo?


回答1:


Alex R commented:

One problem with both alternatives of symlinks or submodules is that you don't get a coherent history for either repoA or repoB in a single repository.

With submodule, it does, since it records in parent repo (repoA) the exact SHA1 of the repoB used.
It records it as a gitlink (a special entry in the index).

They create an external dependency which has to be tracked and managed.

That is what submodule does for you.

If you want to go back to an old commit of repoA, you'd have to track and know what was the corresponding commit in repoB.

Again, submodule records that information.
Go back to an old version of repoA, do a git submodule update --init and your repoB is in a coherent state, at the right sha1.




回答2:


No. The git way would be to make folderB11 a repository, included as submodule by repoA and repoB.

As you don't like that (why? it shouldn't be such a problem), you can use symlinks to make folderB11 point to the appropiate subfolder of repoB checkout. You can make repoB a submodule of repoA and point the symlink to repoB/folderB11 (albeit it's a bit ugly) or, if they are intended to be checkout side by side, you could use a relative path (such as ../repoB/folderB11) knowing that it will break if repoB isn't checked out in the same folder as repoA.




回答3:


There is a poorly documented feature of subtree that can be used here, allowing a subfolder-to-subfolder (as opposed to subfolder-to-repository) to be specified in the merge target syntax.

I just discovered it here: How do I merge a sub directory in git?



来源:https://stackoverflow.com/questions/22646638/how-can-i-keep-a-subfolder-of-a-git-repo-in-sync-with-a-subfolder-of-another-git

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