<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>worktree &amp;mdash; sandeepk</title>
    <link>https://blogs.dgplug.org/sandeepk/tag:worktree</link>
    <description></description>
    <pubDate>Thu, 30 Apr 2026 21:29:42 +0000</pubDate>
    <item>
      <title>Git Worktree</title>
      <link>https://blogs.dgplug.org/sandeepk/git-worktree</link>
      <description>&lt;![CDATA[A few days ago, during our office knowledge-sharing meeting, someone introduced the git worktree command. It lets you create a new branch parallel to your current working branch so you can start something new — or handle a hotfix — without stashing or committing your unfinished changes.&#xA;&#xA;It turned out to be incredibly useful. With git worktree, you can maintain multiple working directories linked to the same Git repository with almost no friction.&#xA;&#xA;---&#xA;&#xA;Why use worktrees?&#xA;&#xA;Imagine you&#39;re working on a long-running feature — say, an optimization — and suddenly you’re assigned an urgent production bug.&#xA;Typically, you would stash your changes or make a temporary commit, switch branches, fix the bug, then restore everything. It&#39;s annoying and error-prone.&#xA;&#xA;With worktrees, you can directly spin up a parallel working directory:&#xA;&#xA;git worktree add path&#xA;Example:&#xA;git worktree add ../hotfix&#xA;&#xA;This creates a new linked worktree, associated with your current repository, with its own metadata and branch checkout. Your original work remains untouched.&#xA;&#xA;---&#xA;&#xA;Removing a worktree&#xA;&#xA;Once you&#39;re done with the hotfix (or any task), removing the worktree is just as simple:&#xA;&#xA;git worktree remove path&#xA;&#xA;If you delete the directory manually, Git will eventually clean up its administrative files automatically (based on gc.worktreePruneExpire in git-config).&#xA;You can also remove stale entries explicitly:&#xA;&#xA;git worktree prune&#xA;&#xA;---&#xA;&#xA;Other useful worktree commands&#xA;&#xA;1. Create a throwaway worktree (detached HEAD)&#xA;&#xA;Perfect for quick experiments:&#xA;&#xA;git worktree add -d path&#xA;&#xA;2. Create a worktree for an existing branch&#xA;&#xA;git worktree add path branch&#xA;&#xA;This checks out the given branch into a new, isolated working directory.&#xA;&#xA;---&#xA;&#xA;Further reading&#xA;&#xA;To dive deeper into git worktree:&#xA;&#xA;Official docs: https://git-scm.com/docs/git-worktree&#xA;Or simply run:&#xA;&#xA;    git help worktree&#xA;  &#xA;&#xA;Cheers!&#xA;&#xA;#Git #TIL #Worktree]]&gt;</description>
      <content:encoded><![CDATA[<p>A few days ago, during our office knowledge-sharing meeting, someone introduced the <code>git worktree</code> command. It lets you create a new branch <em>parallel</em> to your current working branch so you can start something new — or handle a hotfix — <strong>without stashing or committing your unfinished changes</strong>.</p>

<p>It turned out to be incredibly useful. With <code>git worktree</code>, you can maintain multiple working directories linked to the same Git repository with almost no friction.</p>

<hr>

<h2 id="why-use-worktrees">Why use worktrees?</h2>

<p>Imagine you&#39;re working on a long-running feature — say, an optimization — and suddenly you’re assigned an urgent production bug.
Typically, you would stash your changes or make a temporary commit, switch branches, fix the bug, then restore everything. It&#39;s annoying and error-prone.</p>

<p>With worktrees, you can directly spin up a parallel working directory:</p>

<pre><code class="language-bash">git worktree add &lt;path&gt;
# Example:
git worktree add ../hotfix
</code></pre>

<p>This creates a <strong>new linked worktree</strong>, associated with your current repository, with its own metadata and branch checkout. Your original work remains untouched.</p>

<hr>

<h2 id="removing-a-worktree">Removing a worktree</h2>

<p>Once you&#39;re done with the hotfix (or any task), removing the worktree is just as simple:</p>

<pre><code class="language-bash">git worktree remove &lt;path&gt;
</code></pre>

<p>If you delete the directory manually, Git will eventually clean up its administrative files automatically (based on <code>gc.worktreePruneExpire</code> in <code>git-config</code>).
You can also remove stale entries explicitly:</p>

<pre><code class="language-bash">git worktree prune
</code></pre>

<hr>

<h2 id="other-useful-worktree-commands">Other useful worktree commands</h2>

<h3 id="1-create-a-throwaway-worktree-detached-head">1. Create a throwaway worktree (detached HEAD)</h3>

<p>Perfect for quick experiments:</p>

<pre><code class="language-bash">git worktree add -d &lt;path&gt;
</code></pre>

<h3 id="2-create-a-worktree-for-an-existing-branch">2. Create a worktree for an existing branch</h3>

<pre><code class="language-bash">git worktree add &lt;path&gt; &lt;branch&gt;
</code></pre>

<p>This checks out the given branch into a new, isolated working directory.</p>

<hr>

<h2 id="further-reading">Further reading</h2>

<p>To dive deeper into <code>git worktree</code>:</p>
<ul><li>Official docs: <a href="https://git-scm.com/docs/git-worktree" rel="nofollow">https://git-scm.com/docs/git-worktree</a></li>
<li>Or simply run:</li></ul>

<pre><code class="language-bash">  git help worktree
</code></pre>

<p>Cheers!</p>

<p><a href="/sandeepk/tag:Git" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">Git</span></a> <a href="/sandeepk/tag:TIL" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">TIL</span></a> <a href="/sandeepk/tag:Worktree" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">Worktree</span></a></p>
]]></content:encoded>
      <guid>https://blogs.dgplug.org/sandeepk/git-worktree</guid>
      <pubDate>Sun, 30 Nov 2025 13:27:11 +0000</pubDate>
    </item>
  </channel>
</rss>