{"id":5340,"date":"2026-08-30T15:50:34","date_gmt":"2026-08-30T06:50:34","guid":{"rendered":"https:\/\/donguri3.net\/server-tech\/git-clone-specific-branch-2\/"},"modified":"2026-08-30T15:50:35","modified_gmt":"2026-08-30T06:50:35","slug":"git-clone-specific-branch","status":"publish","type":"post","link":"https:\/\/donguri3.net\/en\/server-tech\/development-programming-git\/git-clone-specific-branch\/","title":{"rendered":"How to Clone Only a Specific Branch in Git"},"content":{"rendered":"<p>Today, we are going to dive a bit deeper into how Git repositories are handled.<\/p>\n<p>When someone tells you to &#8220;clone the repository,&#8221; do you just run <code>git clone [URL]<\/code> without thinking?<br \/>\nFor a small personal project, that&#8217;s totally fine. However, doing that on a long-running project or a poorly managed repository is a waste of time and resources.<\/p>\n<p>In this article, we&#8217;ll introduce a technique to &#8220;bring over only the branch you actually need, right where you need it.&#8221;<\/p>\n<h2>Putting Bloated Repositories on a Diet<\/h2>\n<p>Why would you need to clone only a specific branch?<br \/>\nThe biggest reason is &#8220;repository bloat caused by past mistakes.&#8221;<\/p>\n<h3>Accidents happen in development environments.<\/h3>\n<ul>\n<li>Massive test video files that were accidentally committed<\/li>\n<li><code>node_modules<\/code> or <code>vendor<\/code> directories left in the history due to missing <code>.gitignore<\/code> settings<\/li>\n<li>High-resolution design assets that are no longer in use<\/li>\n<\/ul>\n<p>These may have already been deleted and no longer exist in the current <code>main<\/code> branch. However, due to how Git works, a standard <code>clone<\/code> will download all of this garbage data as &#8220;past history from other branches that are no longer in use.&#8221;<br \/>\nConsuming gigabytes of network bandwidth and disk space for files irrelevant to the branch you want to work on right now is a waste.<\/p>\n<h3>Getting Only the Code You Need, as Fast as Possible<\/h3>\n<p>Our goal today is simple:<\/p>\n<ol>\n<li>Target: Retrieve data only for the &#8220;specific branch&#8221; you need right now.<\/li>\n<li>Download Time: Significantly reduced compared to a full clone.<\/li>\n<li>Disk Usage: Keep past unnecessary data down to 0 bytes.<\/li>\n<\/ol>\n<p>Especially when developing in metered CI\/CD environments, VPS with limited storage, or IoT devices like Raspberry Pis, this &#8220;diet&#8221; directly impacts cost and performance.<\/p>\n<h3>The Tragedy of &#8220;Just Full-Clone Everything&#8221;<\/h3>\n<p>Cloning all data without thinking comes with the following disadvantages:<\/p>\n<ul>\n<li>Wasted Waiting Time: In slow network environments or when tethering, downloading gigabyte-scale data is fatal.<\/li>\n<li>CI\/CD Bottlenecks: Every time a pipeline runs, unrelated history is fetched, increasing the waiting time until the build starts and driving up cloud execution costs.<\/li>\n<li>Increased Noise: Running <code>git branch -a<\/code> displays hundreds of lines of work branches left behind by people you don&#8217;t even know. This increases cognitive load and causes human errors (such as merging into the wrong branch).<\/li>\n<\/ul>\n<p>Your toolbox should be organized with only the tools you use right now.<\/p>\n<h3>[The Solution] The Combination of <code>-b<\/code> and <code>--single-branch<\/code><\/h3>\n<p>Let&#8217;s get straight to the point. Use the following command:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">git clone -b &lt;branch-name&gt; --single-branch &lt;repository-URL&gt;<\/pre>\n<h3>Anatomy of the Options<\/h3>\n<p>Many people are satisfied with just <code>-b<\/code> (specifying the branch), but that alone is not enough.<\/p>\n<ul>\n<li><code>-b &lt;branch-name&gt;<\/code>: This merely checks out that branch after the clone completes. Under the hood, it is still downloading data for all branches.<\/li>\n<li><code>--single-branch<\/code>: This is the main event. It restricts the download target to &#8220;only the history of the specified branch.&#8221; Information about other branches is not even saved in the <code>.git<\/code> directory.<\/li>\n<\/ul>\n<h3>Execution Example<\/h3>\n<p>For instance, if you only need a branch named <code>fix\/nando-patch<\/code>:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">git clone -b fix\/nando-patch --single-branch https:\/\/github.com\/user\/repo.git<\/pre>\n<p>With this, massive legacy files lurking in other branches will not be downloaded at all.<\/p>\n<h2>Common Misconceptions and Recovery Methods<\/h2>\n<p>You might worry, &#8220;Does using this option make things inconvenient?&#8221; Let&#8217;s clear that up right now.<\/p>\n<h3>Q1. Can I create a new branch from this state?<\/h3>\n<p>A. Yes, you can.<br \/>\nA lot of people misunderstand this, but the only restriction is &#8220;viewing other people&#8217;s remote branches.&#8221;<br \/>\nThere is absolutely no problem with creating a derived branch using <code>git checkout -b my-new-feature<\/code> based on the code you brought locally and proceeding with your work.<\/p>\n<h3>Q2. What if I later decide, &#8220;Actually, I want to see all the branches after all&#8221;?<\/h3>\n<p>A. You can revert it by rewriting the settings.<br \/>\nIf development progresses and you find you truly need information from other branches, you can lift the restrictions with the following steps:<\/p>\n<p>1. Rewrite the tracking target in the config to &#8220;all branches&#8221;<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">git config remote.origin.fetch &quot;+refs\/heads\/*:refs\/remotes\/origin\/*&quot;<\/pre>\n<p>2. Fetch the information again<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">git fetch origin<\/pre>\n<p>This will return the repository to the same state as a normal one. Rest assured, this is not an irreversible operation.<\/p>\n<h2>Conclusion<\/h2>\n<p>Simplicity is the ultimate efficiency. Criteria for choosing your approach:<\/p>\n<ul>\n<li>Small-scale personal development: Standard <code>git clone<\/code> is fine.<\/li>\n<li>CI\/CD and Deployments: Essential. Make it as fast and lightweight as possible, even by a second.<\/li>\n<li>Long-running, massive repositories: Essential. To keep legacy files out of your local environment.<\/li>\n<\/ul>\n<p>Git is a convenient tool, but using it mindlessly impairs your Developer Experience (DX). &#8220;Only what you need, exactly when you need it.&#8221; This is a golden rule that applies equally to DIY and server management.<br \/>\nBe sure to register this in your local aliases and maintain a clean development environment free of wasted data.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today, we are going to dive a bit deeper into how Git repositories are handled. When someone tells you to &#038;#82 [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":2669,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_locale":"en_US","_original_post":"https:\/\/donguri3.net\/?p=2667","footnotes":""},"categories":[1172],"tags":[1072,9,51,52,835,10,104],"class_list":["post-5340","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development-programming-git","tag-cli","tag-diy","tag-git","tag-https","tag-tips","tag-server","tag-104","en-US"],"_links":{"self":[{"href":"https:\/\/donguri3.net\/wp-json\/wp\/v2\/posts\/5340","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/donguri3.net\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/donguri3.net\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/donguri3.net\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/donguri3.net\/wp-json\/wp\/v2\/comments?post=5340"}],"version-history":[{"count":1,"href":"https:\/\/donguri3.net\/wp-json\/wp\/v2\/posts\/5340\/revisions"}],"predecessor-version":[{"id":5343,"href":"https:\/\/donguri3.net\/wp-json\/wp\/v2\/posts\/5340\/revisions\/5343"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/donguri3.net\/wp-json\/wp\/v2\/media\/2669"}],"wp:attachment":[{"href":"https:\/\/donguri3.net\/wp-json\/wp\/v2\/media?parent=5340"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/donguri3.net\/wp-json\/wp\/v2\/categories?post=5340"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/donguri3.net\/wp-json\/wp\/v2\/tags?post=5340"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}