{"id":32320,"date":"2026-11-30T22:08:01","date_gmt":"2026-11-30T22:08:01","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=32320"},"modified":"2026-11-30T22:08:01","modified_gmt":"2026-11-30T22:08:01","slug":"git-tutorials-merge-code-between-multiple-repos","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/git-tutorials-merge-code-between-multiple-repos\/","title":{"rendered":"Git Tutorials: Merge code between multiple repos"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">To merge code from one Git repository into another, add the second repository as a remote, fetch it, and merge its branch into your target branch with <code>--allow-unrelated-histories<\/code>. This keeps the full commit history from both repositories. Below the original steps you&#039;ll find a corrected version of the merge command, which is reversed in step 4, plus help with conflicts, a way to merge into a subdirectory, and fixes for common errors.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is something you can do locally though, from your machine, the following way:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>1.<\/strong>&nbsp;Assume you have repos&nbsp;<a href=\"https:\/\/bitbucket.org\/my-workspace\/repo-1\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/bitbucket.org\/my-workspace\/repo-1\/<\/a>&nbsp;and&nbsp;<a href=\"https:\/\/bitbucket.org\/my-workspace\/repo-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/bitbucket.org\/my-workspace\/repo-2\/<\/a>, and you want to merge code from repo-2 to repo-1<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Navigate to your local clone of repo-1<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>2.<\/strong>&nbsp;Add the second repo as a remote with a command like the following (I&#8217;m adding the SSHL URL; if you don&#8217;t use SSH, you can replace with the HTTPS URL:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">git remote add second_remote git@bitbucket.org:my-workspace\/repo-2.git<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">then<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">git fetch --all<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>3.<\/strong>&nbsp;Then create (in repo-1) a branch that tracks the remote branch from repo-2 you want to merge, named e.g. feature2-branch<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">git checkout -b feature2-branch --track second_remote\/feature2-branch<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>4.<\/strong>&nbsp;If you wang to merge that to master branch of repo-1, then you can do<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">git merge master --allow-unrelated-histories<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The &#8211;allow-unrelated-histories will be necessary if the repos have no common history, otherwise Git will refuse to make the merge.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">What this technique does and when to use it<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Git can fetch from any number of remotes, not just <code>origin<\/code>. When you add a second repository as a remote, its branches and commits are downloaded into your local repo. You can then merge them like any other branch. The result is one repository with the commit history of both.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Consolidating repos<\/strong>: combining two related services or libraries into one repository (monorepo migration).<\/li>\n\n\n<li><strong>Porting a feature<\/strong>: bringing a branch from a fork or a sibling repository into your own.<\/li>\n\n\n<li><strong>Rescuing work<\/strong>: pulling commits out of an archived or duplicated repository without losing authorship and history.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">If the two repositories were never forks of each other, they share no common commit. Git refuses to merge them unless you pass <code>--allow-unrelated-histories<\/code>. If one repo is a fork of the other, they already share history and the flag is not needed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Corrected step-by-step workflow<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In step 4 of the example above, <code>git merge master<\/code> is run while <code>feature2-branch<\/code> is checked out. That merges <code>master<\/code> <strong>into<\/strong> <code>feature2-branch<\/code>, which is the opposite of the goal. To bring repo-2&#039;s code into repo-1&#039;s main branch, check out the target branch first, then merge the incoming branch into it. The full sequence:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\"># 1. Work inside a clean, up-to-date clone of repo-1<\/span>\ncd repo<span class=\"hljs-number\">-1<\/span>\ngit status                      <span class=\"hljs-comment\"># should report a clean working tree<\/span>\ngit <span class=\"hljs-keyword\">switch<\/span> main                 <span class=\"hljs-comment\"># or: git checkout master<\/span>\ngit pull origin main\n\n<span class=\"hljs-comment\"># 2. Add repo-2 as a second remote (SSH or HTTPS URL)<\/span>\ngit remote add second_remote git@bitbucket.org:my-workspace\/repo<span class=\"hljs-number\">-2.<\/span>git\n<span class=\"hljs-comment\"># HTTPS alternative:<\/span>\n<span class=\"hljs-comment\"># git remote add second_remote https:\/\/bitbucket.org\/my-workspace\/repo-2.git<\/span>\n\n<span class=\"hljs-comment\"># 3. Download repo-2's branches and commits<\/span>\ngit fetch second_remote\n\n<span class=\"hljs-comment\"># 4. Merge the wanted branch from repo-2 into repo-1's main branch<\/span>\ngit merge second_remote\/feature2-branch --allow-unrelated-histories \\\n  -m <span class=\"hljs-string\">\"Merge feature2-branch from repo-2\"<\/span>\n\n<span class=\"hljs-comment\"># 5. Publish the result<\/span>\ngit push origin main<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">You don&#039;t need to create a local tracking branch (step 3 above) just to merge. <code>git merge second_remote\/feature2-branch<\/code> merges the remote-tracking ref directly. A local branch is only useful if you want to inspect or change repo-2&#039;s code before merging. If you want one, use <code>git switch -c feature2-branch --track second_remote\/feature2-branch<\/code>, then switch back to <code>main<\/code> before running the merge.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note:<\/strong> Use the branch names that actually exist. Many repositories now use <code>main<\/code> instead of <code>master<\/code>. Run <code>git branch -r<\/code> after fetching to list the branches available on <code>second_remote<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Verifying the result<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Check that the remote was added and the merge commit has two parents:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$ git remote -v\norigin         git@bitbucket.org:my-workspace\/repo<span class=\"hljs-number\">-1.<\/span>git (fetch)\norigin         git@bitbucket.org:my-workspace\/repo<span class=\"hljs-number\">-1.<\/span>git (push)\nsecond_remote  git@bitbucket.org:my-workspace\/repo<span class=\"hljs-number\">-2.<\/span>git (fetch)\nsecond_remote  git@bitbucket.org:my-workspace\/repo<span class=\"hljs-number\">-2.<\/span>git (push)\n\n$ git log --oneline --graph -n <span class=\"hljs-number\">15<\/span>\n<span class=\"hljs-comment\"># Expect a merge commit at the top with two lines of history<\/span>\n<span class=\"hljs-comment\"># joining it: one from repo-1 and one from repo-2.<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\"><code>git log --author=&quot;Name&quot;<\/code> or <code>git blame<\/code> on a file from repo-2 should show the original authors and dates. This confirms that history was kept and not squashed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Handling conflicts between unrelated repositories<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Unrelated repositories often contain files with the same path, such as <code>README.md<\/code>, <code>.gitignore<\/code>, <code>package.json<\/code> or CI configuration. Git reports these as <code>both added<\/code> conflicts because neither side has a common ancestor version.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Run <code>git status<\/code> to list the conflicting files.<\/li>\n\n\n<li>Open each file, resolve the <code>&lt;&lt;&lt;&lt;&lt;&lt;&lt;<\/code>, <code>=======<\/code> and <code>&gt;&gt;&gt;&gt;&gt;&gt;&gt;<\/code> markers, and keep the content you need from both sides.<\/li>\n\n\n<li>To keep one side entirely, use <code>git checkout --ours path\/to\/file<\/code> (repo-1) or <code>git checkout --theirs path\/to\/file<\/code> (repo-2).<\/li>\n\n\n<li>Stage the resolved files with <code>git add path\/to\/file<\/code>.<\/li>\n\n\n<li>Finish with <code>git commit<\/code>. To abandon the whole merge instead, run <code>git merge --abort<\/code>.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">If many files collide, it is usually cleaner to merge repo-2 into its own subdirectory, as shown in the next section.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Merging another repository into a subdirectory<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">For monorepo migrations you usually want repo-2&#039;s files under a folder such as <code>repo-2\/<\/code> instead of mixed into the root. There are two common approaches.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Option 1: <code>git subtree<\/code><\/strong>. This is included with most Git installations. It adds the content under a prefix and keeps history, unless you pass <code>--squash<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">git fetch second_remote\ngit subtree add --prefix=repo-2 second_remote main<\/code><\/span><\/pre>\n\n\n<p class=\"wp-block-paragraph\"><strong>Option 2: rewrite paths with <code>git filter-repo<\/code>, then merge<\/strong>. This is a separate tool that you install yourself. It rewrites repo-2&#039;s history so every commit already lives under <code>repo-2\/<\/code>, which makes <code>git log<\/code> and <code>git blame<\/code> on the moved files work naturally. Always run it on a fresh, throwaway clone:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">git <span class=\"hljs-keyword\">clone<\/span> git@bitbucket.org:my-workspace\/repo<span class=\"hljs-number\">-2.<\/span>git \/tmp\/repo<span class=\"hljs-number\">-2<\/span>-rewrite\ncd \/tmp\/repo<span class=\"hljs-number\">-2<\/span>-rewrite\ngit filter-repo --to-subdirectory-filter repo<span class=\"hljs-number\">-2<\/span>\n\ncd \/path\/to\/repo<span class=\"hljs-number\">-1<\/span>\ngit remote add repo2_local \/tmp\/repo<span class=\"hljs-number\">-2<\/span>-rewrite\ngit fetch repo2_local\ngit merge repo2_local\/main --allow-unrelated-histories -m <span class=\"hljs-string\">\"Import repo-2 into repo-2\/\"<\/span>\ngit remote remove repo2_local<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\"><strong>Note:<\/strong> <code>git filter-repo<\/code> rewrites commit IDs. Never run it in a clone that you will push back to the original repo-2 remote.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common errors and fixes<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Error<\/th><th>Cause<\/th><th>Fix<\/th><\/tr><\/thead><tbody><tr><td><code>fatal: refusing to merge unrelated histories<\/code><\/td><td>The repos share no common commit<\/td><td>Add <code>--allow-unrelated-histories<\/code> to the merge command<\/td><\/tr><tr><td><code>error: remote second_remote already exists.<\/code><\/td><td>The remote name is already in use<\/td><td>Use another name, or update it with <code>git remote set-url second_remote &lt;url&gt;<\/code><\/td><\/tr><tr><td><code>Permission denied (publickey)<\/code><\/td><td>No SSH key with access to repo-2 is loaded<\/td><td>Add your SSH key to the Bitbucket\/GitHub account, or use the HTTPS URL<\/td><\/tr><tr><td><code>merge: second_remote\/feature2-branch - not something we can merge<\/code><\/td><td>The branch wasn&#039;t fetched or the name is wrong<\/td><td>Run <code>git fetch second_remote<\/code> and check names with <code>git branch -r<\/code><\/td><\/tr><tr><td><code>! [rejected] main -&gt; main (fetch first)<\/code> on push<\/td><td>origin\/main moved while you were merging<\/td><td>Run <code>git pull origin main<\/code>, resolve any conflicts, then push again<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Best practices<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Do the merge on a new branch (for example <code>git switch -c merge-repo-2<\/code>) and open a pull request instead of pushing straight to <code>main<\/code>. This gives reviewers and CI a chance to catch problems.<\/li>\n\n\n<li>Before you start, check that both working trees are clean and both repos are up to date.<\/li>\n\n\n<li>Remove the extra remote when you are done with <code>git remote remove second_remote<\/code>, so no one pushes to it by mistake.<\/li>\n\n\n<li>Before running the merge, search repo-2 for secrets or large binaries. Once merged, they become part of repo-1&#039;s history and are hard to remove.<\/li>\n\n\n<li>Use descriptive merge commit messages that name the source repository and branch, so the import is easy to find later.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">For branching strategies around merges like this, see the <a href=\"https:\/\/www.devopsschool.com\/blog\/git-workflow-master-tutorial\/\">Git Workflow Master Tutorial<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Frequently asked questions<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Does merging two Git repositories keep the commit history?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. Adding the second repo as a remote and running <code>git merge --allow-unrelated-histories<\/code> keeps every commit, author and date from both repositories. History is only lost if you squash, for example with <code>git subtree add --squash<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Why does Git say refusing to merge unrelated histories?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Since Git 2.9, Git refuses by default to merge branches that have no common ancestor, to prevent accidental merges. Add <code>--allow-unrelated-histories<\/code> when you really do mean to combine two independent repositories.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Can I merge only one folder from another repository?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. In a fresh clone of the source repo, run <code>git filter-repo --path folder\/<\/code> to keep only that folder&#039;s history. Then add that clone as a remote and merge from it with <code>--allow-unrelated-histories<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Do I need to create a local tracking branch before merging from another remote?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">No. After <code>git fetch second_remote<\/code>, you can merge <code>second_remote\/branch-name<\/code> directly into your checked-out branch. A local tracking branch is only useful if you want to review or change the code first.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How do I undo a merge from another repository?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">If the merge isn&#039;t finished yet, run <code>git merge --abort<\/code>. If it is committed but not pushed, <code>git reset --hard ORIG_HEAD<\/code> returns you to the state before the merge. If it has already been pushed, use <code>git revert -m 1 &lt;merge-commit&gt;<\/code> instead of rewriting shared history.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Related tutorials<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/git-workflow-master-tutorial\/\">Git Workflow Master Tutorial<\/a><\/li>\n\n\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/category\/devops\/git\/\">More Git tutorials<\/a><\/li>\n\n\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/top-50-interview-question-and-answers-of-git\/\">Top 50 Interview question And answers of Git<\/a><\/li>\n\n\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/what-is-git-and-use-cases-of-git\/\">What is Git and use cases of Git?<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>To merge code from one Git repository into another, add the second repository as a remote, fetch it, and merge its branch into your target branch with&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[38],"tags":[5021],"class_list":["post-32320","post","type-post","status-publish","format-standard","hentry","category-git","tag-git-merge"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/32320","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=32320"}],"version-history":[{"count":2,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/32320\/revisions"}],"predecessor-version":[{"id":80456,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/32320\/revisions\/80456"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=32320"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=32320"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=32320"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}