Merge git repositories
Platform Notice: Data Center and Cloud By Request - This article was written for the Atlassian Data Center platform but may also be useful for Atlassian Cloud customers. If completing instructions in this article would help you, please contact Atlassian Support and mention it.
Support for Server* products ended on February 15th 2024. If you are running a Server product, you can visit the Atlassian Server end of support announcement to review your migration options.
*Except Fisheye and Crucible
Summary
If you are working on two similar projects and you now want them merged into one, it can be helpful to combine the code and version histories from the two repositories. By merging the histories of two repositories, you can create a more complete and accurate record of their project's development. It can also make it easier to manage changes and track progress over time.
Merging two repositories involves combining the code and version history of two separate repositories into a single repository, keep in mind that merging two repositories can be a complex process, and it's important to back up your code and commit history before you begin.
Solution
Choose which repository will be the base repository: One of the repositories will serve as the base repository, which means the other repository will be merged into it. Choose the repository with the code and commit history that you want to keep.
Clone the base repository: Clone the base repository to your local machine if it's not already there. You'll use this local copy to make changes and push them back to the repository.
Add the remote of the other repository: Add the other repository as a remote to your base repository. This will allow you to fetch the code and commit history from the other repository.
Fetch the code and commit history from the other repository: Use the git fetch command to pull the code and commit history from the other repository. You can specify the branch you want to merge, or you can merge all branches.
Merge the other repository into the base repository: Use the git merge command to merge the other repository into the base repository. You can choose to merge all branches or only specific ones.
Resolve any conflicts: When you merge two repositories, there may be conflicts between the code in the two repositories.
Remove the remote of the other repository: Once you've completed the merge, you can remove the remote of the other repository from your base repository.
This is a simple snippet showing us how to merge project-a into project-b:
cd path/to/project-b
git remote add project-a /path/to/project-a
git fetch project-a --tags
git merge --allow-unrelated-histories project-a/master # or whichever branch you want to merge
git remote remove project-a
ℹ️ Keep in mind that this snippet assumes that you want to merge the entire history of the first repository into the second repository, including all branches and tags. If you only want to merge specific branches or tags, you'll need to modify the fetch and merge commands accordingly. Also, note that merging unrelated histories can potentially cause conflicts or unexpected issues, so it's important to thoroughly test your code and commit history after the merge to ensure that everything is working as expected.
Was this helpful?