Git's Best And Most Unknown Feature

ThePrimeagen · Intermediate ·🛠️ AI Tools & Apps ·5y ago

Key Takeaways

The video discusses Git's work trees feature, demonstrating its capabilities and the speaker's workflow with vim, and showcasing tools like getworktree and telescope for worktree management and automation.

Full Transcript

so get work trees are incredible if you haven't used them oh my goodness they fixed so many things that are wrong with my current git workflow so before i tell you what git work trees are i thought it would be better if we talk about why you should use get work trees first have you ever checked out a project started working on and then need to pivot right you either have a message from a co-worker 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 so you know 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 co-workers are like wtf with us so what is this squash message you got here this is not even that good and you have to do this you have to check out something else you have to do some work and then you have to like uncheck out and go back and then unstash or look at your commit and try to remember what you're doing right it's just it's just like not that great right it's just like not a fun experience so work trees 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 haven't been used to get that long because this is 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 work trees 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 get work tree right away and let's do a get clone bear now bears the keywords you need to use here and then go get work tree dot and vim there we go so now we're gonna 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 git worktree 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 get 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 at let's see get work tree add foo bar a bass 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 readme i have master and i have fubar bass if i were to go into foobar baz it would be fubar bass 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 complete it like that let's just remove foo bar bass 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 foobar baz and force there we go if we re-look at our list of items you'll notice that foo barbaz is gone so when i saw this i immediately just i just fell in love i fell in love i i knew for that moment that get work trees were gonna be in my future thoroughly i've been in the process of changing over all of my work projects into bare 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 seemed so arcane and distant so at this point i didn't want to have to write all these commands cd around open and close vim do all that so i wrote a quick plugin called get worktree 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 actually edit the wrong code and we created a telescope plugin to make worktree operations easier all right 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 two 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 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 worktree operations using telescope to help guide me on top of that i can tell where i'm at because the status line and lastly those operations are provided by the getwork3 plugin so at work i have a large c plus 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 five ten 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 worktree 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 for you to tap into changes now notice that i can just simply do entry 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 am going to go and execute get some module update in that repository so 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 i had no idea that osha had rules even for this hey thank you for watching please check out worktrees they will make your life a thousand times better because you still get all the standard usages of git 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 plug-in that i showed you the 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 thousands of people at this point my name is the primetime thank you for watching

Original Description

HOW HAVE I NOT HEARD OF GIT WORK TREES??? WHAT THE EFF. They are so incredible. You have to check them out!!! In this video I go over them briefly, assuming you are smart enough to understand them, and also show you my workflow with vim! Its fantastic! ### Links Linode: https://linode.com/prime Discord: https://discord.gg/3ujcVMe Twitch: https://twitch.tv/ThePrimeagen Insta: https://instagram.com/ThePrimeagen Twitter: https://twitter.com/ThePrimeagen VimRC & i3: https://github.com/ThePrimeagen/.dotfiles Keyboard (15% off, I don't earn commission): https://bit.ly/primeagen-adv2 Keyboard: https://ohkeycaps.com/products/theprimeagen-dactyl?aff=2 (PRIMEDACTYL $25 off) #vim #linux #git
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from ThePrimeagen · ThePrimeagen · 2 of 60

1 Two Simple Steps to go from IDE to Vim
Two Simple Steps to go from IDE to Vim
ThePrimeagen
Git's Best And Most Unknown Feature
Git's Best And Most Unknown Feature
ThePrimeagen
3 My Developer Workflow - How I use i3, tmux, and vim
My Developer Workflow - How I use i3, tmux, and vim
ThePrimeagen
4 Github Copilot MAKES A CLI GAME IN GOLANG FROM SCRATCH?!?!
Github Copilot MAKES A CLI GAME IN GOLANG FROM SCRATCH?!?!
ThePrimeagen
5 I made the greatest tool ever! | tmux & cht.sh & fzf
I made the greatest tool ever! | tmux & cht.sh & fzf
ThePrimeagen
6 Is TypeScript (NodeJS) Faster than Go?? |  A server comparison
Is TypeScript (NodeJS) Faster than Go?? | A server comparison
ThePrimeagen
7 How to make TypeScript Blazingly Fast (JavaScript/NodeJS)
How to make TypeScript Blazingly Fast (JavaScript/NodeJS)
ThePrimeagen
8 Learn Vim in Less than 2 Minutes
Learn Vim in Less than 2 Minutes
ThePrimeagen
9 The biggest lie about programming?  REACTING to tech twitter
The biggest lie about programming? REACTING to tech twitter
ThePrimeagen
10 Rust or Go for my next project? WHAT TO CHOOSE? (as a senior intern engineer)
Rust or Go for my next project? WHAT TO CHOOSE? (as a senior intern engineer)
ThePrimeagen
11 Go is faster than Rust??!  Go vs Rust vs TypeScript Servers (as a scientist)
Go is faster than Rust??! Go vs Rust vs TypeScript Servers (as a scientist)
ThePrimeagen
12 You only Git Merge?!? feat Theo : DevHour #1
You only Git Merge?!? feat Theo : DevHour #1
ThePrimeagen
13 YOU DONT UNIT TEST??? DevHour #1 Theo
YOU DONT UNIT TEST??? DevHour #1 Theo
ThePrimeagen
14 Tech TikTok - Is this really programming? | React
Tech TikTok - Is this really programming? | React
ThePrimeagen
15 I am dying
I am dying
ThePrimeagen
16 Comparing Rust (Yew) vs TypeScript (React) NodeJS | SSR times
Comparing Rust (Yew) vs TypeScript (React) NodeJS | SSR times
ThePrimeagen
17 Why I use Vim in 2022
Why I use Vim in 2022
ThePrimeagen
18 How programming feels
How programming feels
ThePrimeagen
19 Why I Quit Netflix
Why I Quit Netflix
ThePrimeagen
20 Programming on TikTok
Programming on TikTok
ThePrimeagen
21 If I could give advice to myself when starting as a software engineer
If I could give advice to myself when starting as a software engineer
ThePrimeagen
22 How am I suppose to get a job as a software engineer?
How am I suppose to get a job as a software engineer?
ThePrimeagen
23 Why do I feel burnt out? (software engineering)
Why do I feel burnt out? (software engineering)
ThePrimeagen
24 I am done with vim
I am done with vim
ThePrimeagen
25 Maintaining Motivation as a Software Engineer
Maintaining Motivation as a Software Engineer
ThePrimeagen
26 Bun (TypeScript) is faster than Rust!?!
Bun (TypeScript) is faster than Rust!?!
ThePrimeagen
27 Go is blazingly faster than Zig?
Go is blazingly faster than Zig?
ThePrimeagen
28 Facebook Recommends Rust?
Facebook Recommends Rust?
ThePrimeagen
29 5 Things I Hate About JavaScript
5 Things I Hate About JavaScript
ThePrimeagen
30 Why I Make Content
Why I Make Content
ThePrimeagen
31 Is ADHD Bad For Programming?
Is ADHD Bad For Programming?
ThePrimeagen
32 Vim As You Editor - Advanced Motions P1
Vim As You Editor - Advanced Motions P1
ThePrimeagen
33 How I Would Get My First Job If I Started Over
How I Would Get My First Job If I Started Over
ThePrimeagen
34 Vim as your editor - Advanced Motions P2
Vim as your editor - Advanced Motions P2
ThePrimeagen
35 My Best Decision Professionally
My Best Decision Professionally
ThePrimeagen
36 Should you use RUST as your FIRST programming language?
Should you use RUST as your FIRST programming language?
ThePrimeagen
37 How I Made JavaScript BLAZINGLY FAST
How I Made JavaScript BLAZINGLY FAST
ThePrimeagen
38 Is JSON Blazingly Fast or...?
Is JSON Blazingly Fast or...?
ThePrimeagen
39 Vim As Your Editor - Introduction
Vim As Your Editor - Introduction
ThePrimeagen
40 Vim As Your Editor - Horizontal
Vim As Your Editor - Horizontal
ThePrimeagen
41 Astro IS SO GOOD
Astro IS SO GOOD
ThePrimeagen
42 STOP LYING, The truth about Software Engineering
STOP LYING, The truth about Software Engineering
ThePrimeagen
43 Does Your Editor Matter?
Does Your Editor Matter?
ThePrimeagen
44 React Is This Fast???
React Is This Fast???
ThePrimeagen
45 Vim As Your Editor - Vertical Movements
Vim As Your Editor - Vertical Movements
ThePrimeagen
46 Rust for TypeScript devs : Borrow Checker
Rust for TypeScript devs : Borrow Checker
ThePrimeagen
47 Will ChatGPT Terminate Your Job?
Will ChatGPT Terminate Your Job?
ThePrimeagen
48 0 to LSP : Neovim RC From Scratch
0 to LSP : Neovim RC From Scratch
ThePrimeagen
49 From Meth To Netflix
From Meth To Netflix
ThePrimeagen
50 Faster than your favorite JS framework?
Faster than your favorite JS framework?
ThePrimeagen
51 Primeagen Answers the Webs Most Asked Questions for a Software Engineer
Primeagen Answers the Webs Most Asked Questions for a Software Engineer
ThePrimeagen
52 This Algorithm is 1,606,240% FASTER
This Algorithm is 1,606,240% FASTER
ThePrimeagen
53 Looking Under the Hood of JavaScript
Looking Under the Hood of JavaScript
ThePrimeagen
54 What Next...Finding a New Job In Software
What Next...Finding a New Job In Software
ThePrimeagen
55 JSON, I hardly know 'er
JSON, I hardly know 'er
ThePrimeagen
56 RUST Enums ARE Better
RUST Enums ARE Better
ThePrimeagen
57 CoPilot Review: My Thoughts After 6 Months
CoPilot Review: My Thoughts After 6 Months
ThePrimeagen
58 How to Vim in 2023: Tips and Tricks
How to Vim in 2023: Tips and Tricks
ThePrimeagen
59 Twitter's Algorithm Has MASSIVE Problems
Twitter's Algorithm Has MASSIVE Problems
ThePrimeagen
60 Rust Foundation IS DOING WHAT????
Rust Foundation IS DOING WHAT????
ThePrimeagen

This video introduces Git's work trees feature, which allows multiple commits to be checked out simultaneously, and demonstrates how to use tools like getworktree and telescope to manage and automate worktrees, simplifying Git usage.

Key Takeaways
  1. Clone a repository
  2. Use `git worktree add` to add a new worktree
  3. Remove a worktree using `git worktree remove`
  4. Create a branch using the getworktree plugin
  5. Switch between branches using telescope
  6. Hook into changes in the worktree using the getworktree plugin
💡 Git work trees can greatly simplify Git usage by allowing multiple commits to be checked out simultaneously, and tools like getworktree and telescope can automate and streamline worktree management.

Related Reads

📰
Your AI audit tool is a lead generator, not a final verdict
AI audit tools provide leads, not final verdicts, requiring human evaluation and disposition of recommendations
Dev.to · Jeremy Burgos
📰
10 AI Image Prompts Every Content Creator Can Use for Urdu/English Quotes
Learn 10 AI image prompts for creating Urdu/English quotes, enhancing your content creation skills
Medium · Machine Learning
📰
The Best AI Course Generator in 2026? I Tested 7 to Find Out.
Discover the best AI course generator through a comparison of 7 platforms, and learn how to choose the right one for your needs
Dev.to AI
📰
Automate Spotify and YouTube Playlists - Chapter 2: Setting Up Spotify
Automate Spotify playlists by setting up a Spotify account and preparing for API integration, crucial for music lovers and developers alike
Dev.to · Tawanda Nyahuye
Up next
✨ 12 Best AI Tools for Resume Writing in 2026 🔥 | Save Hours Every Week
Adewale Ogunsina
Watch →