Git Worktrees and Neovim: A Match Made in Coding Heaven

Поделиться
HTML-код
  • Опубликовано: 17 авг 2023
  • Git Worktree is somewhat of a niche feature in Git that too few developers know about. It allows you to change branches and contexts without ever having to stash your changes or commit and reset.
    However, it can be quite the hassle when not maintained well and having to work with the CLI.
    In this video I'll go through the best practices including a sweet Neovim plugin that makes the experience silky smooth!
    LINKS AND TOOLS MENTIONED
    ▶ github.com/ThePrimeagen/git-w...
    ▶ git-scm.com/docs/git-worktree
    ▶ github.com/nvim-telescope/tel...
    MY FAVOURITE GEAR
    * If you enjoy my content, using the links below will help me with a small commission, so thank you!
    ⌨️ My keyboard is the wonderful HHKB: amzn.to/3HwJG60
    🔗 The HHKB is wired with a high quality coiled cable: amzn.to/3DCA87d
    ⌨️ Backup keyboard (for traveling mainly) - Nuphy Air60: amzn.to/3R3M7jz
    🖥️ My ultra wide 49" monitor: amzn.to/3JbVThC
    🎙️ Microphone - Shure MV7X: amzn.to/3kA7tZZ
    🦾 Boom Arm - amzn.to/3ZmQgln
    🎤 Audio Interface (for XLR microphones) - Focusrite Scarlett Solo: amzn.to/3Z3fBkQ
    🦾 Heavy Duty Monitor Arm: amzn.to/3kKp7tS
    🖱️ Mouse - MX Master 3: amzn.to/3DiZmaj
    📷 Camera - ZVE10: amzn.to/3I1jfVS
    🎥 Lens - Sigma 1.4 16mm: amzn.to/3BfFN15
    CONSIDER SUPPORTING THIS CHANNEL
    ☕ Buy me a coffee: www.buymeacoffee.com/omerxx
    🎷 60 day free Epidemic Music!: share.epidemicsound.com/w6zfxo
    MY KEY LINKS
    🍿 Subscribe: www.youtube.com/@devopstoolbo...
    🌍 My website: omerxx.com
    🗞️ Dev & Ops tools Telegram channel: t.me/espressops
    🎙️ DevOps Topeaks Podcast: topeaks.buzzsprout.com
    #neovim #productivity #git #devops #ops #dev #terminal #CLI #system #sysadmin #cloud #ricing #linux #macos #commandline #typing #keyboard #tutorial #beginner
  • НаукаНаука

Комментарии • 75

  • @pwnwriter
    @pwnwriter 9 месяцев назад +17

    DevOps toolbox: a place where you learn to cook code and write coffee : )

  • @niksingh710
    @niksingh710 9 месяцев назад +5

    This is so subtle that not knowing it gets the job done but knowing this makes it so fun and easy on head to do the job.
    Thanks buddy got to learn something new.

  • @cantucodes
    @cantucodes 9 месяцев назад +8

    Amazing video! I always appreciate the quality and clarity of how you explain things! I'm definitely going to give this a shot.

    • @devopstoolbox
      @devopstoolbox  9 месяцев назад +1

      Thank you my man! Thanks for the feedback it means a lot!

  • @ascourter
    @ascourter 8 месяцев назад +1

    Great overview. Been meaning to try changing my workflow to use Git worktrees and this helps me understand it a lot better. Thanks!

  • @elythh
    @elythh 9 месяцев назад +1

    Your videos are always so nice ! Thank you once again

  • @ravehlb
    @ravehlb 6 месяцев назад

    Thank you so much for this! So useful.
    I always found it pretty strange how git managed the issue of storing modified files by stashing. Didn't know about worktree until now, and it does seem like a much saner way to handle it.

  • @ilyeshammadi7278
    @ilyeshammadi7278 6 месяцев назад +1

    Thanks for the video. For me what has worked best for me till now is the combination of tmux and git worktree.

    • @devopstoolbox
      @devopstoolbox  6 месяцев назад

      Yep, I know worktree is loved by many. To me, this is still going out of the IDE (even if to a floating pane) and feels like using any external software, where as fugitive is still at the tip of my fingers

  • @JeremyMoore1
    @JeremyMoore1 9 месяцев назад

    This is awesome! Thanks for sharing

  •  7 месяцев назад

    Amazing content as always. Please consider putting your coffee gear in video description too ☕

  • @eclbg
    @eclbg 8 месяцев назад +1

    Thanks so much for this video! I will definitely try to incorporate this into my config. The workflow looks really promising.
    I'll also be that guy: what recipe are you using for the coffee? I struggle to make nice-tasting small cups, and too much caffeine can ruin my day.

    • @devopstoolbox
      @devopstoolbox  8 месяцев назад +2

      Hi! Of course! I’m using the James Hoffman one cup V60 method:
      15gr beans to 250 water:
      Pour off the boil water and wait 45s, add 50ml, on 1:10 add 50ml, at 1:30 add 50ml, at 1:50 add last 50ml.
      Pour gently and in round motions. It’s a lot about the distribution and even-ness of pouring.
      And the taste has a lot to do with water temperature and of course the right grind size! Good luck!

  • @typecraft_dev
    @typecraft_dev 9 месяцев назад +2

    damn that some good looking coffee. Oh and git worktrees seem cool too!

  • @qwarlock4126
    @qwarlock4126 6 месяцев назад

    Your coffee routine is mesmerizing. heh heh now I want coffee!

  • @Guilherme-qk9so
    @Guilherme-qk9so 9 месяцев назад +1

    That's awesome! I have been using worktrees for a year or so now too, it's a game changer.
    I found that using the bare repo is not very nice though. I remember having to manually change refs and whatnot, so these days what I do is:
    git checkout $(git commit-tree $(git hash-object -t tree /dev/null) < /dev/null)
    This will create a new empty root commit and checks it out. So what you'll have is just a .git folder, and it works like you'd expect a regular non-bare repository. Plus you don't get to see all of those bare repository files :) I then do as you did in the video, create the new worktrees under this folder.

    • @devopstoolbox
      @devopstoolbox  9 месяцев назад +1

      Firstly- very cool solution!
      But, what is it exactly that makes you change refs, or why? I’ve been doing that for a month or so and still haven’t had any issues. Maybe I haven’t encountered enough edge cases 🤷
      The bare repo only serves as a parent dir, each tree holds its .git and I find it works like an entirely different clone

    • @Guilherme-qk9so
      @Guilherme-qk9so 9 месяцев назад +3

      @@devopstoolbox I don't recall what the problem was exactly, it was a very long time ago. I got it when trying to either pull or push from a remote.
      I remember researching and finding that, since a bare repo is not supposed to be doing one of those, you needed to run update the remote refs to point to the correct place, or something like that.

    • @S7hadow
      @S7hadow 7 месяцев назад +1

      I’m also using bare repos with the .git folder. To achieve that while cloning you can do ‘git clone -bare /.git’. The small downside is that you have to manually set the folder name, but then you have a clean directory for your work trees

    • @devopstoolbox
      @devopstoolbox  7 месяцев назад

      @@S7hadow cool!

  • @sarojregmi200
    @sarojregmi200 5 месяцев назад

    Thank you sir I learned something new today.

  • @HaroldHersey
    @HaroldHersey 9 месяцев назад

    Love your keyboard.

    • @devopstoolbox
      @devopstoolbox  9 месяцев назад

      Thank you! This is the HHKB. It’s been with me for 4 years and I swear it I proves with time 😃

  • @Borgoo
    @Borgoo 8 месяцев назад

    Great video! What are the plugins for having the commandline/search in a floating window?

    • @devopstoolbox
      @devopstoolbox  8 месяцев назад +1

      Thanks! Checkout nvim “Noice”!

  • @yanisfourel
    @yanisfourel 2 месяца назад +1

    The only issue is when there are important gitignored files like a .env that I want to share across all branches. With that approach, I'd either have to copy it over everywhere or symlink it, both option add complexity

  • @a_maxed_out_handle_of_30_chars
    @a_maxed_out_handle_of_30_chars 9 месяцев назад +1

    ahh, nice and thank you :)

  • @user-qd3jm9vp4y
    @user-qd3jm9vp4y 2 месяца назад +1

    what if i want to run each worktree's should i do npm install on each worktree's or can i create a common file

    • @devopstoolbox
      @devopstoolbox  2 месяца назад

      Good question. Like other things it depends because it would make sense to use the same ones for every branch.
      That said, I’d symlink to a central one and run npm i again to make sure I’ve got the right ones

  • @TayTayChan
    @TayTayChan 6 месяцев назад +1

    Hey, I saw prime's workflow on this too, but decided not to use it because I sometimes need to work in node projects. Each directory needs to have the node_modules folder and always reinstalling dependencies is slow and takes up a lot of space. Have you encountered similar issues in your workflow? If so, how do you cope with it?

    • @devopstoolbox
      @devopstoolbox  6 месяцев назад +1

      Yep! Should be rather easy holding the shared dir on the bare repo and then symlinking all the child branches to ../node_modules. Id even add a script / keybinding to your tooling, depends on what you use to manage the trees (Neovim, CLI / other)

    • @TayTayChan
      @TayTayChan 6 месяцев назад +1

      ​@@devopstoolbox
      I did think of that, but admittedly I was too lazy/busy to try it out 😅
      I'll give it a serious go this time, because you did complete the picture of the worktrees workflow

  • @canchengwu7477
    @canchengwu7477 9 месяцев назад

    Amazing! Do you use kitty terminal ?

  • @user-xd5gd4pc9h
    @user-xd5gd4pc9h 9 месяцев назад +1

    It is amazing! I love your videos. I have two problems to ask for help. 1. How to fetch and update the new branch from the remote, I found it is difficult for me to get the newer updates from a different branch. 2. How would you diff the same file in different branches using this kind of setup?

    • @devopstoolbox
      @devopstoolbox  9 месяцев назад

      Thanks for the kind words :)
      To your questions :
      1. Not sure what you mean by that - are you asking how to integrate changes from another branch to your current one? As in - `git merge` ? Or just some commits by `git cherry-pick` ? Something along those lines?
      BTW, 99% of my git commands are done using fugitive (I have a video about that as well), the rest I run either directly from command line or command line through neovim (`:!git something`)
      2. This video is about worktrees, for diffs I use `gvdiffsplit `. Another tool you might wanna check out is github.com/so-fancy/diff-so-fancy. It's not my cup of tea but many people swear by it!

    • @user-xd5gd4pc9h
      @user-xd5gd4pc9h 9 месяцев назад

      @@devopstoolbox sorry for the fusion I made. I try to make it more clear. 1. I used similar tools and setups. I clone the repo barely and then I add a new worktree. At this time, if the remote repo had a update on different branch or new branch, how to get them in the current worktree (or branch). 2. Since you open the same file in different branches (directories) in one neovim instance, how to diff them more easily? gvdiffsplit will work on this setup? I don't know how to achieve that using fugitive in neovim. Thanks a lot.

    • @devopstoolbox
      @devopstoolbox  9 месяцев назад

      @@user-xd5gd4pc9h Hey!
      So, re 1. just go to the worktree you're using, in which case you'll be running in a .git repo where you can then pull changes!
      2. I think regardless of the structure being worktrees, you can keep working with gdiffsplit because at the end of the day these are git branches :) even though they are structured differently locally.
      Please to tell me if this helps! If not we'll get to the bottom of it

  • @iceke18
    @iceke18 6 месяцев назад +1

    How do you open 2 different branches side by side, if I try this, it always takes the current buffer's place. I tried '' as this is telescopes option to vertical split the file.
    Edit: I created the split first and could then open the other branch with telescope, is this the way?

    • @devopstoolbox
      @devopstoolbox  6 месяцев назад +1

      Yes, this is the straightforward way, unless you’d like to create it directly which you can map if you really insist

  • @5ihdi
    @5ihdi 6 месяцев назад

    Which plugin you use to have this 3:27 command line mode as floating window?

  •  7 месяцев назад +1

    Does Intellij IDEA (or any similar Java IDE) work with this concept of git worktrees? Working in a JVM project the "normal way" requires a lot of complex class loading stuff. Imagine shifting to the git worktree model. That might be a nightmare for IDE developers to implement right.

    • @devopstoolbox
      @devopstoolbox  7 месяцев назад +1

      Hmmm I haven’t been working with anything that isn’t Vim for over 6 years so I can’t really say… sorry

  • @pali122
    @pali122 9 месяцев назад +1

    I have been using it too, but is not really that different from clonning a repo twice and having two different active branches, at the end is mostly the same thing just with git commands

    • @devopstoolbox
      @devopstoolbox  9 месяцев назад +1

      Yeah I think I even mentioned it at some point in the video. However, it’s not quite the same in the way you organize your code and the ability to switch between them. Moving up and down for tree is not the same as working on multiple running got branches at the same time!
      And, if you have more than 1 repo, like 10+ it quickly becomes unmanageable. So I stopping working with more clones and just stashed my code constantly. So happy I don’t have to anymore 😃

  • @jay_morelli
    @jay_morelli 9 месяцев назад

    The meme of prime and lex 💚

  • @thecodealchemist7095
    @thecodealchemist7095 9 месяцев назад +1

    I felt i just learnt how to make myself proper coffee

  • @tamascsontos3324
    @tamascsontos3324 9 месяцев назад

    Why do you do cx instead of cd? What is that tool?

    • @devopstoolbox
      @devopstoolbox  9 месяцев назад +1

      Good catch!
      I use a tool called exa replacing “ls” for me (colorful output, built in tree and more - you can see it in my terminal setup video), so I have “cx” mapped to cd && exa with a few flags so that I cd but also get a list of files with the info I want (got status etc) you can checkout my dotfiles to see. If you don’t find it LMK I’ll send the exact alias

  • @cpplain
    @cpplain 7 месяцев назад

    I like using worktrees but not with bare repos. My solution is to create a worktrees directory in my project directory (../ from repo), add a sym link to the worktrees directory in the repo), and git ignore the link. Not perfect, though I find this better than dealing with bare repo caveats (e.g., working with remotes).

    • @devopstoolbox
      @devopstoolbox  7 месяцев назад +1

      Interesting! I still prefer the bare option although I understand the motive.

  • @Danielo515
    @Danielo515 9 месяцев назад

    Sorry, why you changed branches just to stash Changes?

    • @devopstoolbox
      @devopstoolbox  9 месяцев назад +1

      No! They replace each other 😉
      I started the video showing what I *used to do* - stashing and checking out another branch. Once you work with gittrees it is completely unnecessary!

  • @nasang7478
    @nasang7478 8 месяцев назад

    Just found your channel, very awesome, instantly subscribed 🫡

  • @Ibrahimdevelopment
    @Ibrahimdevelopment 2 месяца назад

    Does git fugitive work when withing a worktree in a --bare repo?
    I can use the interface to stage and commit, but once i have committed it dissapears and i have to use my cli to push to where i want.
    @devopstoolbox

    • @devopstoolbox
      @devopstoolbox  2 месяца назад

      Yes of course! I’m using it all the time.
      How did you switch trees? Is there a chance you’ve actually started nvim from the bare repo path and then switched tree with a plugin? Maybe fugitive then doesn’t see the tree but the bare source

    • @Ibrahimdevelopment
      @Ibrahimdevelopment 2 месяца назад

      @@devopstoolbox what i did: clone a bare repo. then git tree add "lastmod" .
      Then i cd into lastmod, open nvim and change a file
      Fugitive shows unstaged and staged stuff, and lets me hit cc and make a commit.
      once i have hit commit it just vanishes.
      if i run git log from there, i can see the commit and that head is at the lates spot, it's just that it's not letting me push from the interface. Not entirely sure what's going on there.

    • @Ibrahimdevelopment
      @Ibrahimdevelopment 2 месяца назад +1

      @@devopstoolbox usually on other repos it always displays head and merge: origin/branch

    • @devopstoolbox
      @devopstoolbox  2 месяца назад +1

      @@Ibrahimdevelopment interesting. Really not sure what’s going on

    • @Ibrahimdevelopment
      @Ibrahimdevelopment 2 месяца назад

      @@devopstoolbox did the troubleshooting and i figured that apparently 'If the upstream is not setup correctly when trying to pull or push, make sure the following command returns what is shown below. This seems to happen with the gitHub cli.'
      This for some reason seems to fix it