-1

I have a two Git repositories REPO-A and REPO-B.

REPO-A have --> BRANCH-A , and BRANCH-A have following directories DirA, DirB, DirC

REPO-B have ---> BRANCH-B, and BRANCH-B have following directories DirD, DirE, DirA, DirG

I would like to Merge the DirA from BRANCH-A to BRANCH-B. what is the best way to merge DirA ?

Note: BRANCH-A and BRANCH-B are in different repositories.

I am expecting some git commands to solve the problem. I tried few methods and commands but none worked.

knittl
  • 246,190
  • 53
  • 318
  • 364
Ash
  • 1
  • 1
  • "I tried few methods […] but non worked" – what _exactly_ have you tried? – knittl Apr 20 '23 at 06:24
  • Git only ever merges _full commits_, not directories. You might be successful using the subtree merge strategy, but I'm wondering if Git is the correct tool for your use case. – knittl Apr 20 '23 at 06:25
  • Different repositories don't matter much, you could simply fetch the branch from one repo to the other repo, then work with the local commits. – knittl Apr 20 '23 at 06:25
  • Are the branches connected in any way or do they contain unrelated history? Do the commits of branch-a, that are not in branch-b, contain changes to other files outside of dir-a? – knittl Apr 20 '23 at 06:26
  • I tried to create the another temp-DirA branch from Branch-B, and copy DirA to temp-DirA branch, just override.. then use the git merge to merge the temp-DirA into Branch-B. – Ash Apr 20 '23 at 06:30
  • Two different repositories, why not just do a cp of the files ? (for info git tracks only files, no directories) – Ôrel Apr 20 '23 at 06:32
  • Copy files of Dir A directly to Branch B dir A does not work. as I do not want to Override the Files. I just want to merge – Ash Apr 20 '23 at 06:36
  • Are the branches connected in any way or do they contain unrelated history? Do the commits of branch-a, that are not in branch-b, contain changes to other files outside of dir-a? - The DirA was originally part of Branch B only.. it was taken to Branch-A and added new features. on parallel, Branch-B continue to add new features too. Now I want to Pull the Branch-A dirA features to Branch-B dirA. – Ash Apr 20 '23 at 06:36
  • @Ash and branch-a only contains changes to this repository? Then just `git fetch` + `git merge` (or `git pull`) the remote branch into your local branch. I see two questions in this, but I don't know what question is causing you problems: 1) how to get a branch from fork into your repo 2) how to merge a single directory only, while other directories were also changed in the branch – knittl Apr 20 '23 at 06:56
  • As long as the branch names are distinct it doesn’t matter whether they come from different remote repositories. – Guildenstern Apr 20 '23 at 07:28
  • Since Git is distributed, two repositories can share the same branches; if I clone the Linux project locally then there exists one more Linux repository. This seems to be the case given your comments here. Then I don’t see why you cannot just add the second repo as a remote in the first repo, fetch the branch, check it out, and then operate it in the first repo. – Guildenstern Apr 20 '23 at 07:36
  • @knittl Branch-A belong to REPO-A only. To answer your questions. I am not sure what you mean how to get branch. for the second question, merging single directory only, Yes it is a independent module, there is no dependency on other directory, So I want to just merge Dir A from branch -A to Branch-B.. – Ash Apr 21 '23 at 00:19
  • @Ash I'm not following. A branch does not "belong" to a repository. A branch is only a (movable) name for a commit. Commits can be freely copied (`fetch`ed or `push`ed) between repositories. If the commits labeled by `branch-a` build on top of `branch-b` and only contain changes to `dir/a`, then `fetch` + `merge`. I still have not identified the real problem here: 1) fetching commits/branches from another repository 2) merging branches (?) 3) does branch-a contain other changes in other directories? 4) something else – and it really seems like there are at least two questions in this post … – knittl Apr 21 '23 at 05:12
  • … and posts must only contain a single question. So what is _the question_ or the problem? Fetching or merging? Which part is causing you problems? – knittl Apr 21 '23 at 05:12
  • @knittl let me just simplify my question. Here I will just take 1 file as an example, this will simplify the question and understanding. – Ash Apr 26 '23 at 04:50
  • @knittl repository REPO-A have --> BRANCH-A repository REPO-B have ---> BRANCH-B the branch-A contain the file /dirA/file.js the branch-B also contain the file /dirA/file.js, I would like to merge the file.js from Branch-A to Branch-B. This is just an example, I have to merge hundreds of files like this. – Ash Apr 26 '23 at 04:55
  • @Ash you do _not_ merge "files", you merge "commits". Merge commits/branches with `git merge`. Fetching a branch from a remote repository is also a solved problem (cf. https://stackoverflow.com/q/21353656/112968, https://stackoverflow.com/q/70691216/112968, https://stackoverflow.com/q/52653087/112968). You keep coming back and explain that branch-a and branch-b contain some file (and then mention that they also contain other files). Again, you only merge _full branches_ (based on the tree information of their commits). cont'd … – knittl Apr 26 '23 at 06:13
  • … cont'd. So it is **still** unclear if your problem is running the `git merge` command and whether that gives the expected output or if your problem/question is running the `git fetch` command to get the branch you want to merge. I have already asked 3 times which of the two problems is your question and you have always simply repeated what was already written in your question. cont'd … – knittl Apr 26 '23 at 06:16
  • … cont'd. To repeat: fetching branches from arbitrary remote repositories is a solved problem and merging branches is trivial. So what is your question? Please [edit] your question to show the commands which you have already tried (or would try/would expect to exist) and why that failed. – knittl Apr 26 '23 at 06:16

1 Answers1

0

In general, if you have two different repositories they are completely separate and you can't just copy branches between them. The only thing you can do is manually copy the directories between the repositories. This will copy the files, but of course, you will lose the history.

Joshua Zeltser
  • 488
  • 2
  • 9
  • “In general, if you have two different repositories they are completely separate and you can't just copy branches between them.” OP [said](https://stackoverflow.com/questions/76061076/git-merging-a-directory-from-a-branch-of-one-repository-to-the-branch-of-another#comment134143236_76061076) that the histories are related (not unrelated). Git is distributed so saying that you have “two repositories” when/even though they share history is correct terminology. – Guildenstern Apr 20 '23 at 08:08