Skip to content

Add git worktree switching to Git Tree Compare view#145

Open
filoucool wants to merge 6 commits into
letmaik:masterfrom
filoucool:add-worktree-diff
Open

Add git worktree switching to Git Tree Compare view#145
filoucool wants to merge 6 commits into
letmaik:masterfrom
filoucool:add-worktree-diff

Conversation

@filoucool

Copy link
Copy Markdown
Contributor

Hi,
This PR adds support for viewing diffs across linked git worktrees directly from the Git Tree Compare view. When working with multiple worktrees, you can switch between them without opening each one as a separate workspace window. I've been using this in my custom build for a while now and recently decided to clean this up as well and share it with the community.

What it does

  • Lists all linked worktrees via git worktree list --porcelain
  • "Change Worktree..." menu item. Pick another linked worktree to view its diff tree
  • "Switch to Working Tree" button: Dedicated button to return to your workspace checkout (also pinned as the first option in the Change Worktree menu when in a worktree)
  • Allows viewing worktrees outside the workspace folder, as long as Git confirms they are linked worktrees
  • Shows/hides menu items based on context (viewingWorktree, hasWorktrees)

Changes

  • src/gitHelper.ts: Added IWorktreeInfo interface and listWorktrees() parser
  • src/treeProvider.ts: Added worktree picker, switch-to-working-tree, linked-worktree support in setRepository, and context flag updates
  • src/extension.ts: Registered new commands and initial context values
  • package.json: Added changeWorktree and switchToWorkingTree commands with menu contributions
  • README.md: Documentation

Note

Automatic refresh only watches files inside the VS Code workspace folder. If you need to view a worktree outside the workspace, you need to use Refresh to show changes.

Tests

List of available worktrees:

image

New button to go back to working tree:

image

When trying to switch from a worktree to another worktree, working tree is always shown on top:

image

@filoucool filoucool changed the title Add worktree diff Add git worktree switching to Git Tree Compare view Jun 16, 2026
@letmaik

letmaik commented Jun 17, 2026

Copy link
Copy Markdown
Owner

Thanks for the contribution, interesting idea! I tried it but there seems to be some VS Code limitation I think. When clicking on a file in the tree to view the diff, I'm getting:

image

When I click on Open File for the same entry, it works, and then the diff also works. Do you see the same issue?

This is inside WSL on Windows but not sure it matters.

@filoucool

Copy link
Copy Markdown
Contributor Author

Thanks for the contribution, interesting idea! I tried it but there seems to be some VS Code limitation I think. When clicking on a file in the tree to view the diff, I'm getting:

image When I click on Open File for the same entry, it works, and then the diff also works. Do you see the same issue?

This is inside WSL on Windows but not sure it matters.

Thanks for testing

I tried to reproduce on both native Windows and WSL Ubuntu with an external worktree (sibling folder outside the workspace), and Open Changes worked for me in both cases. I did not see the “file was not found” error.

My setup:

  • Windows: workspace = main repo, worktree = sibling folder outside workspace
  • WSL Ubuntu: workspace = ~/vscode-git-tree-compare, worktree = ~/my-feature-worktree
    In both cases, left clicking a changed file opened the diff without needing Open File first.

Since you’re on WSL as well, this may depend on something more specific in your environment (workspace vs worktree paths, etc.). Could you share:

  • Your workspace folder path
  • Your worktree path (inside or outside the workspace?)
  • Whether Open File still works when the diff fails

I’m happy to dig further if we can narrow it down

@letmaik

letmaik commented Jun 18, 2026

Copy link
Copy Markdown
Owner

@copilot resolve the merge conflicts in this pull request

@letmaik

letmaik commented Jun 18, 2026

Copy link
Copy Markdown
Owner

This only seems to happen when having a workspace with multiple root folders open. In this case it also doesn't work well with "Change repository", it gets confused between the repos. If it's only a single root folder then all works fine.

@trin4ik

trin4ik commented Jun 18, 2026

Copy link
Copy Markdown

i use git worktree manager for fast change between worktrees. and git tree compare for compare all commits/untracked files with main. onelove )

@filoucool

Copy link
Copy Markdown
Contributor Author

I fixed the conflicts since copilot doesnt seem to work

@letmaik

letmaik commented Jun 21, 2026

Copy link
Copy Markdown
Owner

@filoucool Were you able to repro this?

This only seems to happen when having a workspace with multiple root folders open. In this case it also doesn't work well with "Change repository", it gets confused between the repos. If it's only a single root folder then all works fine.

@filoucool

Copy link
Copy Markdown
Contributor Author

@filoucool Were you able to repro this?

This only seems to happen when having a workspace with multiple root folders open. In this case it also doesn't work well with "Change repository", it gets confused between the repos. If it's only a single root folder then all works fine.

@letmaik Thanks for narrowing this down! That explains why I couldn't reproduce. I tested with a single root folder on both native Windows and WSL Ubuntu.

Multi root workspaces aren't handled well yet: getWorkspaceRepositoryRoot() picks the first SCM repo, which can confuse Change Repository / Change Worktree, and toGitUri may resolve against the wrong registered repo when opening diffs. Open File works because it only uses a file: URI. Opening the file first likely registers it with the Git extension, which is why the diff works afterward.

There's an open PR that may address this more broadly: #139 – Add multi-repository tree view. It adds per repository state for multi-root workspaces, which seems like the right direction for this issue. I'd prefer to log this as a separate issue or wait for #139 to merge before adding worktree specific multi root handling.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants