3

I want to merge a repository into a folder of an other repository.

Currently, it looks like this :

I have multiple repositories like A, B, C,..

-> A 
   .git
-> B
   .git
-> C
   .git

And then I have only one repository for example called R which contains folder A,B,C (which are not git repositories but only folder which contains almost the same stuff than the previous A,B,C git repositories) :

-> R
   .git
   A
   B
   C

I want to merge A repository with the A folder in R repository, B repository with the B folder in R repository, etc.

What would be the best solution ?

zrrbite
  • 1,180
  • 10
  • 22
PierreP
  • 107
  • 7
  • I haven't found a good way of doing this. You can always add "A", "B" and "C" as remotes in "R" and e.g. `git merge A/ --allow-unrelated-histories` but this would put files from "A" in the root of the application and not in folder "A" (going by your layout) which you could then move. The same goes for `git cherry-pick A/`. If you were to restructure "A" to have an "A" folder, then you would succeed in getting the files from repo A into folder A. – zrrbite Oct 12 '20 at 22:50
  • Look up [[git] subtree merge](https://stackoverflow.com/search?q=%5Bgit%5D+subtree+merge). – jthill Oct 12 '20 at 23:47
  • [Here's](https://stackoverflow.com/questions/57739895/is-it-possible-to-push-on-git-when-i-only-have-a-subdirectory-of-the-project-on/57740405#57740405) an answer that's not close enough to mark as a dup bot should be enough to set you on the right path. – jthill Oct 12 '20 at 23:50
  • Thanks @zrrbite , found some stuff interesting there. – PierreP Oct 13 '20 at 08:02
  • thanks @jthill found some interesting stuff too :) – PierreP Oct 13 '20 at 08:02

1 Answers1

2

The structure of the trees in a git repository for a branch determine the resulting merge. I don't believe you can say "merge this branch from this remote, but merge it into this folder", since that would require git to re-interpret all the tree objects.

I'm not sure what you are allowed to do with your repos A, B and C, but if i restructure repo A to have an "A" folder to mimic "R", i can e.g. git merge repoA/master --merge-unrelated-histories after adding repoA as a remote.

zrrbite@ZRRBITE MINGW64 /d/dev/git/repotest (master)
$ git fetch repoA
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 2 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (2/2), done.
From D:/dev/git/repotest_A
   b73c55c..0b2748b  master     -> repoA/master

zrrbite@ZRRBITE MINGW64 /d/dev/git/repotest (master)
$ git merge repoA/master --allow-unrelated-histories
Merge made by the 'recursive' strategy.
 A/file_from_A.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 A/file_from_A.txt

zrrbite@ZRRBITE MINGW64 /d/dev/git/repotest/A (master)
$ ls
file_from_folder_A.txt  file_from_repo_A.txt

If i had files with identical names e.g. file_from_folder_A.txt then i would simply get a merge-conflict to solve:

zrrbite@ZRRBITE MINGW64 /d/dev/git/repotest (master)             
$ git merge repoA/master --allow-unrelated-histories             
CONFLICT (add/add): Merge conflict in A/file_from_folder_A.txt               
Auto-merging A/file_from_folder_A.txt                                        
Recorded preimage for 'A/file_from_folder_A.txt'                             
Automatic merge failed; fix conflicts and then commit the result.
                                                             
zrrbite@ZRRBITE MINGW64 /d/dev/git/repotest (master|MERGING)
...     
zrrbite
  • 1,180
  • 10
  • 22