<?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>The Ops Community ⚙️: James D.</title>
    <description>The latest articles on The Ops Community ⚙️ by James D. (@jamesatintegratnio).</description>
    <link>https://community.ops.io/jamesatintegratnio</link>
    <image>
      <url>https://community.ops.io/images/DCmUFybJms9414znXUNkBuAwFBXYpG8QOUf2nkfiWys/rs:fill:90:90/g:sm/mb:500000/ar:1/aHR0cHM6Ly9jb21t/dW5pdHkub3BzLmlv/L3JlbW90ZWltYWdl/cy91cGxvYWRzL3Vz/ZXIvcHJvZmlsZV9p/bWFnZS8xNTAvM2U0/ZGQzNjctMmNjOS00/NjMxLTk1Y2ItYzMx/NGQ5MjUwMmZiLmpw/ZWc</url>
      <title>The Ops Community ⚙️: James D.</title>
      <link>https://community.ops.io/jamesatintegratnio</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://community.ops.io/feed/jamesatintegratnio"/>
    <language>en</language>
    <item>
      <title>My Git Worktree Workflow</title>
      <dc:creator>James D.</dc:creator>
      <pubDate>Wed, 25 May 2022 23:21:12 +0000</pubDate>
      <link>https://community.ops.io/jamesatintegratnio/my-git-worktree-workflow-38np</link>
      <guid>https://community.ops.io/jamesatintegratnio/my-git-worktree-workflow-38np</guid>
      <description>&lt;p&gt;I recently made a change to how I work on repositories when I have to work with others. It has been a life saver. &lt;/p&gt;

&lt;p&gt;There is a TLDR example at the bottom to get you interested in the rest of the article.&lt;/p&gt;

&lt;h2&gt;
  
  
  Pain Points
&lt;/h2&gt;

&lt;p&gt;Lets start with the normal git workflow and the pain points it causes. First you clone the repo. You create your branch and you start coding away. Then your buddy hits you up on Slack with a huge PR and scrolling the diff in Github isn't enough for you to approve it. Great. Now you have to stash the code you are working on, update your base so that you have their branch, and pull it all down. Now you can finally review the PR. This is nuts. Its annoying and you completely lose track of whatever you were just working on. I didn't even cover getting back to your branch and getting going again.&lt;/p&gt;

&lt;h2&gt;
  
  
  Painting a Picture With a Dev Story
&lt;/h2&gt;

&lt;p&gt;As a developer I want to be able to review my teammates code without having to stash my work or maintain a second copy of the entire repository. I also want to be able to pause my work and work on a different branch again without having to stash my work or maintain a second copy.&lt;/p&gt;

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

&lt;p&gt;First off, here is a link to the git worktree &lt;a href="https://git-scm.com/docs/git-worktree"&gt;documentation&lt;/a&gt;. I won't cover every caveat. So check it out for a lot more info.&lt;/p&gt;

&lt;p&gt;Git is capable of maintaining multiple working trees of a single repo. You heard that right. With git worktree you can check out more than one branch at a time and sensibly maintain each of those branches.&lt;/p&gt;

&lt;p&gt;I had to rethink my folder folder structure when I started using worktree. It was weird at first. But its been amazing after I settled in.&lt;/p&gt;

&lt;p&gt;When I clone a repository with worktree in mind I will do it a little differently.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;git clone git@github.com:googleapis/python-tasks.git python-tasks/main
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is where the main branch lives. I really don't touch it except to fetch/pull and create new branches with worktree. Its my clean source of truth and I like to keep it that way.&lt;/p&gt;

&lt;p&gt;From here I will create a new branch to for whatever I'm working on But I won't use &lt;code&gt;git branch ${branchName}&lt;/code&gt; or &lt;code&gt;git checkout -b ${branchName}&lt;/code&gt;. I'll do it with worktree while i'm in the main branch folder.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;git worktree add &lt;span class="nt"&gt;-b&lt;/span&gt; my-awesome-branch ../my-awesome-branch main
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Pay attention to that &lt;code&gt;../&lt;/code&gt; If you forget it you will end up putting the branch in the folder where main lives and that can get real ugly, real fast. &lt;/p&gt;

&lt;p&gt;So now we have:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;~/Projects
  /python-tasks
    /main
    /my-awesome-branch
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can now list all of your working trees with the list argument.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;git worktree list
~/Projects/python-tasks/main                63df2ef &lt;span class="o"&gt;[&lt;/span&gt;master]
~/Projects/python-tasks/my-awesome-branch   63df2ef &lt;span class="o"&gt;[&lt;/span&gt;my-awesome-branch]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I change to that directory. I do my work. Next thing I know I get a message from a teammate and he needs me to do that PR review we've been talking about. With git worktree its too easy. I just go back to main and add his branch to my worktree.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;git worktree add &lt;span class="nt"&gt;--track&lt;/span&gt; &lt;span class="nt"&gt;-b&lt;/span&gt; add-appengine-flexible-tasks-samples ../ppr-review origin/add-appengine-flexible-tasks-samples

Preparing worktree &lt;span class="o"&gt;(&lt;/span&gt;new branch &lt;span class="s1"&gt;'add-appengine-flexible-tasks-samples'&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
Branch &lt;span class="s1"&gt;'add-appengine-flexible-tasks-samples'&lt;/span&gt; &lt;span class="nb"&gt;set &lt;/span&gt;up to track remote branch &lt;span class="s1"&gt;'add-appengine-flexible-tasks-samples'&lt;/span&gt;  from &lt;span class="s1"&gt;'origin'&lt;/span&gt;&lt;span class="nb"&gt;.&lt;/span&gt;
HEAD is now at e2c8eee chore: generate noxfile.py &lt;span class="k"&gt;for &lt;/span&gt;samples
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There is a lot going on in that command. So check out the &lt;a href="https://git-scm.com/docs/git-worktree#Documentation/git-worktree.txt-addltpathgtltcommit-ishgt"&gt;doc&lt;/a&gt; for the full explanation, but I'll try to break it down.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git worktree add&lt;/code&gt;: We've seen this. Its the base command to create a branch and a working tree&lt;/p&gt;

&lt;p&gt;&lt;code&gt;--track&lt;/code&gt;: This sets up tracking mode. We need it to track a remote branch&lt;/p&gt;

&lt;p&gt;&lt;code&gt;-b&lt;/code&gt;: This will create a new branch and we give it the same name as the remote branch because its the right thing to do. But really we could call it anything.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;../pr-review&lt;/code&gt; The path we want it checked out to. Don't forget that &lt;code&gt;../&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;&lt;code&gt;origin/add-appengine-flexible-tasks-samples&lt;/code&gt; Finally the &lt;code&gt;&amp;lt;remote&amp;gt;/&amp;lt;branch&amp;gt;&lt;/code&gt; that we want check out.&lt;/p&gt;

&lt;p&gt;Now if we do &lt;code&gt;git worktree list&lt;/code&gt; We'll see our friends branch in our working tree ready for review.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;git worktree list
~/Projects/python-tasks/main                63df2ef &lt;span class="o"&gt;[&lt;/span&gt;master]
~/Projects/python-tasks/my-awesome-branch   63df2ef &lt;span class="o"&gt;[&lt;/span&gt;my-awesome-branch]
~/Projects/python-tasks/pr-review           e2c8eee &lt;span class="o"&gt;[&lt;/span&gt;add-appengine-flexible-tasks-samples]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Pretty sweet.&lt;/p&gt;

&lt;p&gt;Now I've finished my PR review and I don't need the code anymore. so all we have to do from &lt;code&gt;main&lt;/code&gt; is&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;git worktree remove pr-review
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Poof, The folder is gone and we don't have to clog our working tree up with it anymore. Sometimes we get lazy right? maybe I did &lt;code&gt;rm -rf ./pr-review&lt;/code&gt;. This is bad. I just broke my working tree. The folders gone but git still knows about it when I do &lt;code&gt;git worktree list&lt;/code&gt; How can we fix this? Is it time to just throw the whole repo away and clone again? What do we do?&lt;/p&gt;

&lt;p&gt;From main as usual:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;And now our mistake has been removed from the git and we can continue on about our day.&lt;/p&gt;

&lt;p&gt;There are a few other worktree commands out there. Checkout &lt;a href="https://git-scm.com/docs/git-worktree#Documentation/git-worktree.txt-lock"&gt;lock&lt;/a&gt; and &lt;a href="https://git-scm.com/docs/git-worktree#Documentation/git-worktree.txt-unlock"&gt;unlock&lt;/a&gt; if you need to put a working tree on a thumb drive or a network share thats not always mounted. That will protect it from &lt;a href="https://git-scm.com/docs/git-worktree#Documentation/git-worktree.txt-prune"&gt;prune&lt;/a&gt;. This was a long one. If you stuck around and found it useful, feel free to hit me up on my socials to tell me about it or teach me a new trick you found thats even better.&lt;/p&gt;

&lt;h2&gt;
  
  
  TLDR Example
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;git worktree add &lt;span class="nt"&gt;-b&lt;/span&gt; emergency-fix ../temp main
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;pushd&lt;/span&gt; ../temp
&lt;span class="c"&gt;# ... hack hack hack ...&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;git commit &lt;span class="nt"&gt;-a&lt;/span&gt; &lt;span class="nt"&gt;-m&lt;/span&gt; &lt;span class="s1"&gt;'emergency fix for boss'&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;popd&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;git worktree remove ../temp
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&amp;lt;a href="&lt;a href="https://www.buymeacoffee.com/integratnio"&gt;https://www.buymeacoffee.com/integratnio&lt;/a&gt;" &lt;/p&gt;

</description>
      <category>git</category>
      <category>devops</category>
      <category>tutorials</category>
      <category>career</category>
    </item>
  </channel>
</rss>
