Git Worktrees - czym są i jak uprościć sobie dzięki nim pracę (film, 8 minut)
ThePrimeagen w swoim najnowszym filmie omawia potężne możliwości, jakie oferują worktree w Gicie. Podkreśla, jak często programiści napotykają na potrzebę zmiany kontekstu pracy, na przykład, gdy muszą szybko zająć się problemem na produkcji. W takich sytuacjach często trzeba tymczasowo zapisać zmiany w wersji roboczej, co może prowadzić do chaosu i nieporozumień. Paradoksalnie, wiele osób nie wie, jak można uprościć ten proces dzięki worktree. ThePrimeagen wyjaśnia, że worktrees pozwalają na jednoczesne posiadanie wielu commitów aktywnych w różnych folderach, co znacząco ułatwia przełączanie się między różnymi zadaniami. To rozwiązanie znacznie zmniejsza konieczność ręcznego zarządzania zmianami i ogranicza czas stracony na zrozumienie, co zostało zrobione i co trzeba zrobić.
Podczas filmu ThePrimeagen pokazuje również, jak korzystać z poleceń Git, aby stworzyć worktrees. Demonstruje, jak dodać różne commity i zarządzać nimi za pomocą prostych komend. Zauważa, że kluczem do efektywnej pracy jest umiejętność wykorzystania worktrees, co sprawia, że programiści mogą łatwiej dostosować się do szybko zmieniających się wymagań. Dzięki worktrees można np. mieć oddzielne foldery dla głównej gałęzi projektu, nowych cech oraz poprawek, co pozwala uniknąć zamieszania i chaotycznych commitów.
ThePrimeagen dzieli się również swoim doświadczeniem z pisania pluginów, aby zautomatyzować pracę z worktrees. Znalazł sposób na połączenie ich z narzędziem telescope, co dodatkowo podnosi komfort pracy. Wskazuje, że jego plugin ułatwia zarządzanie gałęziami oraz aktualizację statusów, co zdecydowanie przyspiesza proces kodowania. W tym kontekście warto zwrócić uwagę na fakt, że zintegrowane narzędzia mogą znacznie poprawić wydajność pracy zespołów programistycznych.
Na koniec, warto zwrócić uwagę na statystyki filmu. W momencie pisania tego artykułu, materiał ThePrimeagena miał 300095 wyświetleń oraz 9611 polubień. Wskazuje to na rosnące zainteresowanie tematem worktrees w Gicie oraz efektywnością, jaką mogą przynieść w codziennej pracy programisty. Tematyka pracy z Gitem wciąż cieszy się dużym zainteresowaniem, a innowacyjne podejście ThePrimeagena może przyczynić się do jeszcze lepszego wykorzystania tego narzędzia w praktyce.
Toggle timeline summary
-
Wprowadzenie do Git Worktrees i ich zalety.
-
Wyjaśnienie potrzeby zmiany kierunku w projektach i niedogodności tradycyjnych przepływów pracy Git.
-
Opis, jak Worktrees pozwalają na jednoczesne sprawdzanie wielu commitów.
-
Podkreślenie długo ignorowanej funkcjonalności Git Worktrees.
-
Demonstracja Git Worktrees z użyciem komendy klonowania.
-
Wprowadzenie słowa kluczowego 'bear' w użyciu Git Worktree.
-
Dodawanie wielu worktrees dla różnych commitów.
-
Usuwanie worktree i zarządzanie zmianami.
-
Osobiste doświadczenia i zmiana w kierunku korzystania z Git Worktrees.
-
Tworzenie wtyczki ułatwiającej operacje Git Worktree.
-
Używanie stworzonej wtyczki do tworzenia gałęzi bezproblemowo.
-
Integracja wtyczek do efektywnego zarządzania operacjami worktree.
-
Automatyzacja aktualizacji submodułów w związku ze zmianami w worktrees.
-
Podsumowanie, zachęcające do korzystania z Git Worktrees w celu poprawy przepływów pracy.
-
Ostatnie przemyślenia na temat integracji wtyczek i doświadczeń użytkowników.
Transcription
So Git Worktrees are incredible. If you haven't used them, oh my goodness, they fix so many things that are wrong with my current Git workflow. So before I tell you what Git Worktrees are, I thought it would be better if we talk about why you should use Git Worktrees first. Have you ever checked out a project, started working on, and then need to pivot, right? You either have a message from a coworker, 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. 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 coworkers are like, WTF, what is this squash message you got here? This is not even that good. And you have to do this, and you have to check out something else, and you have to do some work, and then you have to uncheck out, and go back, and then unstash, or look at your commit, and try to remember what you're doing. It's just not that great, right? It's just not a fun experience. So Worktrees 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 have menus to get that long, because this is a 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 Worktrees? 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 git work tree right away. And let's do a git clone bear now bears the keyword you need to use here and then go get to work tree dot n vim. There we go. So now we're going to 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 get work tree. 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 git 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 as you get work tree add foo bar baths. 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 read me, I have master, and I have foo bar baths. If I were to go into foo bar baths, it would be foo bar baths 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 completed like that. Let's just remove foo bar baths. 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 foo bar baths. And force there we go. If we relook at our list of items, you'll notice that foo bar baths is gone. So when I saw this, I immediately just I just fell in love. I fell in love. I knew for that moment that get work trees were going to be in my future thoroughly. I've been in the process of changing over all of my work projects into bear 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 seems so arcane and distant. So at this point, I didn't want to have to write all these commands CD around open and close them do all that. So I wrote a quick plugin called get work tree 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 accidentally edit the wrong code. And we created a telescope plugin to make work tree operations easier. Alright, 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 to 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? Lua, 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 work tree operations using telescope to help guide me on top of that I can tell where I'm at because of the status line. And lastly, those operations are provided by the get work tree plugin. So at work, I have a large c++ 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 510 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 work tree 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 tree to tap into changes. Now notice that I can just simply do on tree 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'm going to go and execute get sub module update in that repositories. 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 had no idea that OSHA had rules even for this. Hey, thank you for watching. Please check out work trees, they will make your life 1000 times better because you still get all the standard usages of get 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 plugin that I showed you to 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 1000s of people at this point. My name is the primes and thank you for watching.