GET 50% OFF GitKraken Pro! Ends Soon

Git Worktree

The Git worktree command allows you to checkout and work in multiple Git branches simultaneously.  Now, what situations might you utilize this action?

Imagine you’re in the middle of making numerous changes on a project with multiple new dependencies introduced with various WIP changes. What would happen if you suddenly have to work on a hotfix in another branch? For many people, the normal workflow would be to use Git stash to save your current work, checkout the hotfix branch, conclude that work, then re-checkout the branch you were originally on and pop your stash.  If that sounds inefficient, the makers of Git agree with you. This is where Git worktree comes in.  

Instead of that stash and checkout flow, you can simply add a new worktree entry and change directory into the needed branch. 

For example, imagine you’re working on a project inside the main branch, but need to test and approve changes applied in a feature branch. With Git worktree, you simply tell Git to checkout the feature branch in a different directory, outside your present working directory and change to that directory; from here, you can do your work and change back to the original directory to find all your work in progress awaiting you, just as you left it. 

Review of Git Branches

Before we take a look at how to use Git worktree in GitLens for VSCode and the command line, let’s first do a quick refresher on Git branch.

In Git, a branch is a pointer to one specific commit, while a commit is a snapshot of your repository at a specific point in time. Your branch pointer moves along with each new commit you make. 

By default, Git only tracks and moves that pointer along on one branch at a time. The branch you selected with Git checkout is considered your “working tree”.


Just like a tree in the forest, a Git worktree can have several branches at once, which is exactly what the Git worktree command allows you to do.

To keep things organized, Git worktree places each branch into a different specified folder in your file system.  To move between the multiple branches on the worktree, you simply change directory to work against the needed branch. 

Diagram illustrating Git worktrees. A single Git repository connects to three separate working directories: ../main with the Main branch, ../feature1 with the Feature-1 branch, and ../feature2 with the Feature-2 branch. Each worktree displays a visual representation of its branch history.

GitLens can help you stay organized and on track with your tasks and projects.  Unlock the full power of Git in VS Code for free!

Install GitLens for VS Code

Git Worktree Concepts

Git worktree is a powerful command with a number of options.  For our purposes, we will only be focused on working with branches, as that is the most common use case. We will be going through Git worktree examples with the following commands:

  • Git Worktree Add
  • Git Worktree List
  • Git Worktree Remove


See the official Git documentation for more information on other ways you can leverage Git worktree.

Git Worktree Add

A Git worktree will always point to a folder on your file system while referencing a particular commit object, like a branch. In order to work in multiple checked out branches at once, you need to add each branch as a new working tree to the Git worktree. There are 4 possible scenarios you can encounter when using Git worktree add:

  1. Add a new working tree to a directory that shares the same name as the branch (the most common method)
  2. Add a new working tree to a directory with a different name as the branch
  3. Create a new Git branch and add a new working tree to a directory that shares the same name as the branch
  4. Create a new branch and add a new working tree to a directory with a different name as the new branch

We will cover how to do each of these in the Git worktree examples later in this article. 

It is important to know that whatever path you choose, you can only ever have one working tree per directory. When you create the new working tree, the target directory must be empty, otherwise, the command will fail and you will get an error message.

Git Worktree List

In order to see what branches are currently active as working trees, you need to tell Git to list them with the Git worktree list command.  The main working tree is listed first, followed by each of the linked working trees.

The output details include the path of each working tree, the commit hash, and the name of the branch currently checked out in that entry.

Git Worktree Remove

In order to keep your Git worktree in order, you will need to delete entries from time to time.  No matter how the worktree entry was added, you will remove them the same way: by specifying the folder to be removed. And you don’t need to worry about specifying the branch name inside that folder.  

If there are uncommitted changes in a particular folder Git will not allow you to remove it by default. You can override this restriction by using the --force option. As with any use of -f, be careful and make sure you know what you’re doing before overriding Git’s safety precautions.  

GitLens Worktrees

Worktrees in GitLens for VS Code is a GitLens+ feature requiring a GitLens+ or GitLens+ Pro account. GitLens+ users can leverage Worktrees on public repositories, while GitLens+ Pro users can use Worktrees on all repositories, public and private.  You can read more about GitLens+ Features here.

Worktrees in GitLens can be viewed, by default, under the Source Control menu in the VS Code Sidebar. You can also open up the Worktrees menu by using the VS Code Command Palette, ⇧⌘P and typing GitLens: Show Worktrees view.

Worktrees is just one of the many powerful features you can unlock with GitLens+. What are you waiting for?


Sign up for GitLens+

Git Worktree Add with GitLens

In this Git worktree example, we’re going to review how to add a worktree entry with GitLens Worktrees, covering all 4 possible cases.

To start the process of adding a worktree entry with GitLens Worktrees, simply click the + icon in the Worktree menu. This will open the Command Palette and ask you to choose which existing commit-ish to use for the base of your new worktree entry.  

Visual Studio Code interface showing the creation of a new Git worktree using the GitLens extension. The left panel explains the purpose of worktrees and includes a “Create Worktree…” button. A modal window titled “Create Worktree for” is open, allowing the user to select a branch or tag (highlighting feature-worktree). A terminal panel at the bottom confirms the current branch context.

Commit-ish

A quick note about that strange term “commit-ish”. Commit-ish refers to Git identifiers that eventually point to a Git commit object, like a tag, a branch, or an individual commit. For example, when referring to the tip of the commit graph, you can refer to a specific commit by the full 40 character hash number, the truncated 7 character hash number, the tag name, or “HEAD”. All of these methods eventually point you to the same reference.
For our purposes, we will only use this term for branch names, as that is the most common use case. But remember: you can add individual commits even in a detached HEAD state. See the official Git documentation for more information.

Once you have selected your base branch (most common), tag, or commit, you will be prompted to select a folder that will be the base for the worktree entry.  Remember, each worktree entry needs to have its own empty folder to complete the process.

A file browser dialog in Visual Studio Code prompting the user to select a location for a new Git worktree. The VS Code interface shows the “Create Worktree…” button under the WORKTREES section on the left, while the file dialog displays folders inside the Documents directory. The bottom right corner includes a “Select Worktree Location” button.

Once you have selected the base folder in your local file system, you will be presented with 4 options in the Command Palette.

Visual Studio Code interface showing the “Confirm Create Worktree” dropdown menu. Multiple options are listed to create a worktree or create a new branch and worktree, either inside a designated folder or directly in a selected directory. The highlighted option reads “Create Worktree for branch demo-worktrees” and specifies the target path for the new worktree.

Let’s take a closer look at the 4 creation options you will be presented with inside the VS Code Command Palette drop down menu, which is shown in the image above when you are adding a new worktree entry.

How do you use Git worktree add for an existing branch using the same name as the working directory with GitLens?

Choosing the option: Create Worktree for branch <branch-name> will create the worktree entry in a folder with the same name as the branch. The full path to that branch will be inside a new folder, with a directory name taken from the original folder, appended with the term .worktrees

For example,  let’s say you want to create a worktree based on the branch dev. Let’s also say you are working in a directory called vscode-gitlens and have selected the next highest directory to base the new worktree entry’s folder, ... Your new worktree entry would be at ../vscode-gitlens.worktrees/dev relative to your present working directory. 

VS Code interface showing two active Git worktrees: feature-worktree and dev. The terminal displays the output of git worktree list, confirming both worktrees are linked to the same commit. On the left panel, the worktrees are listed under “WORKTREES” with recent commits shown for the dev branch.

How do you Git worktree add to create a new branch using the same name as the working directory with GitLens?

Choosing the second option from the Command Palette drop-down menu: Create a New Branch and Worktree from branch <branch-name> will present you with a new option from the Command Palette to name the new branch. The new branch’s first commit will be the latest commit on the branch you specified in the previous step. 

Prompt in VS Code for creating a new Git worktree from the branch demo-worktrees. The input field is prefilled with demo-worktrees-branch, and a message below instructs the user to press Enter to confirm or Escape to cancel.

Once you have named the new branch, GitLens will create the new worktree entry in a folder with the same name as the new branch. The full path to that branch will be inside a new folder, with a directory name taken from the original folder, appended with the term .worktrees.  

For example, let’s say you’re creating a worktree entry and want to create a new branch named demo-worktrees-branch based on the dev branch, which you had selected in a previous step. Let’s also say that while working in a directory called vscode-gitlens, you have selected the next highest directory to base the new worktree entry’s folder, ...  In this scenario, your new worktree entry and branch would be at ../vscode-gitlens.worktrees/demo-worktrees-branch relative to your present working directory.  

VS Code interface showing three active Git worktrees in the sidebar: feature-worktree, demo-worktrees-branch, and dev. The terminal displays the output of git worktree list, listing the file paths, commit hashes, and corresponding branch names for each worktree.

How do you use Git worktree add for an existing branch using a different name than the working directory with GitLens?

Up until now, you have named the target directory using the same name as the branch. While this is the most common, and least confusing way to name the directory, Git allows you to have a different name for the folder than the branch name. 

If you select the third option from the Command Palette drop-down menu: Create Worktree (directly in folder) for branch <branch-name>, you will create a new worktree entry based on your specified branch inside the target folder.

For example, let’s say you’re creating a worktree entry from the insiders branch and want to have it live in a directory called vscode-gitlens-demo-1. Let’s also assume you have selected the next highest directory to base the new worktree entry’s folder, ... Your new worktree entry would be at ../vscode-gitlens-demo-1 relative to your present working directory. 

Note: To use this option you must select an empty folder during the file section step, (step 2 in the process). If the folder is not empty you will see an error message the command will fail.   

GitLens Worktree showing the newly added insiders worktree entry and confirming using git worktree list

GitLens Worktrees will still show the name of the commit-ish in the worktree entry list, no matter what you name the folder, making it easy to keep track of which branch, tag, or commit-ish you have added.  

How to use Git worktree add to create a new branch using a different name than the working directory with GitLens?

Choosing the fourth option from the Command Palette drop-down menu: Create a New Branch and Worktree (directly in folder) from branch <branch-name> will present you with a new option from the Command Palette to name the new branch. The new branch’s first commit will be the latest commit on the branch you specified in the previous step. 

GitLens interface in VS Code displaying the “Create Worktree from dev” command palette. A text input field shows the new branch name being entered as feature-demo, with a prompt below instructing the user to press Enter to confirm or Escape to cancel.

For example, let’s say you’re creating a worktree entry, and you start by creating a new branch named ‘feature-demo’ that you want to place in a directory called gitlens-feature-demo. Let’s also say that you’re working in a directory called vscode-gitlens and have selected the next highest directory to base the new worktree entry’s folder, ... In this scenario, your new worktree entry would be at ../gitlens-feature-demo, relative to your present working directory.  

Note: To use this option, you must select an empty folder during the file section step, (step 2 in the process). If the folder is not empty, you will see an error message, and the command will fail.  

GitLens Worktrees view in VS Code showing five active worktrees, including a newly added feature-demo entry. The integrated terminal confirms the worktree’s presence using the git worktree list command, listing directories and associated branches such as feature-worktree, feature-demo, insiders, demo-worktrees-branch, and dev.

Git Worktree List with GitLens 

One of the best parts about the GitLens UI is that you never need to run a command to see the list of worktree entries, you just need to look at the Worktrees section of your Source Control menu.

GitLens Worktrees panel displaying four active worktrees: v8.3.1-gk-client-release-notes, staging, dev, and production. The dev branch is 137 commits ahead of its remote (origin/dev), and production is 154 commits ahead of origin/production. The current checked-out worktree is v8.3.1-gk-client-release-notes.

Make visualizing worktrees, and the rest of Git, easier by powering up to GitLens+ Features right now!

Sign up for GitLens+

Git Worktree Remove with GitLens

To remove a worktree entry with GitLens, just right mouse click, or alt-click, the worktree entry from the list and select the Delete Worktree… option.

GitLens Delete Worktree… option being selected from the alt-click menu

After you’ve selected the Delete Worktree… option, GitLens will open the Confirm Delete Worktree menu from the Command Palette. You can choose to Delete Worktree or Force Delete Worktree.  Force deleting a worktree will also delete any uncommitted changes. 

GitLens interface displaying the “Confirm Delete Worktree” menu, offering two options: “Delete Worktree” to safely remove the worktree at /Users/dwaynemcdaniel/Documents/vscode-gitlens-demo-1, and “Force Delete Worktree” to remove it including any uncommitted changes.

How to Use Git Worktree List with the Command Line 

If you’re using the Git CLI, you won’t have immediate visibility into your active Git worktree, so you will need to run the Git worktree list command to view a list of your worktree entries.

git worktree list

Terminal output of the git worktree list command showing a single worktree located at /Users/dwaynemcdaniel/Documents/dwayne-copy-support/support.gitkraken.com, pointing to commit c8e13872 on the branch v8.3.1-gk-client-release-notes.

The output details include: 

  • the path of each worktree entry
  • the commit hash for the branch checked out for the worktree entry
  • the name of the branch that is currently checked out for the worktree entry

How to Use Git Worktree Add with the Command Line

Just like adding a new worktree entry with GitLens, there are 4 possible scenarios to choose from when using Git worktree in the CLI.  Let’s walk through each Git worktree example.

How do you use Git worktree add for an existing branch using the same name as the working directory with the command line?

git worktree add path/to/folder/<existing-branch-name>

Terminal screenshot showing the use of the git worktree add ../examplefolder/dev command, which checks out the dev branch into the specified directory. The output confirms 100% file update completion and sets HEAD to commit 289180ad. A follow-up git worktree list command displays two worktrees: the main one on v8.3.1-gk-client-release-notes and the new one in examplefolder/dev on the dev branch.

How do you use Git worktree add for an existing branch using a different name than the working directory with the command line?

While this command looks very similar to the previous one, note the use of the space between the file path and the branch name.

git worktree add path/to/folder/ <existing-branch-name>

Terminal screenshot showing the use of git worktree add ../example-folder-insiders/ insiders to create a new worktree for the insiders branch. A git worktree list command confirms the addition, displaying three active worktrees: one on feature-worktree, one in example-folder/dev on dev, and the newly created example-folder-insiders on the insiders branch, all pointing to commit 7c8c5425.

How do you use Git worktree add to create a new branch using the same name as the working directory with the command line?

You can tell Git to create a new branch by using the -b flag. By default, it will create the new branch based on the branch you currently have checked out. 

git worktree add -b <new-branch-name> path/to/folder/<new-branch-name>

Terminal output showing the use of git worktree add -b new-feature-1 ../example-gitlens-worktree/new-feature-1 to create a new Git worktree for a newly created branch new-feature-1. The command prepares the worktree and checks out the branch. A git worktree list command confirms three active worktrees: feature-worktree, new-feature, and the newly created new-feature-1, all pointing to commit 7c8c5425.

You can also choose to base a new branch from another branch, tag or commit-ish, by specifying your choice at the end of the command.

git worktree add -b <new-branch-name> path/to/folder/<new-branch-name> <existing-branch-to-use-as-base>

Terminal showing git worktree add -b new-feature-2 ../example-gitlens-worktree1/new-feature-2 dev to create a new branch new-feature-2 from dev in a new worktree. The git worktree list command confirms the creation of four worktrees: feature-worktree, new-feature, new-feature-1, and new-feature-2, all pointing to commit 7c8c5425.

How to use Git worktree add to create a new branch using a different name than the working directory with the command line?

git worktree add -b <new-branch-name> path/to/folder/

How to Use Git Worktree Remove with the Command Line

To remove a Git worktree entry with the CLI, you will need to specify the folder you want to remove. If the branch name and the folder name are different, you don’t need to name the branch.

git worktree remove path/to/folder/

Terminal session showing the removal of a worktree using the command git worktree remove ../example-folder-new. The process begins with a git worktree list, displaying four worktrees. The user confirms the current directory with pwd, then removes the target worktree. A subsequent git worktree list confirms the worktree at ../example-folder-new has been removed, leaving three remaining worktrees.

If there are uncommitted changes in that working tree’s folder, the remove command will fail. To overcome this, you can use the force option: -f.  As with any use of --force, be careful and make sure you know what you’re doing before overriding Git’s safety precautions. 

Terminal session showing the forced removal of a worktree using the command git worktree remove -f ../example-gitlens-worktree1/new-feature-2. A git worktree list at the start displays three worktrees. After executing the remove command, a final git worktree list confirms that the new-feature-2 worktree has been successfully removed, leaving two remaining worktrees.

Ready for an easier way to manage Git Worktrees?

Your time is valuable, not to mention your mental energy. Why spend them trying to remember or having to manually track existing worktree entries? Why run the Git worktree list command constantly? Let GitLens for VS Code organize all of that for you, and present it in a beautiful and easy-to-follow UI.

Manage worktrees with ease and unleash the full power of Git in VS Code with GitLens!

Install GitLens for VS Code

Additional Resources

Table of Contents

Visual Studio Code is required to install GitLens.

Don’t have Visual Studio Code? Get it now.

Team Collaboration Services

Secure cloud-backed services that span across all products in the DevEx platform to keep your workflows connected across projects, repos, and team members
Launchpad – All your PRs, issues, & tasks in one spot to kick off a focused, unblocked day. Code Suggest – Real code suggestions anywhere in your project, as simple as in Google Docs. Cloud Patches – Speed up PR reviews by enabling early collaboration on work-in-progress. Workspaces – Group & sync repos to simplify multi-repo actions, & get new devs coding faster. DORA Insights – Data-driven code insights to track & improve development velocity. Security & Admin – Easily set up SSO, manage access, & streamline IdP integrations.
winget install gitkraken.cli