Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Merge two different Git repositories?

I have two Github repositories. One repository is on the remote server and another on the local server. Both of them have different commit histories of different files and folders. Now I wanted to merge both of them so that I can have both of them on the remote server as one single repository. Please help!

I have looked for various solutions suggesting as: reset the head of the local repository and then pull the remote repository on the same directory but it doesn't seem to be working:

git reset --soft head ~CommitSHA (First commit of the local repo)

git pull ~giturlofremoterepo (Pulling remote repo in the same directory)

like image 596
CoderSam Avatar asked Jun 14 '19 11:06

CoderSam


People also ask

Can I merge two Git repositories?

Combining two git repositories. Use case: You have repository A with remote location rA, and repository B (which may or may not have remote location rB). You want to do one of two things: preserve all commits of both repositories, but replace everything from A with the contents of B, and use rA as your remote location.

How do I merge two Git repository branches?

Merging Branches in a Local Repository To merge branches locally, use git checkout to switch to the branch you want to merge into. This branch is typically the main branch. Next, use git merge and specify the name of the other branch to bring into this branch.

How do I merge two Git repository and keep history?

To combine two separate Git repositories into one, add the repository to merge in as a remote to the repository to merge into. Then, combine their histories by merging while using the --allow-unrelated-histories command line option.


2 Answers

In a nutshell you can checkout one repository, add a remote to the second, rebase the second on top of the first and push the result to your new single remote repository:

Clone the first repository and add a remote to the second

git clone https://first.repo
git remote add second https://second.repo

Fetch the second and check its master branch out to a local branch second

git fetch second
git checkout second/master
git checkout -b second

Rebase the master branch of the second repository on top of the master branch of the first. Resolve any potential conflict along the way.

git rebase master second

Push to a new upstream repository

git push -u ...

This results in the two commit histories being concatenated one after each other.

like image 99
michid Avatar answered Sep 30 '22 04:09

michid


Create a new git repository and initialize with a new README file.

$ mkdir merged_repo
$ cd merged_repo
$ git init 
$ touch README.md
$ git add .
$ git commit -m "Initialize new repo"

Add the first remote repository

$ git remote add -f first_repo `link_to_first_repo`
$ git merge --allow-unrelated-histories first_repo/master

Create a sub directory and move all first_repo files to it.

$ mkdir first_repo
$ mv * first_repo/
$ git add .
$ git commit -m "Move first_repo files to first_repo directory"

Add the second remote repository

$ git remote add -f second_repo `link_to_second_repo`
$ git merge --allow-unrelated-histories second_repo/master

Fix any merge conflicts and complete the merge as follows

$ git merge --continue
like image 42
sudesh regmi Avatar answered Sep 30 '22 03:09

sudesh regmi