<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Vivek Maskara</title>
    <description>The latest articles on DEV Community by Vivek Maskara (@maskaravivek).</description>
    <link>https://dev.to/maskaravivek</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F278060%2Fea5e9f8c-b600-43e5-b6e7-62f485e360dc.jpeg</url>
      <title>DEV Community: Vivek Maskara</title>
      <link>https://dev.to/maskaravivek</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/maskaravivek"/>
    <language>en</language>
    <item>
      <title>You’re Already Using Git Worktrees. You Should Understand Them.</title>
      <dc:creator>Vivek Maskara</dc:creator>
      <pubDate>Mon, 18 May 2026 12:18:41 +0000</pubDate>
      <link>https://dev.to/maskaravivek/youre-already-using-git-worktrees-you-should-understand-them-4nh7</link>
      <guid>https://dev.to/maskaravivek/youre-already-using-git-worktrees-you-should-understand-them-4nh7</guid>
      <description>&lt;p&gt;If you’ve ever run &lt;a href="https://git-scm.com/docs/git-clone" rel="noopener noreferrer"&gt;&lt;code&gt;git clone&lt;/code&gt;&lt;/a&gt; and then started editing files in the resulting folder, you’ve been using a Git worktree all along. Git just doesn’t call it that in day-to-day conversation.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git worktree&lt;/code&gt; is the feature that lets you add &lt;em&gt;more&lt;/em&gt; checkouts (more folders with files on disk) that all share the same underlying repository history. Done well, it replaces the “stash/switch/stash-back” dance with something simpler: keep your in-progress work open in one directory, and do the urgent/clean/experimental thing in another.&lt;/p&gt;

&lt;p&gt;This is a mental-model article: what a worktree &lt;em&gt;is&lt;/em&gt;, why Git enforces “one branch per worktree,” and the few lifecycle commands that keep worktrees from turning into confusing “already checked out” / stale-metadata problems.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Mental Model: Shared History, Separate Working State
&lt;/h2&gt;

&lt;p&gt;Git’s own &lt;a href="https://git-scm.com/docs/git-worktree" rel="noopener noreferrer"&gt;&lt;code&gt;git worktree&lt;/code&gt; docs&lt;/a&gt; describe the feature as managing “multiple working trees attached to the same repository.” The twist is that you always start with one: the directory you land in after a normal (non-bare) clone is the &lt;strong&gt;main worktree&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Linked worktrees are additional working directories attached to the same repo. They &lt;strong&gt;share&lt;/strong&gt; the object database (commits/trees/blobs), but each worktree has its own working state: checked-out files, its own &lt;a href="https://git-scm.com/docs/gitglossary#def_HEAD" rel="noopener noreferrer"&gt;&lt;code&gt;HEAD&lt;/code&gt;&lt;/a&gt;, and its own index (staging area).&lt;/p&gt;

&lt;p&gt;That’s the whole trick:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Shared:&lt;/strong&gt; history and objects (you don’t redownload Git data like a second clone).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Per worktree:&lt;/strong&gt; files-on-disk + &lt;code&gt;HEAD&lt;/code&gt; + index (you don’t stomp on your other checkout).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;One nuance that matters: a worktree isn’t its own independent “repo.” Most refs and remote configuration are shared. When you &lt;code&gt;git fetch&lt;/code&gt;, you’re updating the shared repository data that all worktrees see. That’s what makes worktrees lighter than multiple clones—but it’s also why Git has to be careful about how a branch name maps to a checkout.&lt;/p&gt;

&lt;p&gt;If you want a deeper explanation of why “objects live once” makes this efficient, the Git book’s “Git Objects” chapter is a good reference: &lt;a href="https://git-scm.com/book/en/v2/Git-Internals-Git-Objects" rel="noopener noreferrer"&gt;Git Internals: Git Objects&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh9evyu4mask5mlpklcqw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh9evyu4mask5mlpklcqw.png" alt="Diagram" width="800" height="250"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  What Actually Changes on Disk when You Add a Worktree
&lt;/h3&gt;

&lt;p&gt;Git keeps the shared repository database in one place, then tracks per-worktree state separately. That’s why it can add a second checkout without cloning.&lt;/p&gt;

&lt;p&gt;If you’ve ever noticed that &lt;code&gt;.git&lt;/code&gt; sometimes looks like a &lt;em&gt;file&lt;/em&gt; (not a folder), that’s often Git pointing your working tree at the real “gitdir.” Worktrees lean on this idea heavily: each worktree has its own admin area, but the objects are shared.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Rule that Surprises People: One Branch per Worktree
&lt;/h2&gt;

&lt;p&gt;If you try to attach the &lt;em&gt;same&lt;/em&gt; branch to two worktrees, Git will usually refuse with “branch is already checked out.” That refusal is protection.&lt;/p&gt;

&lt;p&gt;The core reason is that a branch name is just a movable pointer. If two working directories could both “be” &lt;code&gt;feature&lt;/code&gt;, the branch pointer could move in one directory while the other directory still has older files on disk. Now you have a branch name that no longer uniquely identifies a single working state.&lt;/p&gt;

&lt;p&gt;Said differently: Git is fine with multiple &lt;em&gt;worktrees&lt;/em&gt; having different &lt;code&gt;HEAD&lt;/code&gt;s (each checkout is independent), but a branch ref like &lt;code&gt;refs/heads/feature&lt;/code&gt; is shared at the repository level. Letting two worktrees both “own” the same branch would create a footgun where “what commit is &lt;code&gt;feature&lt;/code&gt;?” depends on which folder last advanced it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fatr4hsx8e1ia3h6vok8u.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fatr4hsx8e1ia3h6vok8u.gif" alt="Git worktree visualized" width="640" height="360"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When you want “two directories, same starting point,” Git’s safer options are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Create a new branch name&lt;/strong&gt; for the second worktree:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git worktree add ../wt-fix &lt;span class="nt"&gt;-b&lt;/span&gt; fix-branch
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Use a detached &lt;code&gt;HEAD&lt;/code&gt;&lt;/strong&gt; when you don’t intend to keep the work long-term:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git worktree add &lt;span class="nt"&gt;-d&lt;/span&gt; ../wt-scratch
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Detached &lt;code&gt;HEAD&lt;/code&gt; is the “I want another directory at this commit, but I’m not claiming a branch name” mode. That’s why it’s safe: you can still make commits, but you’re not advancing a shared branch ref unless you later create one.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;--force&lt;/code&gt; exists for edge cases, but it’s opting out of a guardrail. If you don’t already know &lt;em&gt;why&lt;/em&gt; you need it, you probably don’t.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where Worktrees Shine (in Practice)
&lt;/h2&gt;

&lt;p&gt;Worktrees are a productivity primitive whenever parallelism beats context switching. Three common patterns:&lt;/p&gt;

&lt;h3&gt;
  
  
  1) Hotfix without Touching Your In-Progress Directory
&lt;/h3&gt;

&lt;p&gt;When a production issue lands mid-feature, create a new worktree on a hotfix branch:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git worktree add ../wt-hotfix &lt;span class="nt"&gt;-b&lt;/span&gt; hotfix
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You get a clean checkout in a new folder, and your original directory stays exactly as it was. When you’re done, remove it cleanly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git worktree remove ../wt-hotfix
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://www.git-tower.com/learn/git/faq/git-worktree" rel="noopener noreferrer"&gt;Tower’s Git worktree FAQ&lt;/a&gt; describes this as one of the “default” worktree use cases, and it’s the one most people feel immediately.&lt;/p&gt;

&lt;h3&gt;
  
  
  2) PR Reviews and Repros in a Clean Sandbox
&lt;/h3&gt;

&lt;p&gt;You can attach a worktree directly to a remote-tracking branch and keep it around for as long as the review takes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git worktree add ../wt-review origin/some-pr-branch
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3) Scratch Experiments You Don’t Want to Keep
&lt;/h3&gt;

&lt;p&gt;Detached worktrees are great for “let me try something” work without committing to a branch name yet:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git worktree add &lt;span class="nt"&gt;-d&lt;/span&gt; ../wt-scratch
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you end up making commits you want to keep, create a branch before you delete the folder so you don’t lose track of them.&lt;/p&gt;

&lt;h3&gt;
  
  
  A Quick Note on “Why Not Just Clone Twice?”
&lt;/h3&gt;

&lt;p&gt;Separate clones can absolutely solve the same “two directories” problem. The difference is mainly cost and coupling:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Clones duplicate setup (another fetch/clone, another &lt;code&gt;.git&lt;/code&gt; object store).&lt;/li&gt;
&lt;li&gt;Worktrees share Git objects, but still behave like separate checkouts.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you need complete isolation (different remotes, radically different Git config, or you want to move one copy around freely), a second clone can still be the simplest answer.&lt;/p&gt;

&lt;h2&gt;
  
  
  Keeping Your Directories Sane
&lt;/h2&gt;

&lt;p&gt;The simplest convention (and the one that avoids the “oops, I edited the wrong checkout” mistake) is: keep linked worktrees &lt;em&gt;outside&lt;/em&gt; the main project folder.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;repo/            # main worktree
repo-hotfix/     # linked worktree
repo-review/     # linked worktree
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you do this often, encode intent in the directory name so it’s searchable later (&lt;code&gt;wt-hotfix-1234&lt;/code&gt;, &lt;code&gt;wt-pr-998&lt;/code&gt;, &lt;code&gt;wt-bisect-crash&lt;/code&gt;) instead of generic &lt;code&gt;tmp/&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If you want all worktrees contained under one parent folder, a popular layout is “bare repo + many worktrees” (see &lt;a href="https://nicknisi.com/posts/git-worktrees/" rel="noopener noreferrer"&gt;Nick Nisi&lt;/a&gt; and &lt;a href="https://gabri.me/blog/git-worktrees-done-right" rel="noopener noreferrer"&gt;Ahmed El Gabri&lt;/a&gt;). For background on bare repositories themselves, the Git book chapter is a good reference: &lt;a href="https://git-scm.com/book/en/v2/Git-Basics-Getting-a-Git-Repository" rel="noopener noreferrer"&gt;Git Basics: Getting a Git Repository&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fajmkw6cycj8yplij2ayq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fajmkw6cycj8yplij2ayq.png" alt="a conceptual folder layout illustration comparing “sibling worktrees” vs “bare repo + worktrees inside one folder.”" width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you try the “bare repo + worktrees” approach, keep the tradeoff in mind: you’re saving Git object duplication, not environment duplication. Two worktrees can still mean two &lt;code&gt;node_modules/&lt;/code&gt; folders, two build outputs, and two IDE indexes. If setup dominates your workflow, keep only the worktrees you’re actively using.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Lifecycle Gotchas (and the 5 Commands that Prevent Them)
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Removing vs Deleting (Stale Metadata)
&lt;/h3&gt;

&lt;p&gt;Deleting a worktree folder in Finder isn’t the same as telling Git it’s gone. Prefer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git worktree remove ../wt-review
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you already deleted the folder, clean up the leftover admin state:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git worktree prune
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Git can also garbage-collect stale worktree metadata automatically over time (controlled by &lt;code&gt;gc.worktreePruneExpire&lt;/code&gt;). That’s helpful, but it’s not a substitute for removing worktrees intentionally when you’re done.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmqw33vx4izxlx2nk84f5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmqw33vx4izxlx2nk84f5.png" alt="Diagram" width="800" height="544"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  “This Branch Is Already Checked Out”
&lt;/h3&gt;

&lt;p&gt;This is Git enforcing “one branch per worktree.” Your usual fixes are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;make a new branch name (&lt;code&gt;-b&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;use detached &lt;code&gt;HEAD&lt;/code&gt; (&lt;code&gt;-d&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you’re debugging “why won’t Git let me…”, list the current attachments:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git worktree list
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Moving Worktrees (and Repairing when Paths Change)
&lt;/h3&gt;

&lt;p&gt;If you need to reorganize, prefer Git’s commands over dragging folders around in your file manager.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;git worktree move &amp;lt;worktree&amp;gt; &amp;lt;new-path&amp;gt;&lt;/code&gt; relocates a linked worktree (you can’t move the main worktree).&lt;/li&gt;
&lt;li&gt;If you moved things manually and links broke, &lt;code&gt;git worktree repair&lt;/code&gt; can reestablish the connection in supported cases.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Intermittent Drives: Lock Before Git Prunes
&lt;/h3&gt;

&lt;p&gt;If a worktree lives on an external drive or network mount, Git may treat it as “missing” when the volume isn’t mounted. Use &lt;a href="https://git-scm.com/docs/git-worktree" rel="noopener noreferrer"&gt;&lt;code&gt;git worktree lock&lt;/code&gt;&lt;/a&gt; to prevent surprises:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git worktree lock &lt;span class="nt"&gt;--reason&lt;/span&gt; &lt;span class="s2"&gt;"On external drive"&lt;/span&gt; /Volumes/SSD/wt-release
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Cheat Sheet
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git worktree add ../wt-name &lt;span class="nt"&gt;-b&lt;/span&gt; my-branch   &lt;span class="c"&gt;# new worktree + new branch&lt;/span&gt;
git worktree add ../wt-name origin/branch  &lt;span class="c"&gt;# worktree from remote-tracking branch&lt;/span&gt;
git worktree add &lt;span class="nt"&gt;-d&lt;/span&gt; ../wt-scratch          &lt;span class="c"&gt;# detached-HEAD scratch worktree&lt;/span&gt;
git worktree list                          &lt;span class="c"&gt;# see what's attached&lt;/span&gt;
git worktree remove ../wt-name             &lt;span class="c"&gt;# remove cleanly&lt;/span&gt;
git worktree prune                         &lt;span class="c"&gt;# cleanup after manual deletion&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The Takeaway
&lt;/h2&gt;

&lt;p&gt;Worktrees aren’t a niche add-on. They’re the name for the checkout you already use, plus a way to add more checkouts without cloning again. Keep the mental model in your head—shared history, separate working state—and Git’s guardrails (like one branch per worktree) stop feeling arbitrary and start feeling helpful.&lt;/p&gt;

&lt;p&gt;If you learned something from this post, give me a follow and checkout the AI powered &lt;a href="https://mdedit.ai/" rel="noopener noreferrer"&gt;markdown editor&lt;/a&gt; that i am building on the side.&lt;/p&gt;

</description>
      <category>git</category>
      <category>gitworktree</category>
    </item>
  </channel>
</rss>
