{"id":3323,"date":"2023-12-16T11:24:07","date_gmt":"2023-12-16T11:24:07","guid":{"rendered":"https:\/\/www.itersdesktop.com\/?p=3323"},"modified":"2023-12-16T14:48:18","modified_gmt":"2023-12-16T14:48:18","slug":"move-a-subdirectory-into-a-new-git-repository","status":"publish","type":"post","link":"https:\/\/www.itersdesktop.com\/fr\/2023\/12\/16\/move-a-subdirectory-into-a-new-git-repository\/","title":{"rendered":"Move a subdirectory into a new git repository"},"content":{"rendered":"<p><\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-light-blue ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a2bf1a681cad\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a2bf1a681cad\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.itersdesktop.com\/fr\/2023\/12\/16\/move-a-subdirectory-into-a-new-git-repository\/#context\" >Context<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.itersdesktop.com\/fr\/2023\/12\/16\/move-a-subdirectory-into-a-new-git-repository\/#steps\" >Steps<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.itersdesktop.com\/fr\/2023\/12\/16\/move-a-subdirectory-into-a-new-git-repository\/#step-01-create-a-new-branch-in-the-oldbig-repository\" >Step 01: Create a new branch in the old\/big repository<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.itersdesktop.com\/fr\/2023\/12\/16\/move-a-subdirectory-into-a-new-git-repository\/#step-02-create-a-new-repository-and-pull-all-commits\" >Step 02: Create a new repository and pull all commits<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h1><span class=\"ez-toc-section\" id=\"context\"><\/span>Context<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>In the real world, we may probably have a large Git repository where you want to move out some sub-directories into single Git repositories. Regarding the Git repository, we likely want to preserve old commits or commits relevant to those directories during the movement. Of course, we cannot move them using the standard way of moving files\/directories as usual because the Git histories won&rsquo;t be moved along with the files. Fortunately, Git supports us to do it by implementing <code>subtree split<\/code> command.<\/p>\n<p>We will exercise with one of our GitHub repositories: https:\/\/github.com\/ITersDesktop\/devops although it is not a big repository. To ease your reading, we are supposed to move the <code>K8s<\/code> directory out of the repository.<\/p>\n<h1><span class=\"ez-toc-section\" id=\"steps\"><\/span>Steps<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>Thanks for the <a href=\"https:\/\/stackoverflow.com\/questions\/359424\/detach-move-subdirectory-into-separate-git-repository\">answer<\/a>, we demonstrate the step-by-step procedure to move the <code>K8s<\/code> directory from the GitHub repository above and create a new one.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"step-01-create-a-new-branch-in-the-oldbig-repository\"><\/span>Step 01: Create a new branch in the old\/big repository<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">~\/tmp &gt; cd devops \r\n~\/tmp\/devops &gt; git subtree split -P K8s -b k8s \r\nCreated branch 'k8s' \r\n03d9101f8b5647da94c4b4387660746cfb45bca4<\/pre>\n<p><strong>Note:<\/strong>\u00a0<code>&lt;name-of-folder&gt;<\/code>\u00a0must NOT contain leading or trailing characters. For instance, the folder named\u00a0<code>subproject<\/code>\u00a0MUST be passed as\u00a0<code>subproject<\/code>, NOT\u00a0<code>.\/subproject\/<\/code><\/p>\n<p><strong>Note:<\/strong>\u00a0<code>&lt;name-of-new-branch&gt;<\/code>\u00a0is a branch you will be creating in the existing\/old repo,\u00a0<strong>NOT<\/strong>\u00a0the new one [that comes later].<\/p>\n<p><strong>Note for Windows users:<\/strong>\u00a0When your folder depth is &gt; 1,\u00a0<code>&lt;name-of-folder&gt;<\/code> you must have *nix style folder separator (\/). For instance, the folder named\u00a0<code>path1\\path2\\subproject<\/code>\u00a0MUST be passed as\u00a0<code>path1\/path2\/subproject<\/code><\/p>\n<h2><span class=\"ez-toc-section\" id=\"step-02-create-a-new-repository-and-pull-all-commits\"><\/span>Step 02: Create a new repository and pull all commits<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">~\/tmp\/devops &gt; cd ..\/\r\n ~\/tmp &gt; mkdir k8s\r\n ~\/tmp &gt; cd k8s\r\n ~\/tmp\/k8s &gt; git init\r\nInitialized empty Git repository in \/Users\/tnguyen\/tmp\/k8s\/.git\/\r\n ~\/tmp\/k8s &gt; git pull ~\/tmp\/devops k8s\r\nremote: Enumerating objects: 12, done.\r\nremote: Counting objects: 100% (12\/12), done.\r\nremote: Compressing objects: 100% (5\/5), done.\r\nremote: Total 12 (delta 3), reused 8 (delta 3), pack-reused 0\r\nUnpacking objects: 100% (12\/12), 3.60 KiB | 263.00 KiB\/s, done.\r\nFrom \/Users\/tnguyen\/tmp\/devops\r\n * branch            k8s        -&gt; FETCH_HEAD<\/pre>\n<p>To clarify the result, we run <code>git log<\/code> or <code>git log --oneline<\/code> to see all the commit messages. Also, by running these commands on the old\/big repository we can check all commits relevant to the <code>K8s<\/code> directory.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-4069\" src=\"https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2023\/12\/3323-commits-messages-new-repo.png\" alt=\"\" width=\"1164\" height=\"248\" srcset=\"https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2023\/12\/3323-commits-messages-new-repo.png 1164w, https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2023\/12\/3323-commits-messages-new-repo-300x64.png 300w, https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2023\/12\/3323-commits-messages-new-repo-1024x218.png 1024w, https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2023\/12\/3323-commits-messages-new-repo-768x164.png 768w\" sizes=\"auto, (max-width: 1164px) 100vw, 1164px\" \/>There have been four commits as of writing these lines. If we look into the long list of commit messages of the original repo as the screenshot below, we can find out where these commit messages come. <img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-4068\" src=\"https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2023\/12\/3323-commits-messges-big-repo.png\" alt=\"\" width=\"1510\" height=\"632\" srcset=\"https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2023\/12\/3323-commits-messges-big-repo.png 1510w, https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2023\/12\/3323-commits-messges-big-repo-300x126.png 300w, https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2023\/12\/3323-commits-messges-big-repo-1024x429.png 1024w, https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2023\/12\/3323-commits-messges-big-repo-768x321.png 768w\" sizes=\"auto, (max-width: 1510px) 100vw, 1510px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>This just copies commits relevant to the specific subdirectory.<\/p>","protected":false},"excerpt":{"rendered":"<p>Context In the real world, we may probably have a large Git repository where you want to move out some sub-directories into single Git repositories. Regarding the Git repository, we&hellip; <\/p>\n","protected":false},"author":2,"featured_media":4074,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[611,726],"tags":[610,728,727,592],"class_list":["post-3323","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-git","category-gitlab","tag-git","tag-git-subtree","tag-hands-on-git-guides","tag-huong-dan-su-dung-git"],"_links":{"self":[{"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/posts\/3323","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/comments?post=3323"}],"version-history":[{"count":9,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/posts\/3323\/revisions"}],"predecessor-version":[{"id":4076,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/posts\/3323\/revisions\/4076"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/media\/4074"}],"wp:attachment":[{"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/media?parent=3323"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/categories?post=3323"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/tags?post=3323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}