{"id":8307,"date":"2020-07-11T16:16:58","date_gmt":"2020-07-11T16:16:58","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=8307"},"modified":"2020-07-11T16:16:58","modified_gmt":"2020-07-11T16:16:58","slug":"migrating-a-folder-from-one-git-repo-to-another","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/migrating-a-folder-from-one-git-repo-to-another\/","title":{"rendered":"Migrating a folder from one git repo to another"},"content":{"rendered":"<p>I had a situation where I had a git repo. consisting of a Java project and a C# project (a small monorepo), we decided that permissions for each project needed to differ (i.e. the admin of those projects) and maybe more importantly in a way, changes to one were causing &#8220;Pending&#8221; changes to the other within CI\/CD, in this case TeamCity.<\/p>\n<p>So we need to split the project. Ofcourse it&#8217;s easy to create a new project and copy the code, but we wanted to keep the commit history etc.<\/p>\n<p>What I&#8217;m going to list below are the steps that worked for me, but I owe a lot to this post <a href=\"https:\/\/medium.com\/@ayushya\/move-directory-from-one-repository-to-another-preserving-git-history-d210fa049d4b\" rel=\"noopener noreferrer\" target=\"_blank\">Move files from one repository to another, preserving git history<\/a>.<\/p>\n<p><strong>Use case<\/strong><\/p>\n<p>To reiterate, we have a Java library and C# library sitting in the same git code base and we want to move the C# library into it&#8217;s own repository whilst keeping the commit history.<\/p>\n<p><strong>Steps<\/strong><\/p>\n<ul>\n<li>Clone the repository (i.e. the code we&#8217;re wanting to move)<\/li>\n<li>CD into it<\/li>\n<li>From a command line, run\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\ngit remote rm origin\r\n<\/pre>\n<p>This will remove the remote url and means we&#8217;re not going to accidently commit to the original\/source repository.\n<\/li>\n<li>Now we want to filter our anything that&#8217;s not part of the code we want to keep. It&#8217;s hoped that the C# code, like ours, was in it&#8217;s own folder (otherwise things will be much more complicated). So run\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\ngit filter-branch --subdirectory-filter &lt;directory&gt; -- --all\r\n<\/pre>\n<p>Replace <directory> with the relative folder, i.e. subfolder1\/subfolder2\/FOLDER_TO_KEEP\n<\/li>\n<li>\nRun the following commands<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\ngit reset --hard\r\ngit gc --aggressive \r\ngit prune\r\ngit clean -fd\r\n<\/pre>\n<\/li>\n<li>Now, if you haven&#8217;t already create a remote repository, do so and then run\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\"> \r\ngit remote add origin &lt;YOUR REMOTE REPO&gt;\r\n<\/pre>\n<\/li>\n<p>\/\/ this should have been handled by step 6 git remote set-url origin https:\/\/youreposerver\/yourepo.git<\/p>\n<li>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\ngit push -u origin --all\r\ngit push origin --tags\r\n<\/pre>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>I had a situation where I had a git repo. consisting of a Java project and a C# project (a small monorepo), we decided that permissions for each project needed to differ (i.e. the admin of those projects) and maybe more importantly in a way, changes to one were causing &#8220;Pending&#8221; changes to the other [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[206],"tags":[],"class_list":["post-8307","post","type-post","status-publish","format-standard","hentry","category-git-2"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8307","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=8307"}],"version-history":[{"count":3,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8307\/revisions"}],"predecessor-version":[{"id":8423,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8307\/revisions\/8423"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=8307"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=8307"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=8307"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}