<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
    <title>svnscha - git</title>
    <subtitle>automating annoying tasks, sharing tips, and embracing less frustration</subtitle>
    <link rel="self" type="application/atom+xml" href="https://svnscha.de/tags/git/atom.xml"/>
    <link rel="alternate" type="text/html" href="https://svnscha.de"/>
    <generator uri="https://astro.build/">Astro</generator>
    <updated>2025-04-04T00:00:00+00:00</updated>
    <id>https://svnscha.de/tags/git/atom.xml</id>
    <entry xml:lang="en">
        <title>A Git feature I should've met sooner!</title>
        <published>2025-04-04T00:00:00+00:00</published>
        <updated>2025-04-04T00:00:00+00:00</updated>
        <author>
          <name>Sven Scharmentke</name>
        </author>
        <link rel="alternate" type="text/html" href="https://svnscha.de/posts/git-worktree/"/>
        <id>https://svnscha.de/posts/git-worktree/</id>
        <summary type="html">I was living in stash-hell and didn't even know it.</summary>
        <content type="html" xml:base="https://svnscha.de/posts/git-worktree/">&lt;h2 id=&quot;why-you-ask&quot;&gt;Why, You Ask?&lt;/h2&gt;
&lt;p&gt;Because I thought I knew Git.
Like really. Branches? Merged. Rebased. Cherry-picked. Heck, I've even force-pushed into production (don't judge me). I've danced the &lt;code&gt;git stash&lt;/code&gt; tango more times than I care to admit, and I've rage-quit after realizing I checked out the wrong branch with uncommitted changes.&lt;/p&gt;
&lt;p&gt;But then-then-I discovered &lt;code&gt;git worktree&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;And everything changed.&lt;/p&gt;
&lt;h3 id=&quot;the-problem-with-the-normal-way&quot;&gt;The Problem With the &quot;Normal&quot; Way™&lt;/h3&gt;
&lt;p&gt;Let's say you're working on a feature. You're knee-deep in uncommitted changes, and suddenly you need to switch to another branch to hotfix or review something. What do you do?&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;git stash&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;git checkout other-branch&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Try to remember what you were doing in your branch later.&lt;/li&gt;
&lt;li&gt;Forget what you stashed and why.&lt;/li&gt;
&lt;li&gt;Cry&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This is the standard Git workflow we all just kind of accept. It's chaotic good at best.&lt;/p&gt;
&lt;h3 id=&quot;enter-git-worktree&quot;&gt;Enter: &lt;code&gt;git worktree&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;What if I told you... you could just check out multiple branches at the same time? Each in its own separate folder. No stash. No backflips. No existential dread.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;git worktree&lt;/code&gt; is a built-in Git feature that lets you:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Check out multiple branches at once&lt;/li&gt;
&lt;li&gt;Work on each one in its own directory&lt;/li&gt;
&lt;li&gt;Keep your mental state and filesystem intact&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;examples&quot;&gt;Examples&lt;/h3&gt;
&lt;p&gt;Create a new worktree for a feature branch&lt;/p&gt;
&lt;pre class=&quot;astro-code dark-plus&quot; style=&quot;background-color:#1E1E1E;color:#D4D4D4; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#DCDCAA&quot;&gt;git&lt;/span&gt;&lt;span style=&quot;color:#CE9178&quot;&gt; worktree&lt;/span&gt;&lt;span style=&quot;color:#CE9178&quot;&gt; add&lt;/span&gt;&lt;span style=&quot;color:#CE9178&quot;&gt; ../feature-x&lt;/span&gt;&lt;span style=&quot;color:#CE9178&quot;&gt; origin/feature-x&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This checks out the &lt;code&gt;feature-x&lt;/code&gt; branch (fetched from &lt;code&gt;origin&lt;/code&gt;) into a new directory &lt;code&gt;../feature-x&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;If it's a new feature you start locally, do this:&lt;/p&gt;
&lt;pre class=&quot;astro-code dark-plus&quot; style=&quot;background-color:#1E1E1E;color:#D4D4D4; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#DCDCAA&quot;&gt;git&lt;/span&gt;&lt;span style=&quot;color:#CE9178&quot;&gt; worktree&lt;/span&gt;&lt;span style=&quot;color:#CE9178&quot;&gt; add&lt;/span&gt;&lt;span style=&quot;color:#CE9178&quot;&gt; ../feature-x&lt;/span&gt;&lt;span style=&quot;color:#569CD6&quot;&gt; -b&lt;/span&gt;&lt;span style=&quot;color:#CE9178&quot;&gt; feature-x&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This creates a new local branch &lt;code&gt;feature-x&lt;/code&gt; from your current &lt;code&gt;HEAD&lt;/code&gt; (usually &lt;code&gt;main&lt;/code&gt;) and checks it out into &lt;code&gt;../feature-x&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Wow. 🥳 No more &quot;wait lemme stash real quick.&quot;&lt;/p&gt;
&lt;p&gt;Now you can commit your fix, push it, and go back to your mess later. Like a civilized developer.&lt;/p&gt;
&lt;h4 id=&quot;list-all-worktrees&quot;&gt;List all worktrees&lt;/h4&gt;
&lt;pre class=&quot;astro-code dark-plus&quot; style=&quot;background-color:#1E1E1E;color:#D4D4D4; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#DCDCAA&quot;&gt;git&lt;/span&gt;&lt;span style=&quot;color:#CE9178&quot;&gt; worktree&lt;/span&gt;&lt;span style=&quot;color:#CE9178&quot;&gt; list&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Because you will forget what you spun up after the third coffee.&lt;/p&gt;
&lt;h3 id=&quot;whats-the-catch&quot;&gt;What's the Catch?&lt;/h3&gt;
&lt;p&gt;Honestly? Not much. A few things to keep in mind:&lt;/p&gt;
&lt;p&gt;You can't check out the same branch twice. Git will yell at you. That's fair.&lt;/p&gt;
&lt;p&gt;Deleting a worktree is easy: just &lt;code&gt;git worktree remove ../feature-x&lt;/code&gt; (the directory) and that's it.&lt;/p&gt;
&lt;h3 id=&quot;why-you-need-this-in-your-life&quot;&gt;Why You Need This in Your Life&lt;/h3&gt;
&lt;p&gt;If you're working on multiple features, doing code reviews locally, fixing stuff mid-feature, or just trying to keep your brain from melting-&lt;code&gt;git worktree&lt;/code&gt; is the gift you didn't know Git included.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;No more stash juggling.&lt;/li&gt;
&lt;li&gt;No more &quot;which terminal had which branch again?&quot;&lt;/li&gt;
&lt;li&gt;No more &quot;accidentally committed to main&quot; horror stories.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Just clean, parallel workspaces from a single clone.&lt;/p&gt;
&lt;p&gt;Use it. Love it. Regret not learning it sooner.
I know I did.&lt;/p&gt;
</content>
    </entry>
</feed>
