Menu
About me Kontakt

Git Worktrees - what they are and how to simplify your work with them (video, 8 minutes)

In his latest video, ThePrimeagen discusses the powerful capabilities that Git Worktrees offer. He highlights how often developers encounter the need to pivot in their work, for instance, when they need to quickly address an issue in production. In such scenarios, developers often have to temporarily stash their changes, which can lead to confusion and misunderstandings. Ironically, many people are unaware of how this process can be streamlined with Worktrees. ThePrimeagen explains that Worktrees allow multiple commits to be checked out simultaneously in different folders, significantly easing the transition between various tasks. This solution greatly reduces the need for manual change management and minimizes the time spent recalling what has been done and what needs to be done.

Throughout the video, ThePrimeagen also shows how to utilize Git commands to create Worktrees. He demonstrates adding different commits and managing them with simple commands. He notes that mastering the use of Worktrees is key to effective work, enabling developers to swiftly adapt to changing requirements. With Worktrees, for instance, one might have separate folders for the main branch of the project, new features, and fixes, thus avoiding confusion and chaotic commits.

ThePrimeagen also shares his experiences writing plugins to automate working with Worktrees. He has found a way to integrate them with the telescope tool, which further enhances the work comfort. He emphasizes how his plugin makes branch management and status updates easier, which undeniably accelerates the coding process. In this context, it's important to note that integrated tools can significantly boost the productivity of development teams.

Finally, it's worth noting the video statistics. At the time of writing this article, ThePrimeagen's material had 300095 views and 9611 likes. This indicates a growing interest in the topic of Worktrees in Git and the efficiency they can bring to a developer's daily tasks. The topic of working with Git continues to be popular, and ThePrimeagen's innovative approach may contribute to even better utilization of this tool in practice.

Toggle timeline summary

  • 00:00 Introduction to Git Worktrees and their advantages.
  • 00:11 Explanation of the need to pivot in projects and the inconvenience of traditional Git workflows.
  • 00:58 Description of how Worktrees allow multiple commits to be checked out simultaneously.
  • 01:24 Highlighting the long-ignored functionality of Git Worktrees.
  • 01:43 Demonstration of Git Worktrees with a clone command.
  • 02:07 Introduction of the 'bear' keyword in Git Worktree usage.
  • 02:40 Adding multiple worktrees for different commits.
  • 03:40 Removing a worktree and handling changes.
  • 04:11 Personal experience and shift toward using Git Worktrees.
  • 04:35 Creation of a plugin for easier Git Worktree operations.
  • 04:50 Using the created plugin to create branches seamlessly.
  • 06:02 Integration of plugins to manage worktree operations efficiently.
  • 06:36 Automating submodule updates with changes in worktrees.
  • 07:32 Concluding remarks encouraging the use of Git Worktrees for improved workflows.
  • 07:46 Final thoughts on the integration of plugins and user experiences.

Transcription

So Git Worktrees are incredible. If you haven't used them, oh my goodness, they fix so many things that are wrong with my current Git workflow. So before I tell you what Git Worktrees are, I thought it would be better if we talk about why you should use Git Worktrees first. Have you ever checked out a project, started working on, and then need to pivot, right? You either have a message from a coworker, go look at this bug, something's down in production. I'm my fault on production, by the way. Or you need to just work on something else. You've had to pivot, right? So what do you do? Well, you have to go and stash your current tree, or you commit right there with a little message, please squash me later. And then you forget about doing it, and your coworkers are like, WTF, what is this squash message you got here? This is not even that good. And you have to do this, and you have to check out something else, and you have to do some work, and then you have to uncheck out, and go back, and then unstash, or look at your commit, and try to remember what you're doing. It's just not that great, right? It's just not a fun experience. So Worktrees allow you to have multiple commits checked out at the exact same time. Each folder will represent a different commit, meaning that you can have master, you can have your feature one, you can have feature two, you can have your production issue that you accidentally did. Again, sorry about production. So if that does not sound incredible, you have menus to get that long, because this is a mind blowing. And the best part is, is that this feature has been around for a while. It's just nobody's ever really heard of it. Even Guido just discovered these. Yeah, they're incredible. So what are Git Worktrees? Well, they allow you to have multiple commits checked out at the exact same time, as opposed to a traditional cloning of a repo, you have one commit checked out plus any changes you've made. So let me just show you what that means. So let's go like this, git clone, this repo, you'll notice that all I'm doing is doing a simple clone. And when I go in there, you'll notice that I'm on the master branch, I can go get branch dash A, you can see all the branches. If I do any changes, I'll have to either stash them or commit them to be able to change branches. So let's RM RDF that git work tree right away. And let's do a git clone bear now bears the keyword you need to use here and then go get to work tree dot n vim. There we go. So now we're going to clone in a bear repository. Now if you remember last time, I had Lua, colon W, that's pretty much it inside of my repo. So now let's go back into get work tree. And what you'll see this time is a bunch of different stuff. Now this is obviously all the information required to be able to use a bear repo. So what you're going to do instead now is tell the bear repo what commit you would like to have checked out. So I'll go like this git work tree add master, this will add master at its latest known commit. I can also add other ones get work tree add readme. Right? Okay, I can get as you get work tree add foo bar baths. So now I have three separate work trees checked out. Let's re LS. Now notice the difference between this one. And this one, I have read me, I have master, and I have foo bar baths. If I were to go into foo bar baths, it would be foo bar baths at its commit and whatever changes I have. If I were to jump into Vim, you'll notice that I'll be able to see all the different stuff that is to do with this plugin. If I were to make some sort of change right here, I could then come back out and CD back into say the master folder. And that exact same change would not be there at all. So that means I could have separate checkouts in their own state. Hopefully at this point, you see the power of get work trees and you are excited. You can also remove them obviously get work tree remove and then you can name any of the ones that you want to remove. I don't know why just to auto completed like that. Let's just remove foo bar baths. There we go. We removed it. Oh, I already have some changes in it. You have to do it with four. So get work tree remove. Let's not have the huge gigantic path this time foo bar baths. And force there we go. If we relook at our list of items, you'll notice that foo bar baths is gone. So when I saw this, I immediately just I just fell in love. I fell in love. I knew for that moment that get work trees were going to be in my future thoroughly. I've been in the process of changing over all of my work projects into bear checkouts and actually doing work trees as opposed to whatever the nonsense I have been using. I can't even remember at this point, it seems so arcane and distant. So at this point, I didn't want to have to write all these commands CD around open and close them do all that. So I wrote a quick plugin called get work tree that wraps all those get operations and does some convenient updating for you. I got a status line to make it easier to tell what branch I'm in. So I don't accidentally edit the wrong code. And we created a telescope plugin to make work tree operations easier. Alright, so we're back inside of the get work tree. And so now I'm going to create a branch using the plugin. At this point, I don't have any key maps or anything. So I'm just raw typing it in and just simply creating a work branch on both sides, you'll notice that after I got done executing it, what happened right here. So you should notice right away that something changed, right? I'm no longer in that root directory, I'm actually moved into other branch, you'll see right away that my status line has now let me know that I'm doing that. And of course, telescope has updated to work with this new repo. So I just got done showing you how to create it using the raw command. But this time, I'm going to use telescope to be able to switch branches to make it even easier. So let's switch back to master, you'll notice that something did change. Notice that this branch says master as opposed to other branch to what just happened there. Well, let me make it a little bit easier, let me move my cursor down, and then re switch back. So what just happened? Well, we actually swapped out the file so that you're editing the same file, but in the new work tree. On top of that, if you happen to have a file that doesn't exist, doesn't exist, right? Lua, there we go, if we have something right here, and we decide to swap back to master. So if I jump back and go back to master, it'll actually just put me back in x. That way, I'm looking at it, I know something has changed, it put me back in x, because that file did not exist in master. Combining the three plugins I showed you allowed me to have a pretty fast workflow right there, I can easily do the work tree operations using telescope to help guide me on top of that I can tell where I'm at because of the status line. And lastly, those operations are provided by the get work tree plugin. So at work, I have a large c++ application that does have sub modules in it, if I forget to get some module update, when I create a branch, it will fail the configure stage. And often I don't recognize that it did. And then I end up spending 510 minutes trying to figure out what the heck is going on. I'm not the most observant, I get it. So part of the get work tree plugin allows you to hook into changes in your work tree. So here you go. Here's a simple example of me using plenary to create a job and work tree to tap into changes. Now notice that I can just simply do on tree change, the operation can either be create, switch or delete. And when I see that there is a creation, and it's the Netflix television device repository, I'm going to go and execute get sub module update in that repositories. That way, I just don't even ever have to think about it again. It just happens for me and I am none the wiser, right? This is something that just saves a lot of time and headaches. Because every time I create a new tree, I have to do it anyways. Why not just let it be done for me and I don't have to think about it. Karen, do you think that we could force everyone to use work trees? You can't? Okay, well, if I'm the boss here at the startup, why can't I? OSHA? OSHA doesn't? Oh, wow. I had no idea that OSHA had rules even for this. Hey, thank you for watching. Please check out work trees, they will make your life 1000 times better because you still get all the standard usages of get you can just commit, push, pull, whatever inside of each branch, but each branch is its own folder. And thus works very simple. Hopefully you like that little plugin that I showed you to integration with telescope is incredible. And combining it with a status line would really help you kind of know where you're at. Everything is linked down below. Please let me know if you've used get work trees, what you thought about it. If you have any tips for me, I will take them. Let me know down below. Everything's made live on Twitch. And of course, our discord is filled with 1000s of people at this point. My name is the primes and thank you for watching.