در حال بارگذاری ...

Why Worktrees Matter0:08

All right, tell me if I'm close at all. My guess is you read and learned about Git work trees many years ago, and then you proceeded to completely forget about them. Am I right? I bet I am. Well now, I think you'll find with multiple AI agents, actually GI work trees are wildly useful. Let me show you. So here we are once again within the Laravel workshop repository, and if I scroll down, you'll see in the Read Me,

Laravel workshop repository, and if I scroll down, you'll see in the Read Me, we have a list of just potential additions and features that you might add on your own. One of them is related to bookmarking. Okay? So let's imagine we wanted to work on this, but also there's, there's other stuff we wanna work on simultaneously that is not related to bookmarking. How do we deal with this?

Branching vs Agents0:51

that is not related to bookmarking. How do we deal with this? Well, traditionally of course we would check out a branch, right? So you might do something like this. You might say get check out a branch called bookmarking Here you would work on your various changes. And then, yeah, if you ever need to switch back and work on a different branch or on master, you could stash your changes

and work on a different branch or on master, you could stash your changes or you could do a work in progress, commit, and then you would switch back to your main branch and, and you're fine. And this works and this is how we traditionally do it. But things are a little different now that AI agents are included, right? Imagine you have two different AI agents that are working on totally different features.

Imagine you have two different AI agents that are working on totally different features. How do we deal with that, right? Do we set up two tabs? For example? Do I boot up Claude here and then I also have maybe Junie running here? Do we do those concurrently? Well, we could try, but there's probably going to be some overlap, right? One agent changes this file and the other agent deletes the file entirely.

One agent changes this file and the other agent deletes the file entirely. Like, we don't wanna do this, right? So what's the solution? How can I work on the code? But then also have two agents also working on the code, but also there's no interference. Well, you can't really do it with a simple branching like this. Instead, we need to set up a dedicated work tree. Now, definition alert, very, very quick.

Defining Git Worktrees2:11

Instead, we need to set up a dedicated work tree. Now, definition alert, very, very quick. A work tree is nothing more than a separate directory that points to the same GI history, and that's it. That's all there is to it. But the cool thing is you could have as many of these separate directories as you want. You could even have one per agent. Hint, hint. So let's imagine, let's say I'm working on the code base, but then it occurs to me, you know what?

So let's imagine, let's say I'm working on the code base, but then it occurs to me, you know what? I wish an agent could work on this bookmarking feature, and I wish I didn't have to just pause and wait for it to finish before I feel comfortable interacting with the code base again. So instead, I'm gonna create a work tree and I'm gonna switch to that new directory and I'm gonna boot up my agent and it will get started.

and I'm gonna switch to that new directory and I'm gonna boot up my agent and it will get started. And then I can keep working with the assurance that there's no interference or collision. But then a minute later as I'm working, I'm, I'm thinking, you know what? It would be cool if we could also implement this highlights feature, but I don't wanna work on it. And agent number one is busy. Maybe I could create a work tree

And agent number one is busy. Maybe I could create a work tree and have a second agent booted up in its own workspace effectively and it can work on this feature. So now we have three features being worked on simultaneously, once again, with the assurance that there's no collision or overlap in the entire process. That is what work trees allow for. Lemme show you open the terminal and run, get work tree add,

Creating a Worktree3:31

Lemme show you open the terminal and run, get work tree add, and specify a path to where this new directory will live. Generally, it'll be a sibling, right? So we go up a level and a common conviction is, is to use the same folder name, dash, dash, and then whatever the branch or the feature happens to be. So in our case, if we are working on bookmarking, we might do this. And then as a general good rule,

bookmarking, we might do this. And then as a general good rule, I would recommend always create a branch in the process and I'll tell you why that's helpful shortly. So let's also call the branch bookmarking. All right, so now we have a new work tree. So check this out. If I were to CD into the parent directory, I will now have a new workshop bookmarking folder. So you see what I mean?

I will now have a new workshop bookmarking folder. So you see what I mean? It is a separate directory, but it still points to the same get history. As you can see here, we have some work in progresses, refactoring queries, it's the same get history, okay? So this is really cool. And then also, if I were to go back to my main uh, project, I want you to notice if I run GI Branch, of course we're gonna have

I want you to notice if I run GI Branch, of course we're gonna have that bookmarking branch available here. So that's a key thing to understand. It's still a branch, but in our case with the work tree, the work tree points to that branch and the branch doesn't even care that that separate directory exists or not. It's irrelevant. Okay? So that means if I were to remove the work tree,

It's irrelevant. Okay? So that means if I were to remove the work tree, the branch would still exist. That's very important to, to comprehend. Let's CD and to Laravel workshop bookmarking and open this in PHP storm. All right, so pay attention. We are now within the bookmarking work tree and also the branch as you can see right here. So now, yeah, just proceed as usual

and also the branch as you can see right here. So now, yeah, just proceed as usual to implement the feature. So if you're using an agent, you could boot up the uh, JetBrains AI in this case, maybe you want to use Junie and you could start on your plan. In this case, let's keep it very, very, uh, micro. I'm working on a bookmarking feature. To start, I just want you to create the migration and eloquent model for a bookmark, a user

To start, I just want you to create the migration and eloquent model for a bookmark, a user can bookmark a post. So we need to make sure we store that information. Alright, A little vague, but this is enough to illustrate the point. Let's get started. All right, so Jenny's working on the plan. Let's run the commands. All right? And it's done. So in this case, we can see it created a handful

Let's run the commands. All right? And it's done. So in this case, we can see it created a handful of files as you see right here. And this is our bookmark. We have a bookmark factory. Uh, again, we don't really care about the code in this example, but it's great. No problem here. Okay, so now think about it. The agent is working on this project, but simultaneously, I could have been working on the main project.

but simultaneously, I could have been working on the main project. Let me show you. Let's switch back to the original Laravel workshop folder. All right? And we're back. And yeah, of course notice there's not gonna be any reference to our bookmark files, of course, okay? But now if we wanna pull it in, just merge it like you always do. You could say get merge bookmarking,

just merge it like you always do. You could say get merge bookmarking, because again, it's just a branch and GI doesn't care that it's pointing to that separate folder. It's irrelevant, right? And in fact, just to show it to you, if I were to remove that work tree entirely, I could say get work tree remove, and then provide a path. So that was what Laravel workshop, uh, bookmarking,

and then provide a path. So that was what Laravel workshop, uh, bookmarking, okay, now it's gone, but of course the branch still lives. So I could check out the bookmarking branch. And now we have access to all of the bookmark model and all of those files. It's just a checkout branch. But in the case of a work tree, it exists within a different directory. So cool, right? Okay,

Laravel Setup Caveats7:13

it exists within a different directory. So cool, right? Okay, so now I just wanna wrap up by showing you a few caveats that you should be aware of, especially in the case of Laravel. Let's go, let's boot up the terminal. Let's switch back to our main branch. And in fact, let's just delete bookmarking entirely. And yet let's do this. Let's create a new work tree from scratch.

And yet let's do this. Let's create a new work tree from scratch. So get work tree add. And then once again we'll do Laravel workshop bookmarking. And I'm gonna check out a branch and the process. Okay, so now, yeah, we're gonna have this new directory, right? And because we're using Herd or Laravel Valet, potentially, well, we should be able to just access this directly within the browser, right?

or Laravel Valet, potentially, well, we should be able to just access this directly within the browser, right? And it should just work, right? Well, let's see. Alright, so if we visit that work tree filter name test, which is the Convention for Herd, but if we take a look it fails, we get some reference to Vendor slash Auto Load. Now you may know, oh, that means we need to run composer install. And that makes perfect sense, right?

to run composer install. And that makes perfect sense, right? This is an inversion control. So when we create a new working directory, we would have to go through those usual steps. All right? So these are things we need to keep a mental note of. And of course we can automate this process as a bash script. Let's keep going. I'm gonna CD into that, uh, work tree or that workspace.

Let's keep going. I'm gonna CD into that, uh, work tree or that workspace. We're gonna run composure install and we'll say check. That's something we need to do each time. Alright, switch back, give it a refresh, and now we get a server error. All right? That's kind of weird. So let's see what the issue might be. Let's switch over to that project. Alright? And again, pay attention to the menu to remember, uh,

Let's switch over to that project. Alright? And again, pay attention to the menu to remember, uh, which workspace we are in. Let's go into Laravel dot log. Ah, we get no application key has been specified. And further, if I open up the sidebar, you'll notice we don't even have an ENV file. So we need to create the environment file. And then we also need to generate the key. And as it turns out, there's a couple other things you have

And then we also need to generate the key. And as it turns out, there's a couple other things you have to do as part of a Laravel setup. So check this out. If I go into Composer, Jason, if I scroll down to the script section, you'll see that there are some hooks that will automatically fire when we create a project through Composer. Take a look at this one. After you create a project, generate the key, set up the database sq light, uh,

Take a look at this one. After you create a project, generate the key, set up the database sq light, uh, sq light file, and then run your migrations. And then there's also one called Post Route package install. So again, these will run automatically in certain situations, but in our case, we could also just trigger them manually like this composer run. All right, so that creates the environment file and now I have access to that.

All right, so that creates the environment file and now I have access to that. Cool. Next we could run this one composer run, paste that in. All right, generates the key, creates the database, runs the migrations. Now if I switch back, we're getting closer, we've changed the error, now it can't find the v manifest. That would make sense. We haven't built our dependencies yet, so once again, I would install my dependencies.

That would make sense. We haven't built our dependencies yet, so once again, I would install my dependencies. And then let's also run NPM run production to build them. Or I'm sorry, what is it? Um, I can't remember. Mine's different from my main project. Uh, NPM Run Build. That's right, it was changed. Okay, so now let's cross our fingers, come back and there we go. Now it seems to be working. And yeah, that's just what you would do if you encounter

Automating With Bash Script10:41

Now it seems to be working. And yeah, that's just what you would do if you encounter anything where it's like, oh, this bootstrap cash directory doesn't exist, then you make a mental note to update your bash script to always do that, uh, whenever you, you prepare a new work tree. So why don't we wrap up by having AI generate one of these for us. Let's start up a new chat this time just for variety, we'll use the Claude agent.

Let's start up a new chat this time just for variety, we'll use the Claude agent. So let's say I want you to help me write a bash script that is responsible for creating a new Git work tree for a Laravel app. As you may know, there's a number of commands that need to be run in order to configure the Laravel app. So the bash script should create the work tree and then also run those commands in the process. Alright, so let's say it needs to run Composer install

and then also run those commands in the process. Alright, so let's say it needs to run Composer install NPN install, then composer run, uh, post route package install, and that just sets up the environment file. It will also manually run post create project that will, uh, again configure the database and run the migrations. And again, I think, I can't quite remember, but I think we might need to make a storage cache directory. I can't quite remember though,

but I think we might need to make a storage cache directory. I can't quite remember though, so we can come back to that later. Finally, I'm gonna finish up by saying, um, my workflow, preferred Workflow end current project I run then slash work tree feature and that will create a new, um, project name dash feature work tree. It will then CD there and run the commands above. Yeah, just something like that to get us going.

It will then CD there and run the commands above. Yeah, just something like that to get us going. Okay, so now i's gonna generate a Bash script for us that we can use. All right, that was super quick. Why don't we view Unified and let's see. All right, so it's set up as a bash script. Um, we store some variables here, like the name of the project, the name of the work tree following the convention

of the project, the name of the work tree following the convention of Dash dash branch. It will, um, have some logging. It runs GI Work Tree ad, just like we did go into the new folder name, set up the branch, then configure the layer of a app. This actually looks, uh, super nice, of course. Next, because we have a Bash script, we need it to be executable.

Next, because we have a Bash script, we need it to be executable. So we run this typical one. You've likely done this many times and it's done. So we should now have access, uh, to this script. Let's test it out. I'm excited now. Oh, in this case we're already within a work tree, which I don't love. Okay, let's do this real quick. Let's, uh, grab Work Tree

Okay, let's do this real quick. Let's, uh, grab Work Tree and I'm gonna switch back to my main project, uh, to simulate us starting from scratch. Here we go. All right, sorry about that. So very quickly, let's add Abin directory and then with end there we'll add a file called Work Tree. And then I'll paste that in. Okay, now very quickly we would commit that and now we should, after I set the permissions, uh,

now very quickly we would commit that and now we should, after I set the permissions, uh, we should be able to very easily create a new work tree for Laravel Project that will take care of all of the annoying, uh, setup, uh, steps. All right, let's go Ben Work Tree and what feature are we working on Now? How about Read Me, how about highlights? Okay, get Work Tree highlights. All right, yep. Take a look. It creates the work tree.

Okay, get Work Tree highlights. All right, yep. Take a look. It creates the work tree. It installs all of the dependencies Work tree created successfully. And this is great. So now for example, if I were to CD here, list the files, of course the environment files should already exist among other things. Like right there it is, everything's been configured for us. And then further, we should be able to access this directory and the browser and herd.

And then further, we should be able to access this directory and the browser and herd. Of course we'll do its magic. So we want workshopping and we call this highlights. Oh, and it fails. We forgot to, to run our build. Okay, that's fine. We can come back. Uh, NPM Run, build, I knew I forgot something. Come back, refresh. Yeah, that's working. So yeah, the final step of course, we could return to our bin and then update this to also run.

So yeah, the final step of course, we could return to our bin and then update this to also run. I'll do it right here. Building the assets. NPM Run Build. Yeah, that was the missing step. We forgot. But yeah, mostly this is just a simple bash script. And the important thing is to understand what exactly happens when you add and remove a work tree. Tell me what you think. I.

Tell me what you think. I.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟