How I use Git Worktrees in my developer workflow

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello in this video I'm going to walk through my typical developer workflow what I do day to day to get things done on projects that I'm working on as well as how I collaborate with others uh to check code do code reviews uh run tests locally check for problems on other branches things like that so let's dig right in the first thing that I do is I bring up my terminal so in this case it's Wes term I really like Wes term a lot I think it's one of the best looking and fastest terminals I've ever ever used and I jump right into a tmux session so I use TM a quick script I wrote to get to the available teamwork sessions I have or to create a new session like I do in this case so in this video I'm going to use a project called xstate meme which is a project I did for a conference talk that uses x-state and creates memes so I'll create a new project called that and it just drops me in a new tmux session and I put everything into a uppercase developer folder on my Mac the only reason I do that is because when you're in your when you're the cell the few cases that you're in finder developer gets a special Hammer automatically I don't know started doing it uh anyway let's create the project here so another thing that I do is I rely on scripts from my DOT files and so I'm going to create another tmux session called dot files and then I'll use the Z command to quickly jump over into my DOT files and this is where I'm going to use a script that's in the bin directory called get bear clone and this script allows me to quickly create a bare repository uh and check it out in such a way that I can easily manage it and it's uh it's pretty because I use git work trees for everything and so I'll explain kind of what this does but the the gist of it is that a bear repo is just a repo that you clone that doesn't actually create its own work tree or working directory so then I'll use work trees to create those working directories for everything that I work on from there and so this does a lot of nonsense but the the main things that it does is it creates a work Tree by cloning with dash dash bear and then it automatically adjusts the uh fetch refs for everything and it puts it into a location uh that is going to be the the folder that I create and it'll put it into a DOT bear directory so instead of having everything just plainly in that folder uh all of the contents of the dot get directory in that folder they'll be in a hidden.verifac.bear folder and then I will create a file called dot get that will just specify a get dur to go to that bear location and so that's what it'll do let's take a look at what this does we'll switch back to the X8 meme project and what we'll do is we will make a directory called xstate meme and then we'll CD into that now we're in here there is nothing in that directory and so we need to get the actual project so let's go to GitHub it's x-state meme and in here so I'll just grab the uh the SSH here for it so we can clone it and we can just do get bear clone and because this is in my bin uh which is on my path and it starts with get Dash you can just run it with the get command and it'll act like just another command for that and so I will say uh get X State meme and so we'll clone that here authorize it and I use I put all of my SSH Keys into one password so that's what that was it was asking for my fingerprint so that it could use the SSH key to grab that all right so it created a bear repository so now if we do a list inside of this project we see nothing but if we show hidden files there's two files a folder and a file actually the dot get file and a bear file server folder so if we look at that they dot get just points to bear and then bear just looks like a normal dot get it just has everything in there for us now the cool thing that I can do is I can do things like a git fetch from in here and I can pull things out there's still nothing created uh and so that's the way I like to work everything is kind of in this folder and then if I want to work with something then I can create a work tree from here quickly to do that and so I'll just do get worktree add and I'll call it main just to check out Main and that will create a work tree called Main and then I'll just say check out the branch Main in there and so when we do that now we have a main directory so this is where I will actually work from I'll just load everything from within here and work so this is really nice because now I can do things like I have this totally and completely separate from everything else I can npm install inside of this directory and this is where I would typically have like main checked out just so I can do like a fresh check all the time so this is like one of the more permanent work trees that I have around in each project is the main just to show like hey this is this is the quote unquote good state of the project so then from there I would usually create a new work tree and I would do something like Dash B and then I'll usually prefix it with my name and then I'll do something like uh we'll we'll do you know new feature and that'll create a new branch called new feature and then uh we'll call that new feature that's the folder that or the the work tree that we're going to actually create for that and then I'll say base it on origin main so it'll just base it on what the the main is for that and so now we look we have two work trees so I can quickly switch between the two and I might have them even checked out as different um as different sessions within tmux and so we could go um and rename this session oops rename the window to xstate meme Main and then I can just do a new session and we'll call it xdate meme new feature and now if I go to my list of available tmux sessions I've got X date Meme and the new feature so I can quickly jump between them the really nice thing about this is that it allows me to work on one thing at a time and then when I'm not working on it it's completely hidden and I don't have to look at any other pieces of it so I mentioned that Maine was a worktree that I typically keep around another one that I will typically keep around all the time is one called uh review and so in this I will just call it review and I'll check out origin main again um within there but now when I'm in review the really nice thing about this is this is just like a a place that I can go to do PR reviews and I can check out branches easily within that and I can use the GH command to check out you know a different PR from there so I can say like check out 34 or whatever um oops ghpr check out something like that and then that'll automatically just check it out right to here and then I can run it and check and test it so I can be doing that you know I can be running tests or or doing things while in another Branch another session I've got everything set up so that I can just run it from here so I'll go back into XD meme and I'll go into new feature and I'll install from here but I can be running and working in this session simultaneously while I'm working in another session and I'm not stepping on it I'm not you know adding files or changing things or having to stash anything I can just leave everything as is switch over to another work tree work from there and everything is totally fine and then I can just stop that go back to the other one and uh work from there so it's totally just a nice Easy Flow for all of that I really like that so when I'm in here uh the other thing that I I typically do is I'm working on um I'll be working on some new feature and let's just go ahead and change make some changes so I use neovim for all of that I've got everything installed we'll just go into here and we can make some some quick changes so there's a log statement in here I'll go ahead and delete that probably don't want that in here and oh when I hit save prettier went and changed everything so that's fine uh and when we go to work with this I I do all of my get from the command line as well and what I do is I use lazy get which I open in a floating tmux window anywhere so in any project that I'm in at all I can just say prefix G and it brings up the get the lazy get for this project and then I can go in and use the commands within here to you know quickly uh stage that change and then I can do a commit and I can say formatting and remove console log statements so we'll just have that be our commit summary we'll just say refactor and that changes it and then we can push that up if we want to uh I won't for this in this case and then I just hit Q to close that and I can bring it up at any point at any point by just hitting prefix G again so that's really cool so another thing that I will typically do is as I'm working I'll be making those changes and I'll probably need to run the server right so uh as you can see up here I've got everything kind of set up to be um you know the main window within tmux is my Vim session so it uses the Vim um the Vim symbol there but I'll probably need to run a server and so I'll usually just create another window within here oops and then in here uh you know it automatically puts me right back where I was so I don't have to keep CD around to other directories or anything but then this is where I can just do things like npm start the project and kick it off and now I'm at you know localhost 3000 so I can bring up my editor or my my browser to test that I typically don't use Safari for day-to-day testing I'll use a developer browser called sizzy to do that and let me just bring that up and show what it looks like a bit like this and so I can just bring up localhost three thousand and load up the project from here and then of course it's got its Dev tools and everything but yeah this is typically how I work I will have this over on another monitor uh while I'm working on my main monitor here in Vim and switch back over here so now the server that's running and any other commands you know if I have to have another server maybe I want to run like storybook or something I can run that over here this project I don't think it does have a storybook actually uh so I can run storybook get it set up over here and I can have them running simultaneously uh right here and then when I want to go back to my editor I just switch back over here now for quick commands that I want to do maybe like running tests or something like that I'll typically create a split within my window that's typically uses Vim I'll either have it on the the side here or I'll put it at the bottom uh like this and kind of work with it like that kind of depends on what I'm doing typically tests I like to have more vertical space so I'll have it over here like that and then I can just do things like you know npm test and run it over here this project doesn't have tests so I need to to add some of those but uh that's typically how I'll I'll run that and then when I'm working I do also use the the GH command uh quite a bit so I used you know normal get for for like committing things but then if I wanted to create a PR I'll do things like ghpr create and uh depending on how complex I want it to be I'll either create it here in the the terminal or I will uh just do Dash web and that'll open up a browser for me um so that I can start doing that right away so there created a new one it pushed up my changes created a new one and you can see the changes that are there so that's typically how I work uh it's you know a quick rundown uh the main drivers of everything are obviously the terminal but also working within work trees that's a really important part of my day-to-day process just to to manage everything to quickly be able to jump in and out of projects and to um to work on things kind of simultaneously if I need to because I do a lot of like code reviewing and things like that or or checking you know there's a problem in this Branch let's go find out what's going on and I can just kind of pause the work on one branch switch over to the other one go run it check it test it and then come back uh and and that's really nice and I pretty much always have another team up session open for my DOT files because I'm constantly tweaking them throughout the day as I go so hope this helps just a quick video rundown and if you have any questions nicknamese.com
Info
Channel: Nick Nisi
Views: 4,485
Rating: undefined out of 5
Keywords: git, neovim, shell, tmux, vim
Id: VMJ1eAj5HgI
Channel Id: undefined
Length: 15min 37sec (937 seconds)
Published: Tue Sep 26 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.