Check out a free preview of the full Everything You'll Need to Know About Git course

The "Worktrees" Lesson is part of the full, Everything You'll Need to Know About Git course featured in this preview video. Here's what you'd learn in this lesson:

ThePrimeagen introduces the concept of worktrees as a solution to the limitations and inconveniences of using stash in Git. He explains that a worktree is a linked working tree, separate from the main tree, that allows for easy switching between branches without the need for stashing or committing changes. A demonstration of how to create and manage WorkTrees, highlighting their benefits and potential drawbacks is also provided in this lesson.


Transcript from the "Worktrees" Lesson

>> All right, Stash is not that great, right? We've all used Stash, were we all happy with it? No, you weren't happy with it, okay? You weren't happy at all, you now want to be happy though. So we're gonna be happy by using worktrees, so worktrees, you're, so the best way to describe them is like a little story, right?

You're on a worktree food bar, you're making the greatest progress ever. You're in flow state, everything's going great, but then emergency situation is needed on main, some sort of bug there, so what do you do? Well, typically we've been doing the whole git add, throw everything into the stash, switch the branches, make the changes and then come back.

Or we could commit it and pull it and try to figure out what's wrong with it, we need to do something, right? Well, what's a little bit nicer is worktrees, let me show you how they work. First off, a worktree often when we say this phrase, what we mean is a linked working tree.

It's not your main tree, the main tree is the one you create when you do a get a net or a get clone, those are the main working tree. A linked working tree is just simply a branch from your main tree in its own state. Remember, every commit can rebuild the entirety of a repo, very, very important you understand that one point because this is where it's really used right here.

And it's a little bit different cuz the git folder is not a folder, it's just a file. So let's do a little bit of this, okay? First off you can do a git worktrree add to a path, the base name of the path will be used as the branch name.

If you don't know a base name is, basename, foo bar baz. So this will automatically use your path as a branch name. You can also add in the branch name if you want it, or you can just Let it auto do it. Git worktree list will list out your available worktrees.

If you wanna delete it, you can just delete the worktree folder and then call worktree prune. Or you can actually do a worktree remove on the folder and it will do that. So depending on how you want to go about it, it can do either way. The benefits is that, it's super cheap to make, pretty much, like I said, pretty much as cheap as a branch.

The real big con with the worktree, is if there's high branch setup cost. So like if you npm install everything JS, and it takes way too long to do anything, well, that's a problem, right? And so if you have to build like a rust project and it takes minutes upon minutes, you'll find yourself a little bit more hesitant to use them depending on how long it takes.

So we're gonna create a worktree in hello-git and I want to use foo-bar as the last item in the path, so it creates the branch, food dash bar. My suggestion is just go back one folder from your root of hello-git and just use the dash, dash or dot, dot, slash, foo, dash, bar.

And when we're done, check out the git folder. So I'm gonna go like this, I'm gonna go git worktree add foo-bar, I think it probably already exists. Nice, it doesn't, let's go, there we go. We prepared a new worktree and it's been checked out at this point right here.

So it created a new branch at this current point, which means if I open up foo-bar, you'll notice I'm in a repo. I can tell that I'm in a git repo because I have this nice little thing right here. If I go git log, you can see revert e, a plus nine merge, you can see all of our commits in here, we're in the same repo, just on a different branch.

Now, here's the best part about that, let's cut out the git log or the git folder, remember, it's not a folder anymore. It actually just tells you the file just simply says, hey, I belong here. And so tells you where the main working tree is, because this is a linked working tree.

Yeah, I'm gonna do one little quick thing, it's not in the notes, but we'll just do it right her. If I go like this, if I go foo, put in some changes right here and go git status and go git add, git. Put in a little change really quickly then I'm gonna go back to hello git.

If I go, git log foo-bar, my change is right there, right? Because it's a linked working tree. It's just another, you just have another entrance into your folder. So that way, you can actually have two running copies, right? So you could actually have your feature development and then just like mainline over here.

And so that way you can always, you only have like two folders you're really working on. So anytime you need to like update code or do some sort of like emergency switch, you can do that. And just leave your working tree in whatever state it's in and then you never really switch branches, so the idea is that branch switching is almost an anti-pattern.

And instead, you just use work trees constantly to build everything. And that way you never have to undo stashing just does not exist, you just simply have the file or the branch out as its own folder with its own state. And then when you're done, you just prune it, you got, you just get rid of it.

And so that's kind of the idea, like the ideal idea, you can also just have like two versions of itself where you switch branches all you want, there's some moves. And so if I go like this, git worktree list, it'll tell me my two worktrees, I have my main working tree right here.

And then I have this one, which is my linked working tree, foo-bar. Pretty cool, I like this one, I really do like them. Now remember, there's two ways you can delete them, of course. So that means I can go rm-rf foo-bar and once I do that, if I go worktree list, you'll see right here, it knows, That this thing no longer exists.

So now you're probably thinking, how does it know that it no longer exists? It means not only is there a link from your linked working tree to your main line, your main line must also have that information out to the linked work tree. I'm not sure if I know where it is, honestly just off the top of my head.

I think I do, I bet. Totally preplanned, then you can see worktrees, right, so you can actually see it right here. So you can see here it is bad, you can see what head it's supposed to be at, you can see the git dr. So you'll notice that this is no longer a thing, it probably just stats out the file and notices if the file is missing, it just says, hey, this is a provable worktree, you've already deleted it.

And so, that's how it effectively is able to understand that I've deleted it outside of git. So now, I can just go get worktree prune and if I go get worktree list, it's gone, right? Awesome, I think it's great, I really liked, I love worktrees, they're absolutely fantastic.

They just solved that one really annoying problem of having to switch things, stash things, commit things early, any of that. All right, yeah, we can [INAUDIBLE] git worktree, remove, we don't need to do that one too. I do love worktrees though, they're just my favorite. There is also env files, are really painful, so if you have env file, if it's checked into the repo, which would be a little unusual, if that's the case, then you're fine.

But if If it's not checked into the repo, then you have like this whole like copying of your env file around node modules, goes great, it just builds really fast. So that just works out, but everything else kinda tends to be a little bit more of a pain that way, so just something to think about.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now