<?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>Git on Axqora 博客</title>
    <link>https://blog.axqora.com/tags/git/</link>
    <description>Recent content in Git on Axqora 博客</description>
    <image>
      <title>Axqora 博客</title>
      <url>https://blog.axqora.com/icon.png</url>
      <link>https://blog.axqora.com/icon.png</link>
    </image>
    <generator>Hugo</generator>
    <language>zh</language>
    <lastBuildDate>Sun, 10 May 2020 00:00:00 +0000</lastBuildDate>
    <atom:link href="https://blog.axqora.com/tags/git/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>利用 git worktree 并行开发</title>
      <link>https://blog.axqora.com/posts/git-worktree/</link>
      <pubDate>Sun, 10 May 2020 00:00:00 +0000</pubDate>
      <guid>https://blog.axqora.com/posts/git-worktree/</guid>
      <description>&lt;h2 id=&#34;一什么是worktree&#34;&gt;一、什么是worktree？&lt;/h2&gt;
&lt;blockquote&gt;
&lt;p&gt;Manage multiple working trees attached to the same repository.
A git repository can support multiple working trees, allowing you to check out more than one branch at a time.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;管理连接到同一存储库的多个工作树。一个git仓库可以支持多个工作树，允许一次迁出多个分支。&lt;/p&gt;
&lt;h2 id=&#34;二为什么要用worktree&#34;&gt;二、为什么要用worktree？&lt;/h2&gt;
&lt;p&gt;场景：正在开发新的需求，这时候还需要对已提测功能的bug fixes&lt;/p&gt;
&lt;p&gt;方法1：先stash正在开发的代码，再check out到原分支进行bug fixes
缺点：分支切换后需要重新pod install，然后xcode重建索引并重新编译，耗时耗力&lt;/p&gt;
&lt;p&gt;方法2：clone两个仓库
缺点：.git目录会占空间，且两个仓库之间毫无关联，无法相互查看本地提交&lt;/p&gt;
&lt;p&gt;方法3：git worktree&lt;/p&gt;
&lt;h2 id=&#34;三worktree的相关命令&#34;&gt;三、worktree的相关命令&lt;/h2&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;➜  worktree_test git:(develop) git worktree -h
usage: git worktree add [&amp;lt;options&amp;gt;] &amp;lt;path&amp;gt; [&amp;lt;commit-ish&amp;gt;]
   or: git worktree list [&amp;lt;options&amp;gt;]
   or: git worktree lock [&amp;lt;options&amp;gt;] &amp;lt;path&amp;gt;
   or: git worktree move &amp;lt;worktree&amp;gt; &amp;lt;new-path&amp;gt;
   or: git worktree prune [&amp;lt;options&amp;gt;]
   or: git worktree remove [&amp;lt;options&amp;gt;] &amp;lt;worktree&amp;gt;
   or: git worktree unlock &amp;lt;path&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&#34;四常用操作&#34;&gt;四、常用操作&lt;/h2&gt;
&lt;h5 id=&#34;1添加一个worktree&#34;&gt;1、添加一个worktree&lt;/h5&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;git worktree add ../live_protocol_isolation team_live/live_protocol_isolation
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;img alt=&#34;20298fd5dd1613e9ecfa2a8f2c5a8e2f&#34; loading=&#34;lazy&#34; src=&#34;https://blog.axqora.com/image/git-worktree/iShot2020-08-15AM10.47.39.png&#34;&gt;
&lt;img alt=&#34;289af9dbfe6df4e4e62253452a7d3408&#34; loading=&#34;lazy&#34; src=&#34;https://blog.axqora.com/image/git-worktree/iShot2020-08-15AM10.57.52.png&#34;&gt;&lt;/p&gt;</description>
      <content:encoded><![CDATA[<h2 id="一什么是worktree">一、什么是worktree？</h2>
<blockquote>
<p>Manage multiple working trees attached to the same repository.
A git repository can support multiple working trees, allowing you to check out more than one branch at a time.</p>
</blockquote>
<p>管理连接到同一存储库的多个工作树。一个git仓库可以支持多个工作树，允许一次迁出多个分支。</p>
<h2 id="二为什么要用worktree">二、为什么要用worktree？</h2>
<p>场景：正在开发新的需求，这时候还需要对已提测功能的bug fixes</p>
<p>方法1：先stash正在开发的代码，再check out到原分支进行bug fixes
缺点：分支切换后需要重新pod install，然后xcode重建索引并重新编译，耗时耗力</p>
<p>方法2：clone两个仓库
缺点：.git目录会占空间，且两个仓库之间毫无关联，无法相互查看本地提交</p>
<p>方法3：git worktree</p>
<h2 id="三worktree的相关命令">三、worktree的相关命令</h2>
<pre tabindex="0"><code>➜  worktree_test git:(develop) git worktree -h
usage: git worktree add [&lt;options&gt;] &lt;path&gt; [&lt;commit-ish&gt;]
   or: git worktree list [&lt;options&gt;]
   or: git worktree lock [&lt;options&gt;] &lt;path&gt;
   or: git worktree move &lt;worktree&gt; &lt;new-path&gt;
   or: git worktree prune [&lt;options&gt;]
   or: git worktree remove [&lt;options&gt;] &lt;worktree&gt;
   or: git worktree unlock &lt;path&gt;
</code></pre><h2 id="四常用操作">四、常用操作</h2>
<h5 id="1添加一个worktree">1、添加一个worktree</h5>
<pre tabindex="0"><code>git worktree add ../live_protocol_isolation team_live/live_protocol_isolation
</code></pre><p><img alt="20298fd5dd1613e9ecfa2a8f2c5a8e2f" loading="lazy" src="/image/git-worktree/iShot2020-08-15AM10.47.39.png">
<img alt="289af9dbfe6df4e4e62253452a7d3408" loading="lazy" src="/image/git-worktree/iShot2020-08-15AM10.57.52.png"></p>
<h5 id="2移除一个worktree">2、移除一个worktree</h5>
<pre tabindex="0"><code>git worktree remove /Users/oudushu/WorkSpace/live_protocol_isolation
</code></pre><h5 id="3查看工作区">3、查看工作区</h5>
<pre tabindex="0"><code>git worktree list
</code></pre><p><img alt="65d5798b1495fff9e6f2db2a7d7eb767" loading="lazy" src="/image/git-worktree/iShot2020-08-15AM11.25.39.png"></p>
<h5 id="4查看本地提交">4、查看本地提交</h5>
<pre tabindex="0"><code>git show team_live/live_protocol_isolation
</code></pre><p>因为是同一个repo，所以切到dev分支后，可以马上查看到worktree分支的提交。</p>
<h2 id="五注意点">五、注意点</h2>
<h5 id="1无法checkout到同一个分支">1、无法checkout到同一个分支</h5>
<p><img alt="a85f01f21b2d5e4a2b349a1da5077eab" loading="lazy" src="/image/git-worktree/iShot2020-08-15AM10.48.11.png"></p>
<h5 id="2通过git-worktree-add出来的分支没有git文件夹">2、通过git worktree add出来的分支，没有.git文件夹</h5>
<p><img alt="0d81eac276ce0016fcd09bdbe236adaf" loading="lazy" src="/image/git-worktree/iShot2020-08-15AM10.52.42.png"></p>
<p>原Podfile文件要处理：</p>
<pre tabindex="0"><code>#避免在使用git worktree的时候编译报错
#git worktree中.git是一个文件而不是目录
if File.directory?(&#34;.git&#34;) == true then
    FileUtils.cp_r(&#34;scripts/git-hooks/commit-msg&#34;, &#34;.git/hooks/commit-msg&#34;)
end
</code></pre><p><a href="https://git-scm.com/docs/git-worktree">https://git-scm.com/docs/git-worktree</a></p>
]]></content:encoded>
    </item>
  </channel>
</rss>
