0

I have two repositories, let's say Repo A and Repo B with the structures as shown below:

Repo A (there are many files in each repo. I am just showing 2 files for example):
  |
  |
  |---Test1.cs  (It has some changes made by X Developer)
  |---Test2.cs  (It has some changes made by X Developer)

Repo B:
  |
  |
  |---src
       |
       |
       |---Test1.cs  (It has some changes made by Y Developer)
       |---Test2.cs  (It has some changes made by Y Developer)

I want to merge (or rebase) of files from Repo A to Repo B/src without loosing history. After the merge, when I view history, I want to see both Developer X and Y changes. Is this possible? If yes, please guide me how to do this.

I have looked other SO posts and tried adding remote repo...etc. But those are not covering this type of scenario. My GIT version is 2.21.0.

sam
  • 1,937
  • 1
  • 8
  • 14
  • If you have tried adding `repo A` as remote to `repo B`, have you tried with this flag `--allow-unrelated-histories` ? – Saurabh P Bhandari Dec 27 '19 at 15:55
  • Does this answer your question? [Merge git repository in subdirectory](https://stackoverflow.com/questions/6426247/merge-git-repository-in-subdirectory) – Saurabh P Bhandari Dec 27 '19 at 16:03
  • @SaurabhPBhandari Thank you for the link. Yes, I have tried adding as remote repo and used all the steps enumerated in the SO link that you shared. They are like replacing the files but not like merging branches within same repo. – sam Dec 27 '19 at 16:20
  • 1
    History, in a Git repository, is just the set of commits in the repository. As long as you haven't removed commits from the repository, you've never *lost* any history. Most likely the issue you're seeing is that you're asking about the history of a *file* named `/Test1.cs` vs that of a *file* named `/src/Test1.cs` and unless you can get Git to treat these as *the same* file, well, any time you select "only those commits that contain src/Test1.cs" for instance, you're discarding from this view all those commits that contain "Test1.cs" (not in "src/") too. – torek Dec 27 '19 at 18:17
  • @torek Thank you for your comment. Is there a way to tell GIT to treat them as same files? – sam Dec 30 '19 at 19:45
  • Git is a little weak here. Using `git log --follow`, you can have Git walk from each commit to each previous (parent) commit *and* diff the two commits to see if some file "changed its name" in that particular commit-pair. If so, Git will stop looking for the *new* name and start looking for the *old* name from that point onward (backwards through history). But this only works for one file at a time, and only in this kind of traversal. On the other hand, when doing `git merge`, Git compares *all* the files in the *merge base* commit to *all* the files in each tip commit, and [continued] – torek Dec 30 '19 at 19:58
  • .. and if this comparison show that merge base file `path/A/file` becomes `path/B/file` in one of both *tip* commits, Git counts that as a *rename* operation. So for merge operations, sometimes Git automatically gets this right. – torek Dec 30 '19 at 19:59

1 Answers1

0

You can use a normal merge for this, but you will have to use merge --allow-unrelated-histories to allow unrelated histories to merge.

For example:

cd target-repository
# we work on master
git checkout master
# add the repository as a remote
git remote add other /path/to/source-repo
# fetch the remote repository, which will create other/master
git fetch other
# merge the histories, specifiy --allow-unrelated-histories to avoid the related history check
git merge --allow-unrelated-histories other/master

This assumes that files do not overlap.

somnium
  • 1,487
  • 9
  • 15
  • Thank you but as explained in the post, files overlap in my case and that is the reason those steps are not helping me. – sam Dec 27 '19 at 20:40
  • If they are overlapping you can do a normal merge. If you have reappearing conflicts, use `git rerere` to record appropriate solutions. – somnium Dec 28 '19 at 07:49
  • I tried that option. But it says its merged but am neither seeing merge conflicts nor both the developer changes in destination file. I am sorry I could not get rerere command. What is it and could you give me example? – sam Dec 30 '19 at 14:28