How to Merge Two Git Repositories
In this tutorial, you will find out how to merge several projects in a single repository without losing history. Get the essential information and codes.
Here, you will figure out how to merge two repositories into a single one without losing the commit history. You can use the technique below in case of having two similar repositories and with both maintainers coming together to merge it.
Steps to merging two repositories
Assume we have two repositories: <kbd class="highlighted">repo-1</kbd> and <kbd class="highlighted">repo-2</kbd> and the <kbd class="highlighted">repo-3</kbd> where you want to merge the repositories. Let’s consider that you are in the directory <kbd class="highlighted">repo-3</kbd>, where you want to merge the repositories. Suppose that you want to merge the master branches.
Adding remote URLs
As a first step, let's use the git remote add command with <kbd class="highlighted">-f</kbd> attribute to add a remote for the <kbd class="highlighted">repo-1</kbd>, naming it <kbd class="highlighted">remote-1</kbd>:
git add a remote URL of repositories
git remote add -f remote-1 <repo-1-URL>With -f option, git fetch <name> is run immediately after the remote information is set up.
You should repeat the same steps for <kbd class="highlighted">repo-2</kbd>:
git add a remote URL of repositories and merge all files
git remote add -f remote-2 <repo-2-URL>Combining files and folders
Now, the time has come for combining the files of the <kbd class="highlighted">remote-1</kbd> with the current branch of <kbd class="highlighted">repo-3</kbd>:
git merge all the files and folders from repositories
git merge remote-1/master --allow-unrelated-historiesYou should add <kbd class="highlighted">--allow-unrelated-histories</kbd> so as Git won’t refuse to merge unrelated histories.
Next, you should merge the master branch of the <kbd class="highlighted">remote-2</kbd>:
git add a remote URL of repositories and merge all files
git merge remote-2/master --allow-unrelated-historiesNote that conflicts can occur after running the command above. You need to resolve and commit the merge.
# After resolving conflicts in the affected files:
git add <resolved-files>
git commit -m "Merge remote-2/master into current branch"Viewing history
As a final step run git log command in your terminal to see merged commit history:
git log see commit history
git logThe git remote Command
The git remote command is used for creating, viewing, and removing connections to other repositories. Remote connections are considered to be bookmarks in other repositories, which are convenient names used for referencing URLs that are not convenient enough. Using the git remote add command will add a new remote connection to a remote repository. Executing the git remote add command, a new connection record will be created to a remote repository.
Merging Process
The git merge command is used to merge the independent lines of development into a single branch. The <kbd class="highlighted">git merge</kbd> command works with the git checkout command to select the current branch. The git checkout -b argument creates a new branch and directly switch to it.
The primary use of <kbd class="highlighted">git merge</kbd> is to merge two branches. It is also used to combine multiple commits into one history. It takes two branch tips and finds a common ancestor commit between them. The common ancestor commit creates a new commit that merges the changes in the sequence of each merge commit.