Merge code between multiple repos in the same workspace
Asked by Christian:

Greetings, I'm not sure what the best strategy is but we have developers working on various aspects of software that are separated into three repos in the same workspace on Bitbucket. Let's say we want to test code and need to merge development branches from each repo into one for testing purposes. Is this possible?

Also is there a better way to organize code if one group of developers is working on, as an example, a database and another group is working on front end UI; should this all be in one repo or is it ok to separate it out since the code could be used across multiple applications?

Thank you in advance!


Answers:
author: Theodora Boudale
body: Hi Christian,

Merging code between multiple repos is possible from Bitbucket Cloud website only for repos that have a fork relationship. If they don't, this is not possible, as the repos have unrelated histories.

It is something you can do locally though, from your machine, the following way:

**1.** Assume you have repos <https://bitbucket.org/my-workspace/repo-1/> and <https://bitbucket.org/my-workspace/repo-2/>, and you want to merge code from repo-2 to repo-1

Navigate to your local clone of repo-1

**2.** Add the second repo as a remote with a command like the following (I'm adding the SSHL URL; if you don't use SSH, you can replace with the HTTPS URL:

```
git remote add second_remote git@bitbucket.org:my-workspace/repo-2.git
```

then

```
git fetch --all
```

**3.** Then create (in repo-1) a branch that tracks the remote branch from repo-2 you want to merge, named e.g. feature2-branch

```
git checkout -b feature2-branch --track second_remote/feature2-branch
```

**4.** If you wang to merge that to master branch of repo-1, then you can do

```
git merge master --allow-unrelated-histories
```

The --allow-unrelated-histories will be necessary if the repos have no common history, otherwise Git will refuse to make the merge.

<br />

I'd like to note here the following though: if you merge code like that and push it back to repo-1, this kind of defeats having 3 repos for separation of concerns, since you will be pushing code from one repo to the other. It may be better if you do this merge on the server where you run your tests only?

Regarding your second question and how to better organize code, I believe this depends on your business needs. There's pros and cons for each approach, you can take a look at the following for issues that may arise with monrepos:

* <https://www.atlassian.com/git/tutorials/monorepos>

and I see there are also many articles online available from different sources exploring the monrepo vs multirepo appraoch.

Kind regards,  
Theodora

comments: author: Ganesh Raj Manandhar
body: Hi Theodora,  

Since it was stated that "Merging code between multiple repos is possible from Bitbucket Cloud website only for repos that have a fork relationship". Can you give a work through on how to achieve this.  
Our use case:  
1. Repo B is a forked from Repo A.  
2. There's a bitbucket-pipelines.yml in repo B.  
3. Need to merge branch from Repo A i.e branch A into repo B's branch i.e branch B.  

So, is it possible to do so by writing scripts in the .yml file itself? the idea was to add remote of repo A in the yml and then do the fetching/merge using the remote but it's not working.  
Any other solutions are welcome as well.  

<br />


Tags: atl-gruland

bitbucket-cloud

cloud