3

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?

bkhouri
  • 243
  • 5
  • 13

4 Answers4

2

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.

Community
  • 1
  • 1
VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283
  • Ok, there are some additional reasons I dislike submodules. Most of the same reasons have been documented by opinionated bloggers. In addition, I also found Eclipse support for submodules is limited and buggy. – Alex R Mar 25 '15 at 22:43
  • @AlexR submodules have seen a lot of improvements lately. And they can follwo a branch (http://stackoverflow.com/a/18088319/6309). – VonC Mar 26 '15 at 07:05
  • Thanks for the clarifications. I'm going to award you the +100 bounty. I still prefer the alternative, using subtree merge to directly sync up two subfolders across repos while keeping each repo fully self-contained. – Alex R Mar 28 '15 at 15:07
  • @AlexR I understand: I experimented with subtree here (http://stackoverflow.com/a/24709789/6309) – VonC Mar 28 '15 at 15:09
0

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.

Ángel
  • 820
  • 7
  • 12
  • 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. They create an external dependency which has to be tracked and managed. 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. – Alex R Mar 21 '15 at 21:46
  • @AlexR So far as I know, you have two options: keep everything in one place on the fs and symlink to that place as necessary, or have copies where you need them and sync them as necessary . . . . "you'd have to track and know what was the corresponding commit in repoB" -- nope. There is no "corresponding commit", it's the same commit in every repo. That's git's strongest guarantee: a commit is the same everywhere, verifiably so. Sync in git is "git pull" once you've told it about any other repo(s) of interest, no one has suggested any way it could possibly be made easier so far as I know. – jthill Mar 21 '15 at 23:12
0

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?

Community
  • 1
  • 1
Alex R
  • 10,320
  • 12
  • 76
  • 145
0

I am using Copybara to sync a part of one git Repo to another

Vertexwahn
  • 6,759
  • 6
  • 50
  • 78