简体   繁体   中英

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

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?

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.

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.

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?

我正在使用Copybara将一个 git Repo 的一部分同步到另一个

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM