Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to merge branches in 2 different repositories?

Tags:

enter image description here

I am new to git and I would like to know how to merge Master branch from Sample Project 2 TO UAT branch in Staging/Support. I know its possible to merge different branches under a specific repository, but not sure how merging works across repositories.

What I really want to do is, after finish working on Sample Project 2, I would like to move it to the UAT branch for testing etc, and eventually to the Production branch for release.

Thanks in advance for the help

like image 477
user3528213 Avatar asked Jul 19 '16 21:07

user3528213


People also ask

Can I merge 2 repos?

You can merge repository A into a subdirectory of a project B using the subtree merge strategy. This is described in Subtree Merging and You by Markus Prinz. (Option --allow-unrelated-histories is needed for Git >= 2.9.

How do I merge two GitHub branches?

In GitHub Desktop, click Current Branch. Click Choose a branch to merge into BRANCH. Click the branch you want to merge into the current branch, then click Merge BRANCH into BRANCH. Note: If there are merge conflicts, GitHub Desktop will warn you above the Merge BRANCH into BRANCH button.

How do I merge two master branches?

First we run git checkout master to change the active branch back to the master branch. Then we run the command git merge new-branch to merge the new feature into the master branch. Note: git merge merges the specified branch into the currently active branch. So we need to be on the branch that we are merging into.


1 Answers

I would suggest you to create a local clone of the Staging/Support repository. Then you add the project repositories as further remote repositories, which allows you to interact with them in your local repository. Do the merges locally and push the result to the Staging repository.

This can be achieved using the following steps.

Initialization and first merge

$ git clone -o staging http://staging 

This clones your staging repository. You will need to replace "http://staging" with the correct URL to your Staging/Support repository. You might also want to give a path where to clone the repository as another parameter. The parameter -o makes sure the remote repository is called "staging" which helps to distinguish it from the project repositories later on.

Next step is to add the remote repository to merge from (in this case "Sample Project 2")

$ git remote add project2 http://sampleproject2 

Again, replace "http://sampleproject2" with the URL of the repository "Sample Project 2". You can also change "project2", which is the name of the remote repository, to better fit your project.

After doing that, git branch -r will show the branches from both staging and project2, like this:

$ git branch -r staging/Production staging/UAT project2/Master project2/QA project2/DEV 

Next checkout the branch you want to merge to, like this:

$ git checkout -b staging_UAT --track staging/UAT 

This creates a new local branch called staging_UAT which tracks the remote branch UAT from the staging repository. The new branch will be checked out immediately.

Now you have a copy of the UAT branch from staging checked out. You can merge in the changes from project2:

$ git merge project2/Master 

Now all changes from the branch Master of project2 are merged into the current branch (which is staging_UAT). You might want to have a look at git log to see the result. If it fits your expecations, you can push it to the Staging repository:

$ git push staging staging_UAT:UAT 

Doing this you push the current state of your local branch staging_UAT to the branch UAT in the remote repository called staging.

You can handle the other projects equally and also add a branch like staging_Production to merge your changes into the Production branch of Staging.

Future merges

You can use the same clone for future merges without doing all the cloning and branch creation again and again. However in this case you need to update your local information about the remote branches:

$ git checkout staging_UAT $ git pull 

First you need to update staging_UAT to match the current version of UAT in the Staging repository. This is done by "pull"ing the changes. As the branch staging_UAT was created with "--track staging/UAT", git knows from where to pull the changes. If UAT on staging is never changed on any other way than this one (meaning: using exactly this local clone to push there from staging_UAT), this is not required.

If UAT is changed on Staging and you do not pull, you will get an error when pushing, saying:

Updates were rejected because the tip of your current branch is behind its remote counterpart. 

The other update affects the Project2 repository:

$ git fetch project2 

Also the branches of the repository project2 might have been changed. As you can see git fetch is used to get those changes. The difference between fetch and pull is that pull will also update your local copy of the branch. As there is no local copy of the project2 branches, fetch is the way to go. (In fact git pull is just a shortcut for git fetch and git merge).

Adaptions

If you already have a local copy of a repository containing the code, you could also use that for the merging process. You just need to make sure you do not mix up local development and merging and maybe you need to handle more remote repositories as you have some more for your development.

The local branch staging_UAT could also be called just UAT, making the pushing quite simpler (git push would be enough in that case). However this might be confusing as there are branches with this name in multiple remotes.

Alternatively you could change your setting "push.default". See documentation to find out how that would affect your pushing.

like image 71
lucash Avatar answered Oct 07 '22 20:10

lucash