git worktree add to create a linked folder for the branch you need. Your original folder remains on its current feature branch, while the new folder can hold a bug-fix branch and open independently in Visual Studio Code—no stashing, committing unfinished work, or repeatedly switching branches required.
A worktree is not a second clone. Git creates another working folder connected to the same repository, with its own checked-out files, HEAD, and index while sharing the repository’s common Git data. That makes worktrees efficient, but it also means the same local branch should normally be checked out in only one worktree at a time.
Prepare the repository and choose a folder layout
Before creating a worktree, confirm that Git for Windows is available and that the folder you are using is an existing, non-bare repository.
- Open PowerShell.
- Move to the repository’s current folder. This example assumes your feature work is already open in a folder named
inventory-app:
Set-Location C:\Projects\inventory-app - Confirm Git is available:
git --version
You should see a Git version number. If PowerShell reports thatgitis not recognized, install Git for Windows or correct itsPathenvironment-variable entry before continuing. - Confirm that the current folder is a Git working tree:
git status
Expected result: Git reports the current branch and working-tree state. If it reports that this is not a Git repository, useSet-Locationto enter the repository root first. - Check the branch currently used by the original folder:
git branch --show-current
For example, it might return:
feature/new-checkout-flow
Leave this worktree alone. Any uncommitted files in it remain exactly where they are. - Decide where the parallel folder will live. A sibling folder is usually easiest to recognize:
Do not place the new worktree inside the current repository folder. Use a sibling folder, as shown above, or another separate location.Code:C:\Projects\inventory-app C:\Projects\inventory-app-hotfix
Check remote branches before creating a hotfix worktree
If the bug-fix branch already exists on your remote, refresh your remote-tracking references first:
git fetch origin
To inspect local and remote branches:
git branch --all
Look for entries such as:
main
* feature/new-checkout-flow
remotes/origin/hotfix/login-timeout
The asterisk identifies the branch checked out in the current worktree.
Warning: Do not usegit worktree add --forcejust to work around an error. Git normally refuses to place one local branch in more than one worktree because two folders modifying the same branch can create confusing or unsafe results. Create a separate branch instead, or remove the old worktree correctly.
Create a parallel folder for an existing local branch
Use this method when the branch you need already exists locally and is not currently checked out in another worktree.
For example, to check out the existing local branch hotfix/login-timeout into C:\Projects\inventory-app-hotfix, run this from the original repository folder:
git worktree add C:\Projects\inventory-app-hotfix hotfix/login-timeout
Git creates the folder, checks out the specified branch, and registers it as a linked worktree.
Then enter the new folder:
Set-Location C:\Projects\inventory-app-hotfix
Confirm that it is on the intended branch:
git status
git branch --show-current
Expected result:
On branch hotfix/login-timeout
Your original C:\Projects\inventory-app folder remains on feature/new-checkout-flow. You can now edit, build, test, commit, pull, and push from either folder, provided you keep track of which branch each terminal or editor window is using.
Create a new bug-fix branch in a new worktree
This is the most useful workflow when your original folder contains unfinished work and you need to start a new fix immediately.
Return to the original repository folder:
Set-Location C:\Projects\inventory-app
Fetch the latest remote references if the fix should start from the current remote default branch:
git fetch origin
Then create both a new branch and its parallel worktree. This example starts hotfix/login-timeout from the local main branch:
git worktree add -b hotfix/login-timeout C:\Projects\inventory-app-hotfix main
The command parts are:
git worktree addcreates a linked worktree.-b hotfix/login-timeoutcreates a new local branch.C:\Projects\inventory-app-hotfixis the folder Git will create.mainis the starting point for the new branch.
If you want the new hotfix to start from the freshly fetched remote-tracking branch instead, use:
git worktree add -b hotfix/login-timeout C:\Projects\inventory-app-hotfix origin/main
A branch created from a remote-tracking branch is configured to track that upstream branch by default. Check its configuration after creation:
Set-Location C:\Projects\inventory-app-hotfix
git status -sb
You should see the new branch name. If an upstream was set, the short status output also shows its relationship to the remote branch.
Now make the fix in this folder:
git status
After editing and testing, commit only the hotfix work:
git add .
git commit -m "Fix login timeout handling"
Push the new branch and set its upstream:
git push -u origin hotfix/login-timeout
The original feature work in C:\Projects\inventory-app stays untouched throughout this process.
Check out an existing remote-only branch
Sometimes a branch exists on the remote but has not yet been created locally. For example, a teammate may have pushed release/2.4.1, and you need a separate folder for it.
From the original repository:
Set-Location C:\Projects\inventory-app
git fetch origin
Create a local tracking branch and worktree explicitly:
git worktree add --track -b release/2.4.1 C:\Projects\inventory-app-release-2.4.1 origin/release/2.4.1
This command is deliberate about each part:
--trackrecordsorigin/release/2.4.1as the upstream branch.-b release/2.4.1creates the local branch.- The new path is independent from your main working folder.
origin/release/2.4.1is the remote-tracking branch used as the starting point.
Verify the result:
Set-Location C:\Projects\inventory-app-release-2.4.1
git status -sb
If you use multiple remotes, specify the intended remote explicitly, as in the command above. Avoid relying on Git’s branch-name guessing when a branch name exists on more than one remote.
Open both branch folders in Visual Studio Code
Each worktree is a normal project folder from Visual Studio Code’s perspective. Open each folder in its own VS Code window so the Explorer, integrated terminal, Source Control view, and debugger are all associated with the correct branch.
- In PowerShell, open the original feature folder:
Code:Set-Location C:\Projects\inventory-app code --new-window . - Open the hotfix worktree in another VS Code window:
Code:Set-Location C:\Projects\inventory-app-hotfix code --new-window . - In each VS Code window, open Terminal and run:
git branch --show-current - Check the branch name before editing or committing. The feature window should show the feature branch; the hotfix window should show the hotfix branch.
The code command opens a folder from the command line, and --new-window requests a separate VS Code session instead of reusing the active window. If code is not recognized in PowerShell, VS Code is not available on your Path. Reinstalling VS Code normally restores this configuration, or add VS Code’s bin folder to the Windows Path environment variable.
Do not add two worktree folders from the same repository to one multi-root VS Code workspace unless you have a specific reason. Separate windows make it much harder to edit or commit from the wrong branch accidentally.
Verify every worktree and its branch
From any one of the repository’s worktree folders, run:
git worktree list
Git lists the main worktree first, followed by linked worktrees. A typical result resembles:
C:/Projects/inventory-app a1b2c3d [feature/new-checkout-flow]
C:/Projects/inventory-app-hotfix d4e5f6a [hotfix/login-timeout]
C:/Projects/inventory-app-release-2.4.1 7a8b9c0 [release/2.4.1]
Use this as the authoritative check for:
- The path of each registered worktree.
- The commit checked out in each folder.
- The branch assigned to each folder.
- Whether Git marks a worktree as
lockedorprunable.
Also run this inside the folder you are about to modify:
git status
git branch --show-current
The folder path in your PowerShell prompt and the branch shown by Git should always agree with the work you intend to perform.
Handle dependencies and submodules per worktree
Each worktree has its own working files. That means generated files, package installations, build outputs, and local environment files may need to be created separately in each folder.
For example, a JavaScript or .NET project may require its dependencies to be restored in the new worktree according to the project’s normal setup instructions. Do not assume that dependencies created in the original folder exist in the linked one.
If the repository uses Git submodules, initialize and update them after entering the new worktree:
git submodule update --init --recursive
This initializes missing submodules and processes nested submodules as well. Run it separately in each worktree that needs the submodule contents.
Be cautious with configuration that is shared by default across the repository’s worktrees. Repository-wide Git settings normally apply to all of them. Avoid changing a shared repository setting while assuming it affects only the worktree currently open in your terminal.
Remove a finished worktree safely
When the hotfix is merged or no longer needed, remove its linked folder through Git rather than deleting it in File Explorer.
- Make sure the worktree has no changes you still need:
Code:Set-Location C:\Projects\inventory-app-hotfix git status - Commit, push, copy, or discard any work that must be preserved.
- Move to the original repository or another remaining worktree:
Set-Location C:\Projects\inventory-app - Remove the completed linked worktree:
git worktree remove C:\Projects\inventory-app-hotfix - Confirm that it is gone from Git’s worktree list:
git worktree list
Warning:git worktree removenormally refuses to remove a folder that contains modified tracked files or untracked files. Treat that refusal as protection, not an inconvenience. Reviewgit statusin the target worktree first. Usegit worktree remove --forceonly when you have verified that every uncommitted and untracked file in that folder can be discarded.
Removing a worktree does not delete its Git branch. Delete the branch separately only after confirming it has been merged or is otherwise no longer needed:
git branch -d hotfix/login-timeout
If Git says the branch is not fully merged, stop and verify the branch’s commits before choosing a different deletion approach.
Fix common Git worktree problems
Git says the branch is already checked out
A local branch is already assigned to another worktree. Find the folder using it:
git worktree list
Either work in the existing folder, remove that worktree after cleaning it, or create a different branch for the new folder:
git worktree add -b hotfix/login-timeout-v2 C:\Projects\inventory-app-hotfix-v2 main
Do not bypass the protection with --force unless you fully understand why the duplicate checkout is required.
Git says the destination path already exists
The destination must generally be a new, empty location not already registered to another worktree. Check both the filesystem and Git’s records:
Test-Path C:\Projects\inventory-app-hotfix
git worktree list
If the folder is an obsolete worktree, remove it through git worktree remove. If it was deleted manually in the past, clean up the stale registration after confirming the folder is truly gone:
git worktree prune --dry-run
Review what Git would remove. If the output identifies only obsolete paths, run:
git worktree prune
The linked worktree was moved or renamed in File Explorer
Moving a worktree outside Git can leave its administrative connection pointing to the old location. Use Git’s repair command from a valid worktree and supply the new path:
git worktree repair C:\Projects\inventory-app-hotfix-renamed
For future moves, use Git instead:
git worktree move C:\Projects\inventory-app-hotfix C:\Projects\inventory-app-hotfix-renamed
The new folder is missing submodule files
Enter the affected worktree and initialize its submodules:
Set-Location C:\Projects\inventory-app-hotfix
git submodule update --init --recursive
If access to a private submodule fails, resolve the normal Git authentication or repository-permission issue first, then rerun the command.
code is not recognized in PowerShell
Confirm VS Code’s command-line tool is on Path:
code --version
If it is unavailable, open the folder directly from VS Code with File > Open Folder, or restore the VS Code command-line installation by reinstalling VS Code and opening a new PowerShell window afterward.