<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Posts on ShawnDeng's Tech Blog</title><link>https://shawndeng.tech/posts/</link><description>Recent content in Posts on ShawnDeng's Tech Blog</description><generator>Hugo -- 0.146.0</generator><language>zh-cn</language><lastBuildDate>Fri, 06 Mar 2026 04:20:00 +0800</lastBuildDate><atom:link href="https://shawndeng.tech/posts/index.xml" rel="self" type="application/rss+xml"/><item><title>Git Worktree 实战指南：平行开发的秘密武器</title><link>https://shawndeng.tech/posts/git-worktree-practical-guide/</link><pubDate>Fri, 06 Mar 2026 04:20:00 +0800</pubDate><guid>https://shawndeng.tech/posts/git-worktree-practical-guide/</guid><description>&lt;h2 id="前言">前言&lt;/h2>
&lt;p>你是否经历过这样的场景？&lt;/p>
&lt;p>当你正在为某个功能跑漫长的测试时，突然来了一个紧急的 bug 要修复，但你又不能随便动工作区的代码，因为测试还在进行。此时只能无奈地切换分支，导致 &lt;code>node_modules&lt;/code> 需要重新安装，编译需要重新进行&amp;hellip;&lt;/p>
&lt;p>&lt;strong>Git Worktree&lt;/strong> 就是来解决这个问题的。它允许你在同一个 Git 仓库中同时维护多个工作目录，每个目录可以检出不同的分支，彼此独立。&lt;/p>
&lt;p>这篇文章将从理论到实战，深入讲解如何使用 Git Worktree 来优化你的开发流程。&lt;/p>
&lt;hr>
&lt;h2 id="什么是-git-worktree">什么是 Git Worktree？&lt;/h2>
&lt;h3 id="基本概念">基本概念&lt;/h3>
&lt;p>Git Worktree（工作树）是一个 Git 特性，允许你在&lt;strong>同一个 Git 仓库中创建多个工作目录&lt;/strong>。&lt;/p>
&lt;p>在没有 Worktree 之前：&lt;/p>
&lt;ul>
&lt;li>一个仓库 = 一个工作目录&lt;/li>
&lt;li>切换分支会改变当前工作目录的文件状态&lt;/li>
&lt;li>如果当前工作目录有未提交的改动，切换分支可能很困难&lt;/li>
&lt;/ul>
&lt;p>有了 Worktree 之后：&lt;/p>
&lt;ul>
&lt;li>一个仓库 = 多个独立的工作目录&lt;/li>
&lt;li>每个工作目录可以检出不同的分支&lt;/li>
&lt;li>多个目录可以同时进行开发，互不影响&lt;/li>
&lt;/ul>
&lt;h3 id="工作树-vs-传统分支">工作树 vs 传统分支&lt;/h3>
&lt;p>&lt;strong>常见误解：&lt;/strong> 工作树是对分支的替代&lt;/p>
&lt;p>&lt;strong>正确理解：&lt;/strong> 工作树是对分支的一种**&amp;ldquo;文件系统视图&amp;rdquo;**&lt;/p>
&lt;ul>
&lt;li>&lt;strong>分支&lt;/strong>：Git 中用来隔离开发路线的概念，存储在 &lt;code>.git/refs/heads/&lt;/code> 中&lt;/li>
&lt;li>&lt;strong>工作树&lt;/strong>：只是物理上创建了一个新的目录，让你能同时看到/编辑多个分支&lt;/li>
&lt;/ul>
&lt;p>分支的数量和数目都不会因为创建工作树而改变，工作树只是给了你一个新的&amp;quot;窗口&amp;quot;去访问这些分支。&lt;/p>
&lt;hr>
&lt;h2 id="为什么需要-git-worktree">为什么需要 Git Worktree？&lt;/h2>
&lt;h3 id="常见应用场景">常见应用场景&lt;/h3>
&lt;h4 id="1-场景修复紧急-bug不中断当前工作">1️⃣ &lt;strong>场景：修复紧急 bug，不中断当前工作&lt;/strong>&lt;/h4>
&lt;p>你正在 &lt;code>feature/new-api&lt;/code> 分支上开发新功能，代码还未提交。突然生产环境爆出一个 bug，需要立即修复。&lt;/p>
&lt;p>&lt;strong>传统做法：&lt;/strong>&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># 代码还没 commit，必须先 stash&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git stash
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># 切回 main 分支&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git checkout main
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># 修复 bug，提交&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git commit -m &lt;span style="color:#e6db74">&amp;#34;fix: xxx&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># 切回开发分支&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git checkout feature/new-api
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># 恢复代码&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>git stash pop
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>问题：如果项目很大（如前端项目有巨大的 node_modules），即使只是切换分支也要重新安装依赖！&lt;/p></description></item><item><title>Hello World - 我的博客上线了！</title><link>https://shawndeng.tech/posts/hello-world/</link><pubDate>Tue, 24 Feb 2026 10:00:00 +0800</pubDate><guid>https://shawndeng.tech/posts/hello-world/</guid><description>&lt;h2 id="欢迎">欢迎！👋&lt;/h2>
&lt;p>这是我的第一篇博客文章。在这里，我会分享：&lt;/p>
&lt;ul>
&lt;li>💻 &lt;strong>技术文章&lt;/strong> - 编程经验和心得&lt;/li>
&lt;li>🛠️ &lt;strong>项目记录&lt;/strong> - 有趣的项目开发过程&lt;/li>
&lt;li>📚 &lt;strong>学习笔记&lt;/strong> - 新技术的探索和总结&lt;/li>
&lt;li>💡 &lt;strong>思考感悟&lt;/strong> - 对技术和生活的思考&lt;/li>
&lt;/ul>
&lt;h2 id="博客技术栈">博客技术栈&lt;/h2>
&lt;p>这个博客使用的技术：&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Hugo&lt;/strong> - 超快的静态网站生成器&lt;/li>
&lt;li>&lt;strong>PaperMod&lt;/strong> - 简洁优雅的主题&lt;/li>
&lt;li>&lt;strong>Nginx&lt;/strong> - Web 服务器&lt;/li>
&lt;li>&lt;strong>Let&amp;rsquo;s Encrypt&lt;/strong> - 免费的 SSL 证书&lt;/li>
&lt;/ul>
&lt;h2 id="代码示例">代码示例&lt;/h2>
&lt;p>来个经典的 Hello World：&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-python" data-lang="python">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">def&lt;/span> &lt;span style="color:#a6e22e">hello_world&lt;/span>():
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> print(&lt;span style="color:#e6db74">&amp;#34;Hello, World!&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> print(&lt;span style="color:#e6db74">&amp;#34;欢迎来到我的博客！&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">if&lt;/span> __name__ &lt;span style="color:#f92672">==&lt;/span> &lt;span style="color:#e6db74">&amp;#34;__main__&amp;#34;&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> hello_world()
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-javascript" data-lang="javascript">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">function&lt;/span> &lt;span style="color:#a6e22e">helloWorld&lt;/span>() {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">console&lt;/span>.&lt;span style="color:#a6e22e">log&lt;/span>(&lt;span style="color:#e6db74">&amp;#34;Hello, World!&amp;#34;&lt;/span>);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">console&lt;/span>.&lt;span style="color:#a6e22e">log&lt;/span>(&lt;span style="color:#e6db74">&amp;#34;欢迎来到我的博客！&amp;#34;&lt;/span>);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#a6e22e">helloWorld&lt;/span>();
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h2 id="结语">结语&lt;/h2>
&lt;p>希望这个博客能成为我记录技术成长的地方，也希望能帮到访问的你。&lt;/p>
&lt;p>Stay tuned! 🚀&lt;/p></description></item></channel></rss>