Git Worktrees Tutorial #1 - What are Git Worktrees?

The Net Ninja · Beginner ·🧠 Large Language Models ·3mo ago

Key Takeaways

The video demonstrates the use of Git worktrees, a feature that allows multiple working directories to share the same Git repository, and explains how to add a new worktree, check out a new branch, and work on multiple branches simultaneously without conflicts.

Full Transcript

All right then, my friends. In this series, we're going to be diving into a Git feature that's been around for a long time, Git Work trees, but they've only recently started gaining traction on YouTube thanks to a flurry of AI content. And from what I've seen of some of those videos, I feel like a lot of them don't really dig into the details of what Git work trees are and why they're useful outside of the scope of agentic coding. So, in this series, I want to dive a little bit deeper, show you a couple of different ways we can work with git work trees, and then at the end of the series, I'll make a video about why they're being used so much in aentic coding setups as well. And hopefully that gives you the context you need. So, let's start by talking about what work trees are and what problems they solve during a typical development workflow. Now, just very quickly before you start, I want to say that if you don't already know the basics of Git, then you should definitely start there. So, if you want to learn Git from the beginning, I've got a big Git and GitHub master class available, which I will leave a link to down below the video. I've also added this whole Git Workree series to the end of that Git masterass as well. So, if you're already enrolled, you can watch it there, too. Anyway, what is a Git Workree? Well, a work tree in the simplest sense is just a working directory. It's a folder on your computer that contains a checked out version of your project. So when we clone a GitHub repo onto a computer, Git brings down the repository itself and that's the dotgit folder that contains all the repo data like commits, branches, and the full project history. And at the same time, it also creates a default work tree, which is the project files that are checked out. Usually the latest commits on the default branch, the main branch. That's most of the time. So all of that checked out project code that you see after a clone that is a git work tree. Right? Now what happens if we're working on a new feature? Well, normally we would switch to a new feature branch and start working on that new feature. Right? So imagine we're doing that and then at some point for whatever reason we need to quickly switch to another new branch. Now it could be for a hot fix or another feature we need to implement or whatever else. But we've got a bunch of code changes on this current branch that aren't ready to be committed yet. So what would we do? Well, we would normally commit those changes anyway, but that would give us a messy commit history or we could stash those changes, but stashes are a little fragile themselves, easy to forget, and you can sometimes drop or lose track or pop the wrong one and so on. So the third option then is to add a new work tree for the project. And by using multiple work trees, we don't have to then switch branches inside the same working directory anymore. Instead, we can have multiple working directories or work trees all linked to the same repository. And while one work tree could have a feature branch checked out, another work tree could have a hot fix branch checked out at the same time. And that means when we have to switch focus, we don't need to start making all those messy commits or stashing the changes. We can just add a new work tree, check out a new branch inside that work tree at the same time, and work on whatever it might be that needed your focus. Now, when we're done, we can just head back to the default work tree and carry on with the original work that we were doing. So, then we know now that a work tree is just a working directory that contains a checked out version of a project, right? And each work tree is tied to its own branch which can include uncommitted changes. But each work tree still shares the same git history, the same commits, the same remotes, and so on. So, we're not cloning the repo multiple times. We're just kind of asking it to give us different views of the same project using the same instance of the repository. And the benefit of that over multiple clones is that we're not duplicating the history. We're not storing the repo several times on the computer. And also because we just have a single repo to manage a single fetch from any work tree updates everything. Whereas with multiple clones, you'd have to update every clone separately. And also one more thing, when you use work trees, git stops you from checking out the same branch twice within those different work trees. Whereas if you've got multiple clones, there's nothing stopping you from working on the same branch in each one of those and just potentially messing up your work. So then in this series, I'm going to be working with this repo right here, which I've already added to GitHub. And I'll leave a link to this repo down below the video as well, so you can access it. But it's nothing special. It's literally just got a few HTML files inside it, CSS file and a reb. Now, I've kept this simple because I can more easily then demonstrate how work trees work. And I'm sure you're going to be working on much bigger, much more complex projects. But everything you learn using this one is essentially going to carry over to whatever project you might be working on. So then the first thing we need to do is clone this repo to our computer so we can work on it locally. And we can do that using one of these options right here. I'm just going to use HTTPS by copying this URL. Also, I'm going to be using a terminal called Warp in this series, which you can get for free at warp.dev. And the reason I'm using this terminal is because it's going to let me do two things. First, it comes with a built-in editor, so I can easily make simple code changes myself and view those changes. And second, because it also comes in with some built-in AI tools, which is going to make it easier for me to demonstrate if I need to work trees within an agentic workflow. I might use Claude, I don't know. Now, you don't have to use this terminal. You can choose whichever one you prefer. And this is not an endorsement of Walt by any means. I'm just choosing to use it for this course. So then I've opened up a new terminal and I've already cded into a project directory which you can see right here. And now I want to clone that repository by typing git clone followed by the URL I just copied and then hit enter. Now when I do that it's going to pull down that repo into a new folder called portfolio workre which we should be able to see if we run the ls command. So then let's CD into that new directory now by typing cd portfolio work trees and hitting enter. And when we do that you can see right here we automatically have the main branch checked out. Now when we run the ls command again we're going to see all the project files that git brought down for us right and these files in this folder represent the default work tree that git creates when we clone a repo. And remember a work tree is just a working directory that contains a checked out version of a project which is this. Right now if we run the ls command again this time with the a flag to see all the hidden files then we're going to see the do.getit folder and this is the repo itself where all the commit history branches refs and logs lives. So we have two things right like we said before the default work tree which is the checked out version of the project or the main branch and the git repository itself the do.git folder. Okay, so now let's imagine we want to start working on a new feature. Well, the first thing we normally do is switch to a new branch. So let's do that by typing git switch followed by the C flag to create a new branch. And we'll call this feature hyphen A. And then what we do is just start working on this feature. So I'm just going to list the contents of this project again so that I can click on one of the HTML files. And when I do that, Warp is going to let me open that file up by clicking on the open in Warp link. And then I'm just going to make any old change for now. Doesn't matter what. I'm going to save the file and then close the file, which you can do by hitting command plus W. So now we've got some code changes on this branch, but we've not finished the feature or even reached any kind of milestone, right? But then for whatever reason, we might need to switch branches to quickly work on something else like a different feature or a hot fix. So at this point, we'd either have to stash these changes now or make a commit, which we really don't want to do just yet. and then we could switch to another branch to work on that hot fix or whatever else it was. Or the other option is to add a new work tree to this repo which lets us work on a new branch in a completely separate working directory without having to stash or commit anything here. So we can leave the changes untouched. So, we're going to see how to do that in the next lesson.

Original Description

In this series, you’ll learn how to use Git worktrees, a feature that lets you check out multiple branches at the same time in separate working directories. 🍿👇 Get the FULL Git & GitHub Masterclass: https://netninja.dev/p/git-github-masterclass 🍿👇 Get the FULL Claude Code Masterclass: https://netninja.dev/p/claude-code-masterclass 🔥👇 Get access to ALL Masterclasss & premium courses with Net Ninja Pro: https://netninja.dev/p/net-ninja-pro/#prosignup Starter Project: https://github.com/iamshaunjp/portfolio-worktrees
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Net Ninja · Net Ninja · 0 of 60

← Previous Next →
1 Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
2 Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
3 Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
4 GraphQL Tutorial #1 - Introduction to GraphQL
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
5 GraphQL Tutorial #2 - A Birdseye View of GraphQL
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
6 GraphQL Tutorial #3 - Project (stack) Overview
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
7 GraphQL Tutorial #4 - Making Queries (front-end preview)
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
8 GraphQL Tutorial #5 - Express App Setup
GraphQL Tutorial #5 - Express App Setup
Net Ninja
9 GraphQL Tutorial #6 - Setting up GraphQL
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
10 GraphQL Tutorial #7 - GraphQL Schema
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
11 GraphQL Tutorial #8 - Root Query
GraphQL Tutorial #8 - Root Query
Net Ninja
12 GraphQL Tutorial #9 - The Resolve Function
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
13 GraphQL Tutorial #10 - Testing Queries in Graphiql
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
14 GraphQL Tutorial #11 - GraphQL ID Type
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
15 GraphQL Tutorial #12 - Author Type
GraphQL Tutorial #12 - Author Type
Net Ninja
16 GraphQL Tutorial #13 - Type Relations
GraphQL Tutorial #13 - Type Relations
Net Ninja
17 GraphQL Tutorial #14 - GraphQL Lists
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
18 GraphQL Tutorial #15 - More on Root Queries
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
19 GraphQL Tutorial #16 - Connecting to mLab
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
20 GraphQL Tutorial #17 - Mongoose Models
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
21 GraphQL Tutorial #18 - Mutations
GraphQL Tutorial #18 - Mutations
Net Ninja
22 GraphQL Tutorial #19 - More on Mutations
GraphQL Tutorial #19 - More on Mutations
Net Ninja
23 GraphQL Tutorial #20 - Updating the Resolve Functions
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
24 GraphQL Tutorial #21 - GraphQL NonNull
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
25 GraphQL Tutorial #22 - Adding a Front-end
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
26 GraphQL Tutorial #23 - Create React App
GraphQL Tutorial #23 - Create React App
Net Ninja
27 GraphQL Tutorial #24 - Book List Component
GraphQL Tutorial #24 - Book List Component
Net Ninja
28 GraphQL Tutorial #25 - Apollo Client Setup
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
29 GraphQL Tutorial #26 - Making Queries from React
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
30 GraphQL Tutorial #27 - Rendering Data in a Component
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
31 GraphQL Tutorial #28 - Add Book Component
GraphQL Tutorial #28 - Add Book Component
Net Ninja
32 GraphQL Tutorial #29 - External Query File
GraphQL Tutorial #29 - External Query File
Net Ninja
33 GraphQL Tutorial #30 - Updating Component State
GraphQL Tutorial #30 - Updating Component State
Net Ninja
34 GraphQL Tutorial #31 - Composing Queries
GraphQL Tutorial #31 - Composing Queries
Net Ninja
35 GraphQL Tutorial #32 - query variables
GraphQL Tutorial #32 - query variables
Net Ninja
36 GraphQL Tutorial #33 - Re-fetching Queries
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
37 GraphQL Tutorial #34 - Book Details Component
GraphQL Tutorial #34 - Book Details Component
Net Ninja
38 GraphQL Tutorial #36 - Styling the App
GraphQL Tutorial #36 - Styling the App
Net Ninja
39 GraphQL Tutorial #35 - Making a Single Query
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
40 Build Apps with Vue & Firebase - Udemy Course
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
41 Updated Vue & Firebase Course (Udemy)
Updated Vue & Firebase Course (Udemy)
Net Ninja
42 Vue & Firebase Real-time Chat (Preview) #1 - Intro
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
43 Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
44 Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
45 Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
46 Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
47 Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
48 Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
49 Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
50 Object Oriented JavaScript Tutorial #1 - Introduction
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
51 Object Oriented JavaScript Tutorial #2 - Object Literals
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
52 Object Oriented JavaScript Tutorial #3 - Updating Properties
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
53 Object Oriented JavaScript Tutorial #4 - Classes
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
54 Object Oriented JavaScript Tutorial #5  - Class Constructors
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
55 Object Oriented JavaScript Tutorial #6 - Class Methods
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
56 Object Oriented JavaScript Tutorial #7 - Method Chaining
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
57 Object Oriented JavaScript Tutorial #8 - Class Inheritance
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
58 Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
59 Object Oriented JavaScript Tutorial #10 - Prototype
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
60 Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja

This video teaches how to use Git worktrees to manage multiple branches and working directories, allowing for more efficient and organized development workflows. By using Git worktrees, developers can work on multiple features or fixes without conflicts or messy commits.

Key Takeaways
  1. Add a new worktree
  2. Check out a new branch inside that worktree
  3. Clone a repository to a local computer using Git and HTTPS
  4. Create a new terminal and navigate to a project directory
  5. Use Git to clone a repository into a new folder
  6. Navigate to the new folder and check out the main branch
  7. Use the ls command to view the project files and the .git folder
  8. Switch to a new branch using `git switch`
  9. Create a new branch using `git switch -c`
  10. Make changes to a file
💡 Git worktrees provide a way to work on multiple branches simultaneously without conflicts, allowing developers to work on new features or fixes without affecting the main branch.

Related AI Lessons

Your LLM Doesn’t Pick Stocks — It Remembers Them
Discover how LLMs remember stock picks rather than making actual predictions, and why this matters for AI-driven investment strategies
Medium · Machine Learning
Embeddings Simplified
Learn the basics of embeddings and how they simplify complex data, a crucial concept in AI and ML
Medium · RAG
I built a tool that cuts Claude/ChatGPT token usage by 97% — here's how it works
Learn how to build a tool that reduces Claude/ChatGPT token usage by 97%, increasing productivity and efficiency in debugging and development
Dev.to · Rohith Matam
Serverless AI in a Browser Tab: Java WebAssembly + Local WebGPU LLMs
Learn to build a serverless AI model in a browser tab using Java WebAssembly and Local WebGPU LLMs for a zero-infrastructure RAG architecture
Dev.to · vishalmysore
Up next
5 Levels of AI Agents - From Simple LLM Calls to Multi-Agent Systems
Dave Ebbelaar (LLM Eng)
Watch →