1

I have a git repository A with contents:

 <shared files dir>/
        sharedFile1
        sharedFile2

And I have additional git repository B with 2 projects, each of them has to use the shared files from repository A. So that the file structure will be similar to this:

Project_X
   <shared files dir>/
      sharedFile1
      sharedFile2
   <project X dirs>

Project_Y
   <shared files dir>/
      sharedFile1
      sharedFile2
   <project Y dirs>

I would like the shared files to be updated only in the repository A, and I need to allow project_X and Project_Y to get those updates easily.

Actually, symbolic link could be the exact solution I need, but unfortunately, the filesystem I use doesn't support it.

Git subtree also looks as suitable solution, but the problem is that I couldn't find how to merge a subtree from same branch to 2 locations in same repository. I succeeded to create the file structure I need:

git read-tree --prefix=Project_X/shared_files_dir/ -u shared_remote_branch
git read-tree --prefix=Project_Y/shared_files_dir/ -u shared_remote_branch

Creates the required filestructure (exactly as I described at the top of the question), but trying to merge updates brings me the changes only to the last subtree I created:

git merge --squash -s subtree --no-commit shared_remote_branch

Updates only the Project_Y/shared_files_dir/, and not the Project_X/shared_files_dir/ with the changes from shared_remote_branch.

I would appreciate any ideas. On how to merge the subtree from one branch to 2 locations (Project_X/shared_files_dir/) and (Project_Y/shared_files_dir/) or how to make it in any other methodology

user2924714
  • 1,918
  • 1
  • 19
  • 26

1 Answers1

0

Since git1.8.2, you can register a submodule as following the latest commits of a given branch.

So I would recommend that approach over the subtree one, as it allows to:

  • maintain a separate history for the shared_files repo
  • easily update to the last commit of a branch

    git submodule update --remote
    

You can even convert an existing submodule in order to have it trakc the latest of a branch.


The OP chose a simpler solution:

I've decided to use a workaround:

  • manage the shared modules in separate repo and
  • add a post-checkout hook to my main repo which will copy those shared files from a shared repo to the main repo.

We are a small team, so this approach is the easiest for us

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thank you for your answer. I didn't want to use submodules due to lots of concerns about them on the web (e.g. loosing your changes when doing git submodule update). So I wondered if there might be additional solutions.... – user2924714 Oct 27 '13 at 11:33
  • @user2924714 I understand, but I have been using submodules for months now (as in my project: https://github.com/VonC/compileEverything/blob/master/.gitmodules#L10-L13, where the submodule Semantic-UI follows `master`), and with recent version of git, all those issues have been resolved or seriously mitigated. – VonC Oct 27 '13 at 14:51
  • Tried to use submodules, I still see that it's an overhead and there are too much options for mistakes. Do you know if it's possible to accomplish it using subtrees? (See original question: I failed to merge a subtree from same branch to 2 locations in same repository. I succeeded to create the file structure I need: git read-tree --prefix=Project_X/shared_files_dir/ -u shared_remote_branch git read-tree --prefix=Project_Y/shared_files_dir/ -u shared_remote_branch BUT: git merge --squash -s subtree --no-commit shared_remote_branch Updates only the Project_Y/shared_files_dir, and not both – user2924714 Oct 29 '13 at 08:06
  • @user2924714 I don't actually: subtree is really not the approach I would chose, and I never encounter "errors" with submodules. – VonC Oct 29 '13 at 08:20
  • @user2924714 I would advise you writing a new question (with a link back to this one), with a concrete example script (`git init` for all 3 repos, `git add` dummy files, `git subtree`...) for us to reproduce the issue. – VonC Oct 29 '13 at 08:53
  • 1
    Thanks you for your answer and advise. Meanwhile I won't reserahc on subtrees and submodules any further. I've decided to use a workaround- manage the shared modules in separate repo and add a post-checkout hook to my main repo which will copy those shared files from a shared repo to the main repo. We are a small team, so this approach is the easiest for us. – user2924714 Oct 30 '13 at 08:55
  • @user2924714 Excellent! That seems an easier solution, which should work for you. I have added it to the answer for more visibility. – VonC Oct 30 '13 at 08:56