<?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: LokiDev</title>
    <description>The latest articles on DEV Community by LokiDev (@lokidev).</description>
    <link>https://dev.to/lokidev</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%2F17332%2Fcf556cbd-cb14-4fc0-a46d-a64150333d44.png</url>
      <title>DEV Community: LokiDev</title>
      <link>https://dev.to/lokidev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/lokidev"/>
    <language>en</language>
    <item>
      <title>Advanced Git: Worktree</title>
      <dc:creator>LokiDev</dc:creator>
      <pubDate>Wed, 24 Nov 2021 19:57:14 +0000</pubDate>
      <link>https://dev.to/lokidev/advanced-git-worktree-28ko</link>
      <guid>https://dev.to/lokidev/advanced-git-worktree-28ko</guid>
      <description>&lt;p&gt;&lt;a href="https://media.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%2Fg4g6zfyqhi7s9j8we8z9.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fg4g6zfyqhi7s9j8we8z9.jpg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;One of my favorite unknown features of Git is the &lt;strong&gt;worktree&lt;/strong&gt;. A part from the Git toolbelt which allows us to have multiple checked out branches with just one repository.&lt;/p&gt;

&lt;p&gt;This is perfect for code reviews where you actually want to dig into the code and don't want to use the webfeatures of Git(lab|hub) or other alternatives.&lt;/p&gt;

&lt;p&gt;Enough the words, lets see how this works...&lt;/p&gt;

&lt;h2&gt;
  
  
  Git Worktree
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What we could get
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ tree
project_name/
 |- master/
 |- develop/
 |- feature/
 |   |- ABC-123_implement_death_laser/
 |- hotfix/
     |- ABC-42_solve_climate_crisis/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each branch has it's own Git repository, but all share the repository metadata.&lt;/p&gt;

&lt;h3&gt;
  
  
  How do we get there?
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;create project folder: &lt;code&gt;mkdir project_name&lt;/code&gt; and &lt;code&gt;cd&lt;/code&gt; into the folder &lt;code&gt;cd project_name&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;clone your repository, but with some feature flags: &lt;code&gt;git clone --bare GIT_URL .bare&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;this clones from &lt;code&gt;GIT_URL&lt;/code&gt; to the local hidden folder &lt;code&gt;.bare&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--bare&lt;/code&gt; makes it so, that we don't get any specific branch&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;create the &lt;code&gt;.git&lt;/code&gt; file and tell it (by writing in it ;) where our bare repository is:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   # project_name/.git
   gitdir: ./.bare
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now our repository knows where all the desired metadata can be found&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Lets add our desired branches: &lt;code&gt;git worktree add develop&lt;/code&gt; or &lt;code&gt;git worktree add featre/some_feature&lt;/code&gt;. The &lt;code&gt;/&lt;/code&gt; creates automagically new folders for us.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Additional helpful stuff
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Remove a single branch/folder
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Two possibilities:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;remove the folder just like every other folder and run &lt;code&gt;git worktree prune&lt;/code&gt; after that&lt;/li&gt;
&lt;li&gt;&lt;code&gt;git worktree remove BRANCH_NAME&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Show all worktree folders
&lt;/h3&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;
  
  
  Create a branch folder outside of the repository folder
&lt;/h3&gt;

&lt;p&gt;Yes. this is indeed possible. Say, you have your project &lt;strong&gt;cookie_bakery&lt;/strong&gt; in your project folder: &lt;code&gt;/home/lokidev/projects/cookie_bakery&lt;/code&gt;.&lt;br&gt;
But now you want to have new branch in which you  have a huge project which shouldn't be INSIDE this cookie_bakery folders, but it's really important and needs to be on some external harddrive. Here you go:&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;-b&lt;/span&gt; BRANCH_NAME FOLDER
&lt;span class="c"&gt;# e.g.&lt;/span&gt;
git woktreee add &lt;span class="nt"&gt;-b&lt;/span&gt; feature/super_important /media/usb-drive/important
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Move/Rename branch-folder
&lt;/h3&gt;



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

&lt;/div&gt;



&lt;h2&gt;
  
  
  More awesome Git stuff?
&lt;/h2&gt;

&lt;p&gt;There is more awesome stuff in the Git toolbelt like &lt;code&gt;bisect&lt;/code&gt; which helps you find the exact moment in the git history, where an error was introduced.&lt;/p&gt;

&lt;p&gt;Another nice feature is hunk based adding/committing of files and changes.&lt;/p&gt;

&lt;p&gt;If you want more of this, just tell me. If you have things I can make better: even better!&lt;/p&gt;

&lt;h2&gt;
  
  
  Additional Links
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://git-scm.com/docs/git-worktree" rel="noopener noreferrer"&gt;Official Documentation&lt;/a&gt; &lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>git</category>
      <category>productive</category>
      <category>tools</category>
      <category>shell</category>
    </item>
  </channel>
</rss>
