Honestly, when I watch Linux related videos (git, vim, window managers, core utils, configurations, Emacs) I feel like prehistoric cave-man who somehow got on the alien's spaceship. Sometimes I can even understand how things work
129
Reply
Cool feature - thanks for sharing I would mention that you actually don't need the "git clone --bare <repo>" you can just name your "master" clone accordingly eg git clone ${repo} ${repo}-master cd ${repo}-master And then for each branch you want to work on in a worktree git worktree add ../${repo}-${branchname} #<-- note the "../" infront of the name And then you'll have 2 normal working checkouts in separat directories that are just each branch and you can just switch project/work dir in your IDE of choice. Also if you forget to add the "../" when adding a worktree to the "master" checkout resulting in a new directory that isn't committed, then there is "git worktree move <dir> <new-dir>" command to move it accordingly.
47
Reply
I have actually been using this feature for years as a deployment mechanism to deploy different states of the code to different folders that are mapped to different sites, to test different behaviours side by side in test/integration environments
59
Reply
You should create series how to use git ;)
318
Reply
Pretty cool feature. I don't think I'll integrate into my workflow because git stash works for 99% of my use cases (I'm not often being asked to change directions in the same project), but good to have the knowledge about worktrees if I'm ever asked to change it up and do more work on a singular project.
74
Reply
Hey! I recently got into all these sweet tools and I started git recently, but I only use it to have a trace of all the changes I do on important projects. I love the way you talk and present things! I'd love to see you present a way to work with git and explaining its tools. About what you said to another comment: that people will say there is a better way ect... you can say that : this is my way of doings things, I don't say it's the best, I just present mine so people that are new can see the possibilities. You're not being a teacher, you're still presenting stuff :D Thank you for your content !!!! Cheers from France
16
Reply
Cool to see that even the Primeagen gets files like ":w" sometimes :)
50
Reply
It’s been 2 years… still the best thing since sliced bread?! In all seriousness, a two year later retrospective would be great!
11
Reply
Interesting, I just never had this problem you pitch at the start. I rarely even stash, stashes are too easy to forget about. If I have to check out a different branch while having uncommitted changes, I just do a WIP commit with a meaningful name that will be sitting right there when I get back. It's to be amended later. On the remote, there is a push rule that rejects any commits that start with WIP. So these WIP commits are self-descriptive, you don't forget them and you can't push them by accident. As for the worktree, I feel like having a bunch of different folders with uncommitted and unnamed changes hanging around would create more problems than it would solve.
21
Reply
Ohh my I just woke up and this is what I watched I have been searching for a proper way to do this forever Man you are a life saviour @ThePrimeagen
8
Reply
You blew my mind in the first minute. This is game changing. Thanks prime! <3
10
Reply
How the hell did I not know about this? Awesome walkthrough. Thanks!
16
Reply
What I do when I need a separate workspace is to simply create another folder and clone from the other local folder which was already cloned. I can then push and pull from the origin which is simply the other folder. I can name the remote whatever I want and do this with multiple folders to isolate my work and make it easy to move commits between them.
12
Reply
BRUH. This is going to make the work at my day job SOOOOOOO much easier. Legit AF
10
Reply
Holy cow I am excited....you know how I was doing this? I was checking out multiple copies of the repo...what a PITA.....I AM FRICKEN STOKED!
3
Reply
I just downloaded your worktree plugin, my life is forever changed now! Thank you so much for making my life easier!
Reply
Oh boy...every year I have 4-5 of those "huh, I need to adopt this right now" moment. This is definitely one of those, thanks for explaining Git worktree !
Reply
Did he touched his mouse o.O This is not the content for what I am subscribed for! :' D Keep going man, love it
2
Reply
Prime, thank you. You gave me the idea, since I'm a CTO, to force all coders to use worktree! Now my suggestion: you should squeeze that in, squeeze the work flow! Let me explain. With worktree you manage the work at a higher level, top-down. Also teach the noobs about git add -pv, gotta squeeze it from below also! Today I've had my first success. After preaching about hunked staging for weeks, the junior of the team finally asked me "what was that command again?".
2
Reply
I've been wanting a slick way to view two branches at the same time and edit them simultaneously. BOOM! Problem solved. Thanks, Prime!
Reply
Damn... you know how to blow my mind with git things. Last time was with fugitive and now with this. Amazing! Thank you.
2
Reply
Aaahh, I need this in my life. I can just checkout code to review it without disrupting what I’m working on. Thanks!
2
Reply
I would have some issue with JS projects needing to have a humongous node_modules folder under each worktree directory, but I can see this being super useful for other types of projects where I'm jumping back and forth between branches constantly to test changes or find differences. It can be really useful to have two IDE windows open with a different branch in each, and I used to just clone to another directory. No more!
35
Reply
I like worktrees, but one of the downsides is support. Often when some tool has git support, it doesn't tend to have worktree support :(.
13
Reply
As usual, your videos are awesome, keep it up Primeagen!
3
Reply
Instant subbed to your channel. This video is so informative and fun that makes me want to go back to record more YouTube videos. You rock! (Going back to re-watch it for the n-th time now.)
6
Reply
This is super interesting, thank you for sharing! I'm trying to think of what the best way to integrate this for me would be. My current flow is to have little "buttcap" commits when I have to switch contexts; something like `git add -u && git commit -m "TMP"`. Then when I come back, I `git reset --soft HEAD^` and continue where I left off. It seems like worktrees might be useful to have like different "lanes"? So I could have a main worktree for my primary development, and a maintainer worktree for reviewing PRs/etc. That would be good because node_modules/etc would also be independent. Not sure. Regardless, glad I learned about this! Time to read some docs :) Edit: Oooh! Also for longer lived branches, like major version bumps.
7
Reply
That's truly awesome! We'll definitely give it a try.
2
Reply
I find git stash / commit and switch to be my preferred approach. Reviewing a ton of branches and going through a variety of commits by several people would be insane to have separate worktrees for each
Reply
The bare repo stuff is new to me, makes a lot of sense to use with worktrees. Thanks!
1
Reply
You can use fixup during a rebase to drop commit messages for work-in-progress commits. Works great.
1
Reply
Great, I didn't know about git worktree until seen the Prime fight againt the Foo !
2
Reply
OMG I did not know this existed. this is a huge game changer for me!!
Reply
Omg... this feature is mind blowing. Yet another amazing thing about git that I never even knew existed.
1
Reply
Awesome, I didn't know that I needed this. Thanks
3
Reply
This is so good they should advertise it on every git command ;)
Reply
We squash each PR, so commits with "wip" in it are pretty regular, because they get squashed anyway. The name of the commit is whatever you pass into the PR that we create in BitBucket.
Reply
Was use it couple years ago, but it does not work well with large project in monolith modular architecture for some reason. my alternative approach is just to post a base PR to remote and then create branch based on that then merge back to the base branch once it gets reviewed.
Reply
I wanted something like this bad enough I added another command to my local git that runs a bash script. Yes, you can do that. I call it "git save". It simply piggy backs off "git stash" and creates two stashes with the "save" name prepended on both stash messages for the script to identify them, instead of relying on the stash order. One stash is for staged changes, the other for unstaged changes and uncommitted files. I can then run "git save as NAME", "git save view NAME", "git save drop NAME", and "git save list" to manage "saves". The only benefit workloads would have over this is that they would also have appropriate dependencies and builds in isolated folders ordered by branch, which can be nice. My solution allows you to do this all in the same repo,but with shared deps and builds. So, they both have their pros and cons.
Reply
This is insane i was dreaming about such a feature for months
Reply